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
{{ message }}
Repository navigation
macOS / Mac Catalyst / iOS: CurrentThread.Current[UI]Culture is always the invariant culture #68321
In legacy Xamarin, the current culture was set in the get_darwin_locale method in mono. This function doesn't exist anymore in this repo (removed in 738066c).
That's really only feasible for command-line executables. There's no user-friendly way to set LANG for normal (UI) apps.
Agree. The UI apps can force the culture in the startup code too. I am not suggesting that but just mentioning it.
Looks like we're relying on the default to come out of ICU, which I believe is "en_US_POSIX" and we subsequently ignore
does en_US_POSIX set on the device/machine? I think what is happening is there is no locale set in the environment variable LANG nor LC_ALL which causes us to fallback to invariant. uloc_getDefault() just return empty string I guess.
I think in addition to porting get_darwin_locale, maybe also port get_posix_locale at the same time? This way when icuLocale has POSIX locale, before falling back to Invariant on Linux as we currently do, it will attempt to use setlocale(.., NULL) (which implies "get locale") on Linux / FreeBSD etc. and CFLocaleGetValue on Darwin platforms.
@steveisok I looked into setting LANG in the native main method as a workaround, and I ran into a few issues:
It doesn't work with CoreCLR (i.e. on macOS), because CoreCLR links with the system libicucore.dylib, and that library reads LANG in a dylib initializer (so before the native main method):
It seems to work when using Mono as the runtime (iOS, tvOS, Mac Catalyst), until an unknown value for LANG is set (say LANG=en-garbage), then the app crashes: https://gist.github.com/rolfbjarne/59de02a3facfbb723f21466ac692f0e4. The fact that it crashes might be a separate issue, but in order to make the workaround useful we'd have to fix the crash too, and that makes it less viable as a last moment temporary workaround. And even if we did fix the crash itself, we'd still probably throw an exception, which app developers would likely have a hard time track down (because it wouldn't happen on their devices, only on end user devices with uncommon regional settings).
This leads me to believe that setting LANG isn't viable as a workaround, it's much better to have an easily understandable bug that app developers can work around themselves instead.
@rolfbjarne the crash because you are using a locale name en-garbage. When calling ICU library to normalize this name, it returns something like en__GARBAGE which is invalid name. Try to use a name with a second part is 4 characters or less. Something like en-GARB.
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
rolfbjarne commentedon Apr 21, 2022
In legacy Xamarin, the current culture was set in the get_darwin_locale method in mono. This function doesn't exist anymore in this repo (removed in 738066c).
Ref: dotnet/macios#14740 (comment)
rolfbjarne commentedon Apr 21, 2022
CC @steveisok @akoeplinger
rolfbjarne commentedon Apr 21, 2022
For reference, this is the output of an equivalent Xamarin.Mac test project:
steveisok commentedon Apr 21, 2022
Looks like we're relying on the default to come out of ICU, which I believe is "en_US_POSIX" and we subsequently ignore
runtime/src/native/libs/System.Globalization.Native/pal_locale.c
Lines 138 to 141 in 7414af2
The right way would be to add a get_darwin_locale equivalent.
am11 commentedon Apr 21, 2022
ICU uses system default, which you can check with
locale(1)command or from environment vars like$LANG,$LC_ALLand$LC_MESSAGESetc.This applies to all supported Unix OS and versions of .NET Core; it is not specific to Darwin and .NET 6.
tarekgh commentedon Apr 21, 2022
Yes, setting the
LANGenvironment variable should workaround this issue. But I hope this can be fixed as @steveisok mentioned in #68321 (comment).@steveisok will you take care with this issue? I am seeing you have marked for
6.x.rolfbjarne commentedon Apr 21, 2022
That's really only feasible for command-line executables. There's no user-friendly way to set LANG for normal (UI) apps.
tarekgh commentedon Apr 21, 2022
Agree. The UI apps can force the culture in the startup code too. I am not suggesting that but just mentioning it.
does
en_US_POSIXset on the device/machine? I think what is happening is there is no locale set in the environment variableLANGnorLC_ALLwhich causes us to fallback to invariant.uloc_getDefault()just return empty string I guess.runtime/src/native/libs/System.Globalization.Native/pal_locale.c
Line 137 in 2389815
5 remaining items
am11 commentedon Apr 21, 2022
I think in addition to porting
get_darwin_locale, maybe also portget_posix_localeat the same time? This way whenicuLocalehasPOSIXlocale, before falling back to Invariant on Linux as we currently do, it will attempt to usesetlocale(.., NULL)(which implies "get locale") on Linux / FreeBSD etc. andCFLocaleGetValueon Darwin platforms.tarekgh commentedon Apr 21, 2022
@am11 we are currently calling ICU
uloc_getDefaultAPI, which does all the logic you are mentioning here. https://github.com/unicode-org/icu/blob/fca6b342c03d89998eba227d284e232dabbfab47/icu4c/source/common/putil.cpp#L1676. The only missing part is on Darwin, it doesn't callCFLocaleGetValue.rolfbjarne commentedon Apr 28, 2022
@steveisok I looked into setting LANG in the native main method as a workaround, and I ran into a few issues:
It doesn't work with CoreCLR (i.e. on macOS), because CoreCLR links with the system libicucore.dylib, and that library reads LANG in a dylib initializer (so before the native
mainmethod):It seems to work when using Mono as the runtime (iOS, tvOS, Mac Catalyst), until an unknown value for LANG is set (say
LANG=en-garbage), then the app crashes: https://gist.github.com/rolfbjarne/59de02a3facfbb723f21466ac692f0e4. The fact that it crashes might be a separate issue, but in order to make the workaround useful we'd have to fix the crash too, and that makes it less viable as a last moment temporary workaround. And even if we did fix the crash itself, we'd still probably throw an exception, which app developers would likely have a hard time track down (because it wouldn't happen on their devices, only on end user devices with uncommon regional settings).This leads me to believe that setting LANG isn't viable as a workaround, it's much better to have an easily understandable bug that app developers can work around themselves instead.
tarekgh commentedon Apr 28, 2022
@rolfbjarne the crash because you are using a locale name
en-garbage. When calling ICU library to normalize this name, it returns something likeen__GARBAGEwhich is invalid name. Try to use a name with a second part is 4 characters or less. Something likeen-GARB.Detect the default locale name during startup on Apple platforms
Detect the default locale name during startup on Apple platforms (#68706
Detect the default locale name during startup on Apple platforms
[release/6.0] Detect the default locale name during startup on Apple …