diff --git a/src/libraries/Common/src/Interop/Interop.Locale.iOS.cs b/src/libraries/Common/src/Interop/Interop.Locale.iOS.cs index 3062de8918dc37..d0049b7a7a1734 100644 --- a/src/libraries/Common/src/Interop/Interop.Locale.iOS.cs +++ b/src/libraries/Common/src/Interop/Interop.Locale.iOS.cs @@ -11,7 +11,7 @@ internal static partial class Globalization internal static partial string GetDefaultLocaleNameNative(); [LibraryImport(Libraries.GlobalizationNative, EntryPoint = "GlobalizationNative_GetLocaleInfoStringNative", StringMarshalling = StringMarshalling.Utf8)] - internal static partial string GetLocaleInfoStringNative(string localeName, uint localeStringData); + internal static partial string GetLocaleInfoStringNative(string localeName, uint localeStringData, string? uiLocaleName = null); [LibraryImport(Libraries.GlobalizationNative, EntryPoint = "GlobalizationNative_GetLocaleInfoIntNative", StringMarshalling = StringMarshalling.Utf8)] internal static partial int GetLocaleInfoIntNative(string localeName, uint localeNumberData); diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs index 1973aa523bcc0d..7013429ad4c0e5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs @@ -976,7 +976,12 @@ internal string DisplayName private string GetLanguageDisplayNameCore(string cultureName) => GlobalizationMode.UseNls ? NlsGetLanguageDisplayName(cultureName) : +#if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS + GlobalizationMode.Hybrid ? GetLocaleInfoNative(cultureName, LocaleStringData.LocalizedDisplayName, CultureInfo.CurrentUICulture.Name) : + IcuGetLanguageDisplayName(cultureName); +#else IcuGetLanguageDisplayName(cultureName); +#endif /// /// English pretty name for this locale (ie: English (United States)) @@ -2371,7 +2376,7 @@ private string GetLocaleInfoCore(LocaleStringData type, string? uiCultureName = return null!; #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS - return GlobalizationMode.Hybrid ? GetLocaleInfoNative(type) : IcuGetLocaleInfo(type, uiCultureName); + return GlobalizationMode.Hybrid ? GetLocaleInfoNative(type, uiCultureName) : IcuGetLocaleInfo(type, uiCultureName); #else return GlobalizationMode.UseNls ? NlsGetLocaleInfo(type) : IcuGetLocaleInfo(type, uiCultureName); #endif @@ -2384,7 +2389,7 @@ private string GetLocaleInfoCore(string localeName, LocaleStringData type, strin return null!; #if TARGET_MACCATALYST || TARGET_IOS || TARGET_TVOS - return GlobalizationMode.Hybrid ? GetLocaleInfoNative(localeName, type) : IcuGetLocaleInfo(localeName, type, uiCultureName); + return GlobalizationMode.Hybrid ? GetLocaleInfoNative(localeName, type, uiCultureName) : IcuGetLocaleInfo(localeName, type, uiCultureName); #else return GlobalizationMode.UseNls ? NlsGetLocaleInfo(localeName, type) : IcuGetLocaleInfo(localeName, type, uiCultureName); #endif diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.iOS.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.iOS.cs index 6e1b43bfd08986..0617855576a88b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.iOS.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.iOS.cs @@ -14,21 +14,21 @@ internal static string GetLocaleNameNative(string localeName) return Interop.Globalization.GetLocaleNameNative(localeName); } - private string GetLocaleInfoNative(LocaleStringData type) + private string GetLocaleInfoNative(LocaleStringData type, string? uiLocaleName = null) { Debug.Assert(!GlobalizationMode.Invariant); Debug.Assert(_sWindowsName != null, "[CultureData.GetLocaleInfoNative] Expected _sWindowsName to be populated already"); - return GetLocaleInfoNative(_sWindowsName, type); + return GetLocaleInfoNative(_sWindowsName, type, uiLocaleName); } // For LOCALE_SPARENT we need the option of using the "real" name (forcing neutral names) instead of the // "windows" name, which can be specific for downlevel (< windows 7) os's. - private static string GetLocaleInfoNative(string localeName, LocaleStringData type) + private static string GetLocaleInfoNative(string localeName, LocaleStringData type, string? uiLocaleName = null) { Debug.Assert(localeName != null, "[CultureData.GetLocaleInfoNative] Expected localeName to be not be null"); - return Interop.Globalization.GetLocaleInfoStringNative(localeName, (uint)type); + return Interop.Globalization.GetLocaleInfoStringNative(localeName, (uint)type, uiLocaleName); } private int GetLocaleInfoNative(LocaleNumberData type) diff --git a/src/libraries/System.Runtime/tests/System.Globalization.Tests/CultureInfo/CultureInfoNames.cs b/src/libraries/System.Runtime/tests/System.Globalization.Tests/CultureInfo/CultureInfoNames.cs index e6da8fc7a5518e..2cce0e5b842c62 100644 --- a/src/libraries/System.Runtime/tests/System.Globalization.Tests/CultureInfo/CultureInfoNames.cs +++ b/src/libraries/System.Runtime/tests/System.Globalization.Tests/CultureInfo/CultureInfoNames.cs @@ -10,7 +10,7 @@ namespace System.Globalization.Tests { public class CultureInfoNames { - private static bool SupportFullIcuResources => PlatformDetection.IsNotMobile && PlatformDetection.IsIcuGlobalization; + private static bool SupportFullIcuResources => (PlatformDetection.IsNotMobile && PlatformDetection.IsIcuGlobalization) || PlatformDetection.IsHybridGlobalizationOnOSX; [ConditionalTheory(nameof(SupportFullIcuResources))] [InlineData("en", "en", "English", "English")] diff --git a/src/native/libs/System.Globalization.Native/pal_locale.m b/src/native/libs/System.Globalization.Native/pal_locale.m index fce8f03a4fc704..9cb806cd1a650a 100644 --- a/src/native/libs/System.Globalization.Native/pal_locale.m +++ b/src/native/libs/System.Globalization.Native/pal_locale.m @@ -135,7 +135,7 @@ static int16_t _findIndex(const char* const* list, const char* key) return LANGUAGES_3[offset]; } -const char* GlobalizationNative_GetLocaleInfoStringNative(const char* localeName, LocaleStringData localeStringData) +const char* GlobalizationNative_GetLocaleInfoStringNative(const char* localeName, LocaleStringData localeStringData, const char* currentUILocaleName) { @autoreleasepool { @@ -152,6 +152,12 @@ static int16_t _findIndex(const char* const* list, const char* key) { ///// localized name of locale, eg "German (Germany)" in UI language (corresponds to LOCALE_SLOCALIZEDDISPLAYNAME) case LocaleString_LocalizedDisplayName: + { + NSString *currUILocaleName = [NSString stringWithFormat:@"%s", currentUILocaleName == NULL ? GlobalizationNative_GetDefaultLocaleNameNative() : currentUILocaleName]; + NSLocale *currentUILocale = [[NSLocale alloc] initWithLocaleIdentifier:currUILocaleName]; + value = [currentUILocale displayNameForKey:NSLocaleIdentifier value:currentLocale.localeIdentifier]; + break; + } /// Display name (language + country usually) in English, eg "German (Germany)" (corresponds to LOCALE_SENGLISHDISPLAYNAME) case LocaleString_EnglishDisplayName: value = [gbLocale displayNameForKey:NSLocaleIdentifier value:currentLocale.localeIdentifier]; @@ -162,6 +168,12 @@ static int16_t _findIndex(const char* const* list, const char* key) break; /// Language Display Name for a language, eg "German" in UI language (corresponds to LOCALE_SLOCALIZEDLANGUAGENAME) case LocaleString_LocalizedLanguageName: + { + NSString *currUILocaleName = [NSString stringWithFormat:@"%s", currentUILocaleName == NULL ? GlobalizationNative_GetDefaultLocaleNameNative() : currentUILocaleName]; + NSLocale *currentUILocale = [[NSLocale alloc] initWithLocaleIdentifier:currUILocaleName]; + value = [currentUILocale localizedStringForLanguageCode:currentLocale.languageCode]; + break; + } /// English name of language, eg "German" (corresponds to LOCALE_SENGLISHLANGUAGENAME) case LocaleString_EnglishLanguageName: value = [gbLocale localizedStringForLanguageCode:currentLocale.languageCode]; @@ -781,24 +793,31 @@ int32_t GlobalizationNative_GetLocalesNative(UChar* value, int32_t length) { @autoreleasepool { - NSLocale *currentLocale = [NSLocale currentLocale]; - NSString *localeName = @""; - - if (!currentLocale) - { - return strdup([localeName UTF8String]); - } - - if ([currentLocale.languageCode length] > 0 && [currentLocale.countryCode length] > 0) + if (NSLocale.preferredLanguages.count > 0) { - localeName = [NSString stringWithFormat:@"%@-%@", currentLocale.languageCode, currentLocale.countryCode]; + return strdup([NSLocale.preferredLanguages[0] UTF8String]); } else { - localeName = currentLocale.localeIdentifier; - } + NSLocale *currentLocale = [NSLocale currentLocale]; + NSString *localeName = @""; - return strdup([localeName UTF8String]); + 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]); + } } } diff --git a/src/native/libs/System.Globalization.Native/pal_localeStringData.h b/src/native/libs/System.Globalization.Native/pal_localeStringData.h index a6961c39761bfd..b93d5d7dc85f47 100644 --- a/src/native/libs/System.Globalization.Native/pal_localeStringData.h +++ b/src/native/libs/System.Globalization.Native/pal_localeStringData.h @@ -51,6 +51,6 @@ PALEXPORT int32_t GlobalizationNative_GetLocaleInfoString(const UChar* localeNam const UChar* uiLocaleName); #ifdef __APPLE__ -PALEXPORT const char* GlobalizationNative_GetLocaleInfoStringNative(const char* localeName, LocaleStringData localeStringData); +PALEXPORT const char* GlobalizationNative_GetLocaleInfoStringNative(const char* localeName, LocaleStringData localeStringData, const char* currentUILocaleName); #endif