-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
pal_locale.m
162 lines (149 loc) · 7.19 KB
/
pal_locale.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include <stdlib.h>
#include "pal_locale_internal.h"
#include "pal_localeStringData.h"
#import <Foundation/Foundation.h>
#import <Foundation/NSFormatter.h>
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]);
}
#if defined(TARGET_OSX) || defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)
const char* GlobalizationNative_GetLocaleNameNative(const char* localeName)
{
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
const char* value = [currentLocale.localeIdentifier UTF8String];
return strdup(value);
}
const char* GlobalizationNative_GetLocaleInfoStringNative(const char* localeName, LocaleStringData localeStringData)
{
const char* value;
NSString *locName = [NSString stringWithFormat:@"%s", localeName];
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.locale = currentLocale;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:currentLocale];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
switch (localeStringData)
{
///// <summary>localized name of locale, eg "German (Germany)" in UI language (corresponds to LOCALE_SLOCALIZEDDISPLAYNAME)</summary>
case LocaleString_LocalizedDisplayName:
/// <summary>Display name (language + country usually) in English, eg "German (Germany)" (corresponds to LOCALE_SENGLISHDISPLAYNAME)</summary>
case LocaleString_EnglishDisplayName:
value = [[gbLocale displayNameForKey:NSLocaleIdentifier value:currentLocale.localeIdentifier] UTF8String];
break;
/// <summary>Display name in native locale language, eg "Deutsch (Deutschland) (corresponds to LOCALE_SNATIVEDISPLAYNAME)</summary>
case LocaleString_NativeDisplayName:
value = [[currentLocale displayNameForKey:NSLocaleIdentifier value:currentLocale.localeIdentifier] UTF8String];
break;
/// <summary>Language Display Name for a language, eg "German" in UI language (corresponds to LOCALE_SLOCALIZEDLANGUAGENAME)</summary>
case LocaleString_LocalizedLanguageName:
/// <summary>English name of language, eg "German" (corresponds to LOCALE_SENGLISHLANGUAGENAME)</summary>
case LocaleString_EnglishLanguageName:
value = [[gbLocale localizedStringForLanguageCode:currentLocale.languageCode] UTF8String];
break;
/// <summary>native name of language, eg "Deutsch" (corresponds to LOCALE_SNATIVELANGUAGENAME)</summary>
case LocaleString_NativeLanguageName:
value = [[currentLocale localizedStringForLanguageCode:currentLocale.languageCode] UTF8String];
break;
/// <summary>English name of country, eg "Germany" (corresponds to LOCALE_SENGLISHCOUNTRYNAME)</summary>
case LocaleString_EnglishCountryName:
value = [[gbLocale localizedStringForCountryCode:currentLocale.countryCode] UTF8String];
break;
/// <summary>native name of country, eg "Deutschland" (corresponds to LOCALE_SNATIVECOUNTRYNAME)</summary>
case LocaleString_NativeCountryName:
value = [[currentLocale localizedStringForCountryCode:currentLocale.countryCode] UTF8String];
break;
case LocaleString_ThousandSeparator:
value = [currentLocale.groupingSeparator UTF8String];
break;
case LocaleString_DecimalSeparator:
value = [currentLocale.decimalSeparator UTF8String];
// or value = [[currentLocale objectForKey:NSLocaleDecimalSeparator] UTF8String];
break;
case LocaleString_MonetarySymbol:
value = [currentLocale.currencySymbol UTF8String];
break;
case LocaleString_Iso4217MonetarySymbol:
// check if this is correct, check currencyISOCode
value = [currentLocale.currencySymbol UTF8String];
break;
case LocaleString_CurrencyEnglishName:
value = [[gbLocale localizedStringForCurrencyCode:currentLocale.currencyCode] UTF8String];
break;
case LocaleString_CurrencyNativeName:
value = [[currentLocale localizedStringForCurrencyCode:currentLocale.currencyCode] UTF8String];
break;
case LocaleString_AMDesignator:
value = [dateFormatter.AMSymbol UTF8String];
break;
case LocaleString_PMDesignator:
value = [dateFormatter.PMSymbol UTF8String];
break;
case LocaleString_PositiveSign:
value = [numberFormatter.plusSign UTF8String];
break;
case LocaleString_NegativeSign:
value = [numberFormatter.minusSign UTF8String];
break;
case LocaleString_Iso639LanguageTwoLetterName:
// check if this is correct
value = [[currentLocale objectForKey:NSLocaleLanguageCode] UTF8String];
break;
case LocaleString_Iso3166CountryName:
value = [currentLocale.countryCode UTF8String];
break;
case LocaleString_NaNSymbol:
value = [numberFormatter.notANumberSymbol UTF8String];
break;
case LocaleString_PositiveInfinitySymbol:
value = [numberFormatter.positiveInfinitySymbol UTF8String];
break;
case LocaleString_NegativeInfinitySymbol:
value = [numberFormatter.negativeInfinitySymbol UTF8String];
break;
case LocaleString_PercentSymbol:
value = [numberFormatter.percentSymbol UTF8String];
break;
case LocaleString_PerMilleSymbol:
value = [numberFormatter.perMillSymbol UTF8String];
break;
// TODO find mapping for below cases
// https://github.com/dotnet/runtime/issues/83514
case LocaleString_Digits:
case LocaleString_MonetaryDecimalSeparator:
case LocaleString_MonetaryThousandSeparator:
case LocaleString_Iso639LanguageThreeLetterName:
case LocaleString_ParentName:
case LocaleString_Iso3166CountryName2:
default:
value = "";
break;
}
return strdup(value);
}
#endif
#if defined(TARGET_MACCATALYST) || defined(TARGET_IOS) || defined(TARGET_TVOS)
const char* GlobalizationNative_GetICUDataPathFallback(void)
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"icudt" ofType:@"dat"];
return strdup([bundlePath UTF8String]);
}
#endif