You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importtimeimportobjcfromFoundationimportNSLocale, NSRunLoop, NSDate, NSUserDefaults, NSArrayimportCoreLocationdefset_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}")
defget_localized_placemark_name(placemark):
current_locale=NSLocale.currentLocale()
# Check if placemark.name exists, some geocoding responses may not have this propertyname=placemark.country()
ifname:
localized_description=name.stringByFoldingWithOptions_locale_(
0, current_locale
)
returnlocalized_descriptionreturn"No name available"defgeocode_address():
geocoder=CoreLocation.CLGeocoder.alloc().init()
address="1 Infinite Loop, Cupertino, CA"# Example address to geocodeplacemark_found=Falsedefcompletion_handler(placemarks, error):
nonlocalplacemark_foundifplacemarksandlen(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 asynchronouslygeocoder.geocodeAddressString_completionHandler_(address, completion_handler)
# Run the event loop until the geocoding is completerun_loop=NSRunLoop.currentRunLoop()
whilenotplacemark_found:
run_loop.runMode_beforeDate_(
'NSDefaultRunLoopMode', NSDate.dateWithTimeIntervalSinceNow_(0.1)
)
if__name__=="__main__":
set_locale('fr')
geocode_address()
The text was updated successfully, but these errors were encountered:
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 alocale
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:
The text was updated successfully, but these errors were encountered: