Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locale template filter #1679

Open
RhetTbull opened this issue Sep 12, 2024 · 0 comments
Open

Add locale template filter #1679

RhetTbull opened this issue Sep 12, 2024 · 0 comments
Labels
feature request New feature or request

Comments

@RhetTbull
Copy link
Owner

RhetTbull commented Sep 12, 2024

See this Reddit thread

Currently, some text such as {place.name.country} will render in English as it appears Apple stores only English strings in the database. A user might want to have these rendered in the language specified by the locale. Add a locale template function that does this:

{place.name.country|locale} would use the current locale.
{place.name.country|locale(fr)} would force the use French as the locale language, locale(es) would force use of Spanish, and so on.

This would work for any string, not just country names.

The following code shows how to use the locale functions:

import time
import objc
from Foundation import NSLocale, NSRunLoop, NSDate, NSUserDefaults, NSArray
import CoreLocation

def set_locale(language_code):
    """set the locale to the specified language code."""
    # note that this overwrites user defaults!!! 
    user_defaults = NSUserDefaults.standardUserDefaults()
    user_defaults.setObject_forKey_(NSArray.arrayWithObject_(language_code), "AppleLanguages")
    user_defaults.synchronize()
    print(f"Locale temporarily set to: {language_code}")

def get_localized_placemark_name(placemark):
    current_locale = NSLocale.currentLocale()
    # Check if placemark.name exists, some geocoding responses may not have this property
    name = placemark.country()
    if name:
        localized_description = name.stringByFoldingWithOptions_locale_(
            0, current_locale
        )
        return localized_description
    return "No name available"

def geocode_address():
    geocoder = CoreLocation.CLGeocoder.alloc().init()
    address = "1 Infinite Loop, Cupertino, CA"  # Example address to geocode
    placemark_found = False

    def completion_handler(placemarks, error):
        nonlocal placemark_found
        if placemarks and len(placemarks) > 0:
            placemark = placemarks[0]
            localized_name = get_localized_placemark_name(placemark)
            print(f"Localized description: {localized_name}")
        else:
            print(f"Geocoding failed: {error}")
        placemark_found = True

    # Perform the geocoding asynchronously
    geocoder.geocodeAddressString_completionHandler_(address, completion_handler)

    # Run the event loop until the geocoding is complete
    run_loop = NSRunLoop.currentRunLoop()
    while not placemark_found:
        run_loop.runMode_beforeDate_(
            'NSDefaultRunLoopMode', NSDate.dateWithTimeIntervalSinceNow_(0.1)
        )

if __name__ == "__main__":
    set_locale('fr')
    geocode_address()
@RhetTbull RhetTbull added the feature request New feature or request label Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant