diff --git a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt index a3837f8049220..bd6a653261c67 100644 --- a/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt +++ b/src/coreclr/dlls/mscoree/coreclr/CMakeLists.txt @@ -166,10 +166,15 @@ if(FEATURE_MERGE_JIT_AND_ENGINE) set(CLRJIT_STATIC clrjit_static) endif(FEATURE_MERGE_JIT_AND_ENGINE) +if (CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_MACCATALYST) + include(CMakeFindFrameworks) + find_library(FOUNDATION Foundation REQUIRED) +endif() + target_sources(coreclr PUBLIC $) -target_link_libraries(coreclr PUBLIC ${CORECLR_LIBRARIES} ${CLRJIT_STATIC} cee_wks) +target_link_libraries(coreclr PUBLIC ${CORECLR_LIBRARIES} ${CLRJIT_STATIC} cee_wks ${FOUNDATION}) target_sources(coreclr_static PUBLIC $) -target_link_libraries(coreclr_static PUBLIC ${CORECLR_LIBRARIES} clrjit_static cee_wks_mergeable) +target_link_libraries(coreclr_static PUBLIC ${CORECLR_LIBRARIES} clrjit_static cee_wks_mergeable ${FOUNDATION}) target_compile_definitions(coreclr_static PUBLIC CORECLR_EMBEDDED) if(CLR_CMAKE_TARGET_WIN32) diff --git a/src/libraries/System.Globalization/tests/CultureInfo/CultureInfoCurrentCulture.cs b/src/libraries/System.Globalization/tests/CultureInfo/CultureInfoCurrentCulture.cs index 9215095756e98..44afccec6b9f8 100644 --- a/src/libraries/System.Globalization/tests/CultureInfo/CultureInfoCurrentCulture.cs +++ b/src/libraries/System.Globalization/tests/CultureInfo/CultureInfoCurrentCulture.cs @@ -33,6 +33,17 @@ public void CurrentCulture() } } + [Fact] + [PlatformSpecific(TestPlatforms.OSX | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS)] + public void CurrentCulture_Default_Not_Invariant() + { + // On OSX-like platforms, it should default to what the default system culture is + // set to. Since we shouldn't assume en-US, we just test if it's not the invariant + // culture. + Assert.NotEqual(CultureInfo.CurrentCulture, CultureInfo.InvariantCulture); + Assert.NotEqual(CultureInfo.CurrentUICulture, CultureInfo.InvariantCulture); + } + [Fact] public void CurrentCulture_Set_Null_ThrowsArgumentNullException() { @@ -125,7 +136,7 @@ public void CurrentCulture_BasedOnLangEnvVar(string langEnvVar, string expectedC }, expectedCultureName, new RemoteInvokeOptions { StartInfo = psi }).Dispose(); } - [PlatformSpecific(TestPlatforms.AnyUnix)] // When LANG is empty or unset, should default to the invariant culture on Unix. + [PlatformSpecific(TestPlatforms.AnyUnix)] [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] [InlineData("")] [InlineData(null)] @@ -141,13 +152,28 @@ public void CurrentCulture_DefaultWithNoLang(string langEnvVar) psi.Environment["LANG"] = langEnvVar; } + // When LANG is empty or unset, on Unix it should default to the invariant culture. + // On OSX-like platforms, it should default to what the default system culture is + // set to. Since we shouldn't assume en-US, we just test if it's not the invariant + // culture. RemoteExecutor.Invoke(() => { Assert.NotNull(CultureInfo.CurrentCulture); Assert.NotNull(CultureInfo.CurrentUICulture); - Assert.Equal("", CultureInfo.CurrentCulture.Name); - Assert.Equal("", CultureInfo.CurrentUICulture.Name); + if (PlatformDetection.IsOSXLike) + { + Assert.NotEqual("", CultureInfo.CurrentCulture.Name); + Assert.NotEqual("", CultureInfo.CurrentUICulture.Name); + + Assert.NotEqual(CultureInfo.CurrentCulture, CultureInfo.InvariantCulture); + Assert.NotEqual(CultureInfo.CurrentUICulture, CultureInfo.InvariantCulture); + } + else + { + Assert.Equal("", CultureInfo.CurrentCulture.Name); + Assert.Equal("", CultureInfo.CurrentUICulture.Name); + } }, new RemoteInvokeOptions { StartInfo = psi }).Dispose(); } diff --git a/src/mono/mono/mini/CMakeLists.txt b/src/mono/mono/mini/CMakeLists.txt index e09b23e4380eb..5921fb7371e7b 100644 --- a/src/mono/mono/mini/CMakeLists.txt +++ b/src/mono/mono/mini/CMakeLists.txt @@ -68,6 +68,13 @@ if(HAVE_SYS_ICU AND NOT HOST_WASI) pal_timeZoneInfo.c entrypoints.c ${pal_icushim_sources_base}) + + if (TARGET_DARWIN) + set(icu_shim_sources_base + ${icu_shim_sources_base} + pal_locale.m) + endif() + addprefix(icu_shim_sources "${ICU_SHIM_PATH}" "${icu_shim_sources_base}") set_source_files_properties(${icu_shim_sources} PROPERTIES COMPILE_DEFINITIONS OSX_ICU_LIBRARY_PATH="${OSX_ICU_LIBRARY_PATH}") set_source_files_properties(${icu_shim_sources} PROPERTIES COMPILE_FLAGS "-I\"${ICU_INCLUDEDIR}\" -I\"${CLR_SRC_NATIVE_DIR}/libs/System.Globalization.Native/\" -I\"${CLR_SRC_NATIVE_DIR}/libs/Common/\" ${ICU_FLAGS}") diff --git a/src/native/libs/System.Globalization.Native/CMakeLists.txt b/src/native/libs/System.Globalization.Native/CMakeLists.txt index d140506599692..54dad314caf88 100644 --- a/src/native/libs/System.Globalization.Native/CMakeLists.txt +++ b/src/native/libs/System.Globalization.Native/CMakeLists.txt @@ -60,6 +60,10 @@ set(NATIVEGLOBALIZATION_SOURCES pal_icushim.c ) +if (CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS) + set(NATIVEGLOBALIZATION_SOURCES ${NATIVEGLOBALIZATION_SOURCES} pal_locale.m) +endif() + # time zone names are filtered out of icu data for the browser and associated functionality is disabled if (NOT CLR_CMAKE_TARGET_BROWSER) set(NATIVEGLOBALIZATION_SOURCES ${NATIVEGLOBALIZATION_SOURCES} pal_timeZoneInfo.c) @@ -76,6 +80,11 @@ endif() include_directories("../Common") if (GEN_SHARED_LIB) + if (CLR_CMAKE_TARGET_OSX OR CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS) + include(CMakeFindFrameworks) + find_library(FOUNDATION Foundation REQUIRED) + endif() + add_library(System.Globalization.Native SHARED ${NATIVEGLOBALIZATION_SOURCES} @@ -84,12 +93,12 @@ if (GEN_SHARED_LIB) target_link_libraries(System.Globalization.Native dl + ${FOUNDATION} ) install_with_stripped_symbols (System.Globalization.Native PROGRAMS .) endif() - add_library(System.Globalization.Native-Static STATIC ${NATIVEGLOBALIZATION_SOURCES} diff --git a/src/native/libs/System.Globalization.Native/pal_locale.c b/src/native/libs/System.Globalization.Native/pal_locale.c index cd0e25ed8837f..498f713404ca0 100644 --- a/src/native/libs/System.Globalization.Native/pal_locale.c +++ b/src/native/libs/System.Globalization.Native/pal_locale.c @@ -129,12 +129,15 @@ int32_t FixupLocaleName(UChar* value, int32_t valueLength) return i; } -// We use whatever ICU give us as the default locale except if it is en_US_POSIX. We'll map -// this POSIX locale to Invariant instead. The reason is POSIX locale collation behavior -// is not desirable at all because it doesn't support case insensitive string comparisons. +// We use whatever ICU give us as the default locale except if it is en_US_POSIX. +// +// On Apple related platforms (OSX, iOS, tvOS, MacCatalyst), we'll take what the system locale is. +// On all other platforms we'll map this POSIX locale to Invariant instead. +// The reason is POSIX locale collation behavior is not desirable at all because it doesn't support case insensitive string comparisons. const char* DetectDefaultLocaleName() { const char* icuLocale = uloc_getDefault(); + if (strcmp(icuLocale, "en_US_POSIX") == 0) { return ""; @@ -216,6 +219,16 @@ int32_t GlobalizationNative_GetDefaultLocaleName(UChar* value, int32_t valueLeng const char* defaultLocale = DetectDefaultLocaleName(); +#ifdef __APPLE__ + char* appleLocale = NULL; + + if (strcmp(defaultLocale, "") == 0) + { + appleLocale = DetectDefaultAppleLocaleName(); + defaultLocale = appleLocale; + } +#endif + uloc_getBaseName(defaultLocale, localeNameBuffer, ULOC_FULLNAME_CAPACITY, &status); u_charsToUChars_safe(localeNameBuffer, value, valueLength, &status); @@ -236,6 +249,11 @@ int32_t GlobalizationNative_GetDefaultLocaleName(UChar* value, int32_t valueLeng } } +#ifdef __APPLE__ + if (appleLocale) + free(appleLocale); +#endif + return UErrorCodeToBool(status); } diff --git a/src/native/libs/System.Globalization.Native/pal_locale.m b/src/native/libs/System.Globalization.Native/pal_locale.m new file mode 100644 index 0000000000000..f574b9d14797e --- /dev/null +++ b/src/native/libs/System.Globalization.Native/pal_locale.m @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include +#include "pal_locale_internal.h" + +#import + +char* DetectDefaultAppleLocaleName() +{ + NSLocale *currentLocale = [NSLocale currentLocale]; + NSString *localeName = @""; + + 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_locale_internal.h b/src/native/libs/System.Globalization.Native/pal_locale_internal.h index c58436ab77517..c754554bbfdd5 100644 --- a/src/native/libs/System.Globalization.Native/pal_locale_internal.h +++ b/src/native/libs/System.Globalization.Native/pal_locale_internal.h @@ -51,3 +51,13 @@ Detect the default locale for the machine, defaulting to Invaraint if we can't compute one (different from uloc_getDefault()) would do. */ const char* DetectDefaultLocaleName(void); + +#ifdef __APPLE__ +/* +Function: +DetectDefaultSystemLocaleName + +Detects the default locale string for Apple platforms +*/ +char* DetectDefaultAppleLocaleName(void); +#endif