From e7d4827e745960d36c73b1fd934d9c3b1e61dce9 Mon Sep 17 00:00:00 2001 From: Matous Kozak Date: Tue, 28 May 2024 11:32:24 +0200 Subject: [PATCH] use preferred language on Apple platforms --- .../System.Globalization.Native/pal_locale.m | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/native/libs/System.Globalization.Native/pal_locale.m b/src/native/libs/System.Globalization.Native/pal_locale.m index 4789ac89691da2..758bb5778ada27 100644 --- a/src/native/libs/System.Globalization.Native/pal_locale.m +++ b/src/native/libs/System.Globalization.Native/pal_locale.m @@ -16,24 +16,34 @@ char* DetectDefaultAppleLocaleName(void) { - NSLocale *currentLocale = [NSLocale currentLocale]; - NSString *localeName = @""; - - if (!currentLocale) + @autoreleasepool { - return strdup([localeName UTF8String]); - } + if (NSLocale.preferredLanguages.count > 0) + { + return strdup([NSLocale.preferredLanguages[0] UTF8String]); + } + else + { + NSLocale *currentLocale = [NSLocale currentLocale]; + NSString *localeName = @""; - if ([currentLocale.languageCode length] > 0 && [currentLocale.countryCode length] > 0) - { - localeName = [NSString stringWithFormat:@"%@-%@", currentLocale.languageCode, currentLocale.countryCode]; - } - else - { - localeName = currentLocale.localeIdentifier; - } + if (!currentLocale) + { + return strdup([localeName UTF8String]); + } + + if ([currentLocale.languageCode length] > 0 && [currentLocale.countryCode length] > 0) + { + localeName = [NSString stringWithFormat:@"%@-%@", currentLocale.languageCode, currentLocale.countryCode]; + } + else + { + localeName = currentLocale.localeIdentifier; + } - return strdup([localeName UTF8String]); + return strdup([localeName UTF8String]); + } + } } #if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)