Skip to content

macOS / Mac Catalyst / iOS: CurrentThread.Current[UI]Culture is always the invariant culture #68321

Closed

Description

@rolfbjarne

Description

Thread.CurrentThread.Current[UI]Culture is always the invariant culture at launch in macOS and Mac Catalyst (and probably iOS and tvOS as well).

This affects both Mono (on Mac Catalyst) and CoreCLR (no macOS).

Ref: dotnet/macios#14740
Ref: dotnet/maui#3962

Reproduction Steps

  1. Create a new Mac Catalyst app:
dotnet new maccatalyst
  1. Print out Current[UI]Culture at launch by changing the Main.cs file to:
using System.Globalization;

Console.WriteLine ($"CurrentUICulture: {GetString (Thread.CurrentThread.CurrentUICulture)}");
Console.WriteLine ($"CurrentCulture: {GetString (Thread.CurrentThread.CurrentCulture)}");

Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");

Console.WriteLine ("After setting to 'en-US':");
Console.WriteLine ($"CurrentUICulture: {GetString (Thread.CurrentThread.CurrentUICulture)}");
Console.WriteLine ($"CurrentCulture: {GetString (Thread.CurrentThread.CurrentCulture)}");


static string GetString (CultureInfo ci)
{
	return $"Name: {ci.Name} NativeName: '{ci.NativeName}' DisplayName: '{ci.DisplayName}' EnglishName: '{ci.EnglishName}'";
}
  1. Build
dotnet run
  1. Run the executable (not the app) to get stdout written to a terminal:
$ ./bin/Debug/net6.0-maccatalyst/maccatalyst-x64/currentculturetest.app/Contents/MacOS/currentculturetest
[...]
CurrentUICulture: Name:  NativeName: 'Invariant Language (Invariant Country)' DisplayName: 'Invariant Language (Invariant Country)' EnglishName: 'Invariant Language (Invariant Country)'
CurrentCulture: Name:  NativeName: 'Invariant Language (Invariant Country)' DisplayName: 'Invariant Language (Invariant Country)' EnglishName: 'Invariant Language (Invariant Country)'
After setting to 'en-US':
CurrentUICulture: Name: en-US NativeName: 'en (US)' DisplayName: 'en (US)' EnglishName: 'en (US)'
CurrentCulture: Name: en-US NativeName: 'en (US)' DisplayName: 'en (US)' EnglishName: 'en (US)

Complete test project: currentculturetest-d0b7a3f.zip

Expected behavior

CurrentThread.Current[UI]Culture should reflect whatever's configured in the OS.

Actual behavior

Always Invariant culture

Regression?

Don't know

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Apr 21, 2022
  2. rolfbjarne commented on Apr 21, 2022

    @rolfbjarne
    MemberAuthor

    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)

  3. rolfbjarne commented on Apr 21, 2022

    @rolfbjarne
    MemberAuthor
  4. rolfbjarne commented on Apr 21, 2022

    @rolfbjarne
    MemberAuthor

    For reference, this is the output of an equivalent Xamarin.Mac test project:

    $ ./bin/Debug/mactestapp.app/Contents/MacOS/mactestapp
    CurrentUICulture: Name: en-US NativeName: 'English (United States)' DisplayName: 'English (United States)' EnglishName: 'English (United States)'
    CurrentCulture: Name: en-US NativeName: 'English (United States)' DisplayName: 'English (United States)' EnglishName: 'English (United States)'
    After setting to 'en-US':
    CurrentUICulture: Name: en-US NativeName: 'English (United States)' DisplayName: 'English (United States)' EnglishName: 'English (United States)'
    CurrentCulture: Name: en-US NativeName: 'English (United States)' DisplayName: 'English (United States)' EnglishName: 'English (United States)'
    
  5. removed
    untriaged
    on Apr 21, 2022
  6. added this to the 6.0.x milestone on Apr 21, 2022
  7. self-assigned this
    on Apr 21, 2022
  8. steveisok commented on Apr 21, 2022

    @steveisok
    Member

    Looks like we're relying on the default to come out of ICU, which I believe is "en_US_POSIX" and we subsequently ignore

    if (strcmp(icuLocale, "en_US_POSIX") == 0)
    {
    return "";
    }

    The right way would be to add a get_darwin_locale equivalent.

  9. am11 commented on Apr 21, 2022

    @am11
    Member

    ICU uses system default, which you can check with locale(1) command or from environment vars like $LANG, $LC_ALL and $LC_MESSAGES etc.

    6.0.x milestone

    This applies to all supported Unix OS and versions of .NET Core; it is not specific to Darwin and .NET 6.

  10. tarekgh commented on Apr 21, 2022

    @tarekgh
    Member

    Yes, setting the LANG environment 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.

  11. rolfbjarne commented on Apr 21, 2022

    @rolfbjarne
    MemberAuthor

    Yes, setting the LANG environment variable should workaround this issue.

    That's really only feasible for command-line executables. There's no user-friendly way to set LANG for normal (UI) apps.

  12. tarekgh commented on Apr 21, 2022

    @tarekgh
    Member

    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.

    const char* icuLocale = uloc_getDefault();

  13. added
    os-macos
    on Apr 21, 2022
  14. 5 remaining items

  15. am11 commented on Apr 21, 2022

    @am11
    Member

    What are you suggesting doing here?

    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.

  16. tarekgh commented on Apr 21, 2022

    @tarekgh
    Member

    @am11 we are currently calling ICU uloc_getDefault API, 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 call CFLocaleGetValue.

  17. rolfbjarne commented on Apr 28, 2022

    @rolfbjarne
    MemberAuthor

    @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):

    (lldb) bt
    * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.4
      * frame #0: 0x00007ff800657090 libsystem_c.dylib`getenv
        frame #1: 0x00007ff802b58ccb libicucore.A.dylib`uprv_getDefaultLocaleID + 137
        frame #2: 0x00007ff802b3d832 libicucore.A.dylib`___lldb_unnamed_symbol229$$libicucore.A.dylib + 58
        frame #3: 0x00007ff802b3e073 libicucore.A.dylib`icu::Locale::getDefault() + 63
        frame #4: 0x00007ff802b3e02e libicucore.A.dylib`___lldb_unnamed_symbol233$$libicucore.A.dylib + 9
        frame #5: 0x00007ff802bd4b0e libicucore.A.dylib`___lldb_unnamed_symbol1298$$libicucore.A.dylib + 663
        frame #6: 0x00007ff802b4660a libicucore.A.dylib`uloc_getTableStringWithFallback + 140
        frame #7: 0x00007ff802b39855 libicucore.A.dylib`___lldb_unnamed_symbol183$$libicucore.A.dylib + 171
        frame #8: 0x00007ff802b37e6d libicucore.A.dylib`uloc_getDisplayLanguage + 207
        frame #9: 0x00007ff800816779 CoreFoundation`__CFLocaleLanguageName + 73
        frame #10: 0x00007ff8008165d3 CoreFoundation`CFLocaleCopyDisplayNameForPropertyValue + 354
        frame #11: 0x00007ff800816465 CoreFoundation`-[__NSCFLocale displayNameForKey:value:] + 15
        frame #12: 0x00007ff80164ce98 Foundation`-[NSUserDefaults(NSUserDefaults) init] + 1626
        frame #13: 0x00007ff80164c7fe Foundation`+[NSUserDefaults(NSUserDefaults) standardUserDefaults] + 66
        frame #14: 0x00007ff80323d415 AppKit`_NSGetBoolAppConfig + 66
        frame #15: 0x00007ff803adb74d AppKit`registerTaggedPointerColorsIfNecessary + 85
        frame #16: 0x00007ff80323d354 AppKit`_NSColorSpaceTaggedPointerColor + 24
        frame #17: 0x00007ff8032427d6 AppKit`+[NSColor colorWithColorSpace:components:count:] + 33
        frame #18: 0x00007ff80347255d AppKit`+[NSColor colorWithRed:green:blue:alpha:] + 176
        frame #19: 0x00007ffa1d560b1c GameplayKit`_GLOBAL__sub_I_GKNoiseMap.mm + 92
        frame #20: 0x000000010045cddb dyld`invocation function for block in dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const + 182
        frame #21: 0x0000000100483257 dyld`invocation function for block in dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const + 242
        frame #22: 0x000000010047a85a dyld`invocation function for block in dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const + 557
        frame #23: 0x0000000100449db3 dyld`dyld3::MachOFile::forEachLoadCommand(Diagnostics&, void (load_command const*, bool&) block_pointer) const + 129
        frame #24: 0x000000010047a5eb dyld`dyld3::MachOFile::forEachSection(void (dyld3::MachOFile::SectionInfo const&, bool, bool&) block_pointer) const + 179
        frame #25: 0x0000000100482dae dyld`dyld3::MachOAnalyzer::forEachInitializer(Diagnostics&, dyld3::MachOAnalyzer::VMAddrConverter const&, void (unsigned int) block_pointer, void const*) const + 466
        frame #26: 0x000000010045cd0e dyld`dyld4::Loader::findAndRunAllInitializers(dyld4::RuntimeState&) const + 144
        frame #27: 0x000000010046322a dyld`dyld4::PrebuiltLoader::runInitializers(dyld4::RuntimeState&) const + 30
        frame #28: 0x000000010045ce9a dyld`dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const + 178
        frame #29: 0x000000010045ce7d dyld`dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const + 149
        frame #30: 0x000000010045ce7d dyld`dyld4::Loader::runInitializersBottomUp(dyld4::RuntimeState&, dyld3::Array<dyld4::Loader const*>&) const + 149
        frame #31: 0x000000010045cf3e dyld`dyld4::Loader::runInitializersBottomUpPlusUpwardLinks(dyld4::RuntimeState&) const + 108
        frame #32: 0x000000010047054e dyld`dyld4::APIs::runAllInitializersForMain() + 222
        frame #33: 0x000000010044e37d dyld`dyld4::prepare(dyld4::APIs&, dyld3::MachOAnalyzer const*) + 3443
        frame #34: 0x000000010044d4d4 dyld`start + 388
    (lldb) p (char *) $rdi
    (char *) $1 = 0x00007ff802da24e9 "LANG"
    

    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.

  18. tarekgh commented on Apr 28, 2022

    @tarekgh
    Member

    @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.

  19. ghost added
    in-pr
    on Apr 29, 2022
  20. added a commit that references this issue on May 5, 2022
    45887d1
  21. ghost removed
    in-pr
    on May 5, 2022
  22. ghost locked as resolved and limited conversation to collaborators on Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Globalizationos-iosos-maccatalystos-macosos-tvos

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions