chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
806 stars 132 forks source link

Support standard library `locale` module #1150

Open mhsmith opened 4 months ago

mhsmith commented 4 months ago

From https://github.com/beeware/toga/discussions/2562:

Hello, how can I recognize the current language with locale.getlocale()? When i try it on android, it only returns ('en_US', 'UTF-8'), even if default language was set to an other language.

I don't think Android sets any of the standard LC_... environment variables, but we can get this information from the Android API:

# In a Toga app, the equivalent of `context` is `app._impl.native`, 
# where `app` is your `toga.App` object.
str(context.getResources().getConfiguration().getLocales().get(0))

This will return a string in the form en_US, which can be passed to locale.setlocale.

freakboy3742 commented 4 months ago

Is this not something that could/should be upstreamed into CPython itself? Or is this a case where because the usual C API isn't honored, fixing it would require the use of a Java API, would require either CPython to adopt Chaquopy (or another JNI wrapper), or the implementation of locale to replicate enough raw JNI handling to satisfy the API call?

There's a broad analog of this with the iOS implementation of webbrowser - that code does the bare minimum ctypes invocation to call the native Objective C API to launch the browser. Would something analogous to that be possible for Android (both for webbrowser, and for locale?)

At the very least, it seems like there should be a note in the CPython docs that locale has limitations on Android.

mhsmith commented 4 months ago

Would something analogous to that be possible for Android (both for webbrowser, and for locale?)

Yes, but as with webbrowser (#1149), an upstream implementation would need to use a non-public API to get a reference to the context.

At the very least, it seems like there should be a note in the CPython docs

I'll include it in a future docs PR when I update the "Availability" markers for Android.