From f450f492ca266e9c5c78266dcd4781ea8907cf48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Tue, 20 Jun 2023 17:48:48 +0200 Subject: [PATCH] Support relative path for ICU_DAT_FILE_PATH on iOS/tvOS/Catalyst (#87813) We consider it relative to the app bundle root. Required for https://github.com/xamarin/xamarin-macios/issues/17877 and NativeAOT scenarios. --- .../pal_icushim_internal.h | 1 + .../pal_icushim_static.c | 7 ++++++- .../libs/System.Globalization.Native/pal_locale.m | 14 +++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/native/libs/System.Globalization.Native/pal_icushim_internal.h b/src/native/libs/System.Globalization.Native/pal_icushim_internal.h index 9c981541277e6..9b5fd1a6c0be9 100644 --- a/src/native/libs/System.Globalization.Native/pal_icushim_internal.h +++ b/src/native/libs/System.Globalization.Native/pal_icushim_internal.h @@ -341,6 +341,7 @@ FOR_ALL_ICU_FUNCTIONS #define ucal_getTimeZoneIDForWindowsID_ptr ucal_getTimeZoneIDForWindowsID #if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS) +const char* GlobalizationNative_GetICUDataPathRelativeToAppBundleRoot(const char* path); const char* GlobalizationNative_GetICUDataPathFallback(void); #endif diff --git a/src/native/libs/System.Globalization.Native/pal_icushim_static.c b/src/native/libs/System.Globalization.Native/pal_icushim_static.c index 6d2f5ce9bdeb9..ebf271429573e 100644 --- a/src/native/libs/System.Globalization.Native/pal_icushim_static.c +++ b/src/native/libs/System.Globalization.Native/pal_icushim_static.c @@ -171,9 +171,14 @@ int32_t GlobalizationNative_LoadICUData(const char* path) { #if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS) + if (path && path[0] != '/') + { + // if the path is relative, prepend the app bundle root + path = GlobalizationNative_GetICUDataPathRelativeToAppBundleRoot(path); + } if (!path) { - // fallback to icudt.dat in the app bundle root in case the path isn't set + // fallback to icudt.dat in the app bundle resources in case the path isn't set path = GlobalizationNative_GetICUDataPathFallback(); } #endif diff --git a/src/native/libs/System.Globalization.Native/pal_locale.m b/src/native/libs/System.Globalization.Native/pal_locale.m index 66b951db76125..c26e89b601829 100644 --- a/src/native/libs/System.Globalization.Native/pal_locale.m +++ b/src/native/libs/System.Globalization.Native/pal_locale.m @@ -9,7 +9,7 @@ #import #import -char* DetectDefaultAppleLocaleName() +char* DetectDefaultAppleLocaleName(void) { NSLocale *currentLocale = [NSLocale currentLocale]; NSString *localeName = @""; @@ -571,9 +571,17 @@ Returns time format information (in native format, it needs to be converted to . #endif #if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS) +const char* GlobalizationNative_GetICUDataPathRelativeToAppBundleRoot(const char* path) +{ + NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; + NSString *dataPath = [bundlePath stringByAppendingPathComponent: [NSString stringWithFormat:@"%s", path]]; + + return strdup([dataPath UTF8String]); +} + const char* GlobalizationNative_GetICUDataPathFallback(void) { - NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"icudt" ofType:@"dat"]; - return strdup([bundlePath UTF8String]); + NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"icudt" ofType:@"dat"]; + return strdup([dataPath UTF8String]); } #endif