summaryrefslogtreecommitdiff
path: root/src/corefx/System.Globalization.Native/localeStringData.cpp
blob: bfe04697ea55216067d707bea9f81506e148748a (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full
// license information.
//

#include <assert.h>
#include <string.h>

#include "locale.hpp"

#include "unicode/dcfmtsym.h" //decimal symbols
#include "unicode/dtfmtsym.h" //date symbols
#include "unicode/smpdtfmt.h" //date format
#include "unicode/localpointer.h"

// invariant character definitions used by ICU
#define UCHAR_SPACE ((UChar)0x0020)   // space
#define UCHAR_NBSPACE ((UChar)0x00A0) // space

// Enum that corresponds to managed enum CultureData.LocaleStringData.
// The numeric values of the enum members match their Win32 counterparts.
enum LocaleStringData : int32_t
{
    LocalizedDisplayName = 0x00000002,
    EnglishDisplayName = 0x00000072,
    NativeDisplayName = 0x00000073,
    LocalizedLanguageName = 0x0000006f,
    EnglishLanguageName = 0x00001001,
    NativeLanguageName = 0x00000004,
    EnglishCountryName = 0x00001002,
    NativeCountryName = 0x00000008,
    ListSeparator = 0x0000000C,
    DecimalSeparator = 0x0000000E,
    ThousandSeparator = 0x0000000F,
    Digits = 0x00000013,
    MonetarySymbol = 0x00000014,
    Iso4217MonetarySymbol = 0x00000015,
    MonetaryDecimalSeparator = 0x00000016,
    MonetaryThousandSeparator = 0x00000017,
    AMDesignator = 0x00000028,
    PMDesignator = 0x00000029,
    PositiveSign = 0x00000050,
    NegativeSign = 0x00000051,
    Iso639LanguageName = 0x00000059,
    Iso3166CountryName = 0x0000005A,
    NaNSymbol = 0x00000069,
    PositiveInfinitySymbol = 0x0000006a,
    ParentName = 0x0000006d,
    PercentSymbol = 0x00000076,
    PerMilleSymbol = 0x00000077
};

/*
Function:
GetLocaleInfoDecimalFormatSymbol

Obtains the value of a DecimalFormatSymbols
*/
UErrorCode GetLocaleInfoDecimalFormatSymbol(const Locale& locale,
                                            DecimalFormatSymbols::ENumberFormatSymbol symbol,
                                            UChar* value,
                                            int32_t valueLength)
{
    UErrorCode status = U_ZERO_ERROR;
    LocalPointer<DecimalFormatSymbols> decimalsymbols(new DecimalFormatSymbols(locale, status));
    if (decimalsymbols == NULL)
    {
        status = U_MEMORY_ALLOCATION_ERROR;
    }

    if (U_FAILURE(status))
    {
        return status;
    }

    UnicodeString s = decimalsymbols->getSymbol(symbol);

    s.extract(value, valueLength, status);
    return status;
}

/*
Function:
GetDigitSymbol

Obtains the value of a Digit DecimalFormatSymbols
*/
UErrorCode GetDigitSymbol(const Locale& locale,
                          UErrorCode previousStatus,
                          DecimalFormatSymbols::ENumberFormatSymbol symbol,
                          int digit,
                          UChar* value,
                          int32_t valueLength)
{
    if (U_FAILURE(previousStatus))
    {
        return previousStatus;
    }

    return GetLocaleInfoDecimalFormatSymbol(locale, symbol, value + digit, valueLength - digit);
}

/*
Function:
GetLocaleInfoAmPm

Obtains the value of a DateFormatSymbols Am or Pm string
*/
UErrorCode GetLocaleInfoAmPm(const Locale& locale, bool am, UChar* value, int32_t valueLength)
{
    UErrorCode status = U_ZERO_ERROR;
    LocalPointer<DateFormatSymbols> dateFormatSymbols(new DateFormatSymbols(locale, status));
    if (dateFormatSymbols == NULL)
    {
        status = U_MEMORY_ALLOCATION_ERROR;
    }

    if (U_FAILURE(status))
    {
        return status;
    }

    int32_t count = 0;
    const UnicodeString* tempStr = dateFormatSymbols->getAmPmStrings(count);
    int offset = am ? 0 : 1;
    if (offset >= count)
    {
        return U_INTERNAL_PROGRAM_ERROR;
    }

    tempStr[offset].extract(value, valueLength, status);
    return status;
}

/*
PAL Function:
GetLocaleInfoString

Obtains string locale information.
Returns 1 for success, 0 otherwise
*/
extern "C" int32_t
GetLocaleInfoString(const UChar* localeName, LocaleStringData localeStringData, UChar* value, int32_t valueLength)
{
    Locale locale = GetLocale(localeName);
    if (locale.isBogus())
    {
        return UErrorCodeToBool(U_ILLEGAL_ARGUMENT_ERROR);
    }

    UnicodeString str;
    UErrorCode status = U_ZERO_ERROR;
    switch (localeStringData)
    {
        case LocalizedDisplayName:
            locale.getDisplayName(str);
            str.extract(value, valueLength, status);
            break;
        case EnglishDisplayName:
            locale.getDisplayName(Locale::getEnglish(), str);
            str.extract(value, valueLength, status);
            break;
        case NativeDisplayName:
            locale.getDisplayName(locale, str);
            str.extract(value, valueLength, status);
            break;
        case LocalizedLanguageName:
            locale.getDisplayLanguage(str);
            str.extract(value, valueLength, status);
            break;
        case EnglishLanguageName:
            locale.getDisplayLanguage(Locale::getEnglish(), str);
            str.extract(value, valueLength, status);
            break;
        case NativeLanguageName:
            locale.getDisplayLanguage(locale, str);
            str.extract(value, valueLength, status);
            break;
        case EnglishCountryName:
            locale.getDisplayCountry(Locale::getEnglish(), str);
            str.extract(value, valueLength, status);
            break;
        case NativeCountryName:
            locale.getDisplayCountry(locale, str);
            str.extract(value, valueLength, status);
            break;
        case ListSeparator:
        // fall through
        case ThousandSeparator:
            status = GetLocaleInfoDecimalFormatSymbol(
                locale, DecimalFormatSymbols::kGroupingSeparatorSymbol, value, valueLength);
            break;
        case DecimalSeparator:
            status = GetLocaleInfoDecimalFormatSymbol(
                locale, DecimalFormatSymbols::kDecimalSeparatorSymbol, value, valueLength);
            break;
        case Digits:
            status = GetDigitSymbol(locale, status, DecimalFormatSymbols::kZeroDigitSymbol, 0, value, valueLength);
            // symbols kOneDigitSymbol to kNineDigitSymbol are contiguous
            for (int32_t symbol = DecimalFormatSymbols::kOneDigitSymbol;
                 symbol <= DecimalFormatSymbols::kNineDigitSymbol;
                 symbol++)
            {
                int charIndex = symbol - DecimalFormatSymbols::kOneDigitSymbol + 1;
                status = GetDigitSymbol(
                    locale, status, (DecimalFormatSymbols::ENumberFormatSymbol)symbol, charIndex, value, valueLength);
            }
            break;
        case MonetarySymbol:
            status =
                GetLocaleInfoDecimalFormatSymbol(locale, DecimalFormatSymbols::kCurrencySymbol, value, valueLength);
            break;
        case Iso4217MonetarySymbol:
            status =
                GetLocaleInfoDecimalFormatSymbol(locale, DecimalFormatSymbols::kIntlCurrencySymbol, value, valueLength);
            break;
        case MonetaryDecimalSeparator:
            status = GetLocaleInfoDecimalFormatSymbol(
                locale, DecimalFormatSymbols::kMonetarySeparatorSymbol, value, valueLength);
            break;
        case MonetaryThousandSeparator:
            status = GetLocaleInfoDecimalFormatSymbol(
                locale, DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol, value, valueLength);
            break;
        case AMDesignator:
            status = GetLocaleInfoAmPm(locale, true, value, valueLength);
            break;
        case PMDesignator:
            status = GetLocaleInfoAmPm(locale, false, value, valueLength);
            break;
        case PositiveSign:
            status =
                GetLocaleInfoDecimalFormatSymbol(locale, DecimalFormatSymbols::kPlusSignSymbol, value, valueLength);
            break;
        case NegativeSign:
            status =
                GetLocaleInfoDecimalFormatSymbol(locale, DecimalFormatSymbols::kMinusSignSymbol, value, valueLength);
            break;
        case Iso639LanguageName:
            status = u_charsToUChars_safe(locale.getLanguage(), value, valueLength);
            break;
        case Iso3166CountryName:
            // coreclr expects 2-character version, not 3 (3 would correspond to
            // LOCALE_SISO3166CTRYNAME2 and locale.getISO3Country)
            status = u_charsToUChars_safe(locale.getCountry(), value, valueLength);
            break;
        case NaNSymbol:
            status = GetLocaleInfoDecimalFormatSymbol(locale, DecimalFormatSymbols::kNaNSymbol, value, valueLength);
            break;
        case PositiveInfinitySymbol:
            status =
                GetLocaleInfoDecimalFormatSymbol(locale, DecimalFormatSymbols::kInfinitySymbol, value, valueLength);
            break;
        case ParentName:
        {
            // ICU supports lang[-script][-region][-variant] so up to 4 parents
            // including invariant locale
            char localeNameTemp[ULOC_FULLNAME_CAPACITY];

            uloc_getParent(locale.getName(), localeNameTemp, ULOC_FULLNAME_CAPACITY, &status);
            if (U_SUCCESS(status))
            {
                status = u_charsToUChars_safe(localeNameTemp, value, valueLength);
                if (U_SUCCESS(status))
                {
                    FixupLocaleName(value, valueLength);
                }
            }
            break;
        }
        case PercentSymbol:
            status = GetLocaleInfoDecimalFormatSymbol(locale, DecimalFormatSymbols::kPercentSymbol, value, valueLength);
            break;
        case PerMilleSymbol:
            status = GetLocaleInfoDecimalFormatSymbol(locale, DecimalFormatSymbols::kPerMillSymbol, value, valueLength);
            break;
        default:
            status = U_UNSUPPORTED_ERROR;
            break;
    };

    return UErrorCodeToBool(status);
}

/*
Function:
NormalizeTimePattern

Convert an ICU non-localized time pattern to .NET format
*/
void NormalizeTimePattern(const UnicodeString* srcPattern, UnicodeString* destPattern)
{
    // An srcPattern example: "h:mm:ss a"
    // A destPattern example: "h:mm:ss tt"
    destPattern->remove();

    bool amPmAdded = false;
    for (int i = 0; i <= srcPattern->length() - 1; i++)
    {
        UChar ch = srcPattern->charAt(i);
        switch (ch)
        {
            case ':':
            case '.':
            case 'H':
            case 'h':
            case 'm':
            case 's':
                destPattern->append(ch);
                break;

            case UCHAR_SPACE:
            case UCHAR_NBSPACE:
                destPattern->append(UCHAR_SPACE);
                break;

            case 'a': // AM/PM
                if (!amPmAdded)
                {
                    amPmAdded = true;
                    destPattern->append("tt");
                }
                break;
        }
    }
}

/*
PAL Function:
GetLocaleTimeFormat

Obtains time format information.
Returns 1 for success, 0 otherwise
*/
extern "C" int32_t GetLocaleTimeFormat(const UChar* localeName, int shortFormat, UChar* value, int32_t valueLength)
{
    Locale locale = GetLocale(localeName);
    if (locale.isBogus())
    {
        return UErrorCodeToBool(U_ILLEGAL_ARGUMENT_ERROR);
    }

    DateFormat::EStyle style = (shortFormat != 0) ? DateFormat::kShort : DateFormat::kMedium;
    LocalPointer<DateFormat> dateFormat(DateFormat::createTimeInstance(style, locale));
    if (dateFormat == NULL || !dateFormat.isValid())
    {
        return UErrorCodeToBool(U_MEMORY_ALLOCATION_ERROR);
    }

    // cast to SimpleDateFormat so we can call toPattern()
    SimpleDateFormat* sdf = dynamic_cast<SimpleDateFormat*>(dateFormat.getAlias());
    if (sdf == NULL)
    {
        return UErrorCodeToBool(U_INTERNAL_PROGRAM_ERROR);
    }

    UnicodeString icuPattern;
    sdf->toPattern(icuPattern);

    UnicodeString dotnetPattern;
    NormalizeTimePattern(&icuPattern, &dotnetPattern);

    UErrorCode status = U_ZERO_ERROR;
    dotnetPattern.extract(value, valueLength, status);

    return UErrorCodeToBool(status);
}