/
micuconversions.cpp
410 lines (363 loc) · 12.1 KB
/
micuconversions.cpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/***************************************************************************
**
** Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "micuconversions.h"
#include <QDebug>
#include <QString>
#include <unicode/unistr.h>
#include <unicode/datefmt.h>
#include "mlocale_p.h"
namespace ML10N {
icu::UnicodeString MIcuConversions::qStringToUnicodeString(const QString &sourceStr)
{
return icu::UnicodeString(sourceStr.utf16(), sourceStr.length());
}
QString MIcuConversions::unicodeStringToQString(const icu::UnicodeString &sourceStr)
{
return QString(reinterpret_cast<const QChar *>(sourceStr.getBuffer()),
sourceStr.length());
}
icu::DateFormat::EStyle MIcuConversions::toEStyle(MLocale::DateType dateType)
{
if (dateType == MLocale::DateNone) {
return icu::DateFormat::kNone;
}
if (dateType == MLocale::DateShort) {
return icu::DateFormat::kShort;
}
if (dateType == MLocale::DateMedium) {
return icu::DateFormat::kMedium;
}
if (dateType == MLocale::DateLong) {
return icu::DateFormat::kLong;
}
return icu::DateFormat::kFull;
}
icu::DateFormat::EStyle MIcuConversions::toEStyle(MLocale::TimeType timeType)
{
if (timeType == MLocale::TimeNone) {
return icu::DateFormat::kNone;
}
if (timeType == MLocale::TimeShort) {
return icu::DateFormat::kShort;
}
if (timeType == MLocale::TimeMedium) {
return icu::DateFormat::kMedium;
}
if (timeType == MLocale::TimeLong) {
return icu::DateFormat::kLong;
}
return icu::DateFormat::kFull;
}
QString MIcuConversions::layoutDirectionToString(Qt::LayoutDirection layoutDirection)
{
QString layoutDirectionName;
switch(layoutDirection) {
case Qt::LeftToRight:
layoutDirectionName = "ltr";
break;
case Qt::RightToLeft:
layoutDirectionName = "rtl";
break;
case Qt::LayoutDirectionAuto:
default:
layoutDirectionName = "auto";
break;
}
return layoutDirectionName;
}
Qt::LayoutDirection MIcuConversions::stringToLayoutDirection(QString layoutDirectionName)
{
if(layoutDirectionName == "rtl")
// force layout direction to RTL
return Qt::RightToLeft;
else if(layoutDirectionName == "ltr")
// force layout direction to LTR
return Qt::LeftToRight;
else if(layoutDirectionName == "auto")
// determine layout direction from the locale
return Qt::LayoutDirectionAuto;
else
// no option means force layout direction to LTR:
return Qt::LeftToRight;
}
QString MIcuConversions::collationToString(MLocale::Collation coll)
{
QString collationName;
switch(coll) {
case MLocale::PhonebookCollation:
collationName = "phonebook";
break;
case MLocale::PinyinCollation:
collationName = "pinyin";
break;
case MLocale::TraditionalCollation:
collationName = "traditional";
break;
case MLocale::StrokeCollation:
collationName = "stroke";
break;
case MLocale::DirectCollation:
collationName = "direct";
break;
case MLocale::PosixCollation:
collationName = "posix";
break;
case MLocale::Big5hanCollation:
collationName = "big5han";
break;
case MLocale::Gb2312hanCollation:
collationName = "gb2312han";
break;
case MLocale::DefaultCollation:
default:
collationName = "";
break;
}
return collationName;
}
MLocale::Collation MIcuConversions::stringToCollation(QString collationName)
{
if(collationName == "phonebook")
return MLocale::PhonebookCollation;
else if (collationName == "pinyin")
return MLocale::PinyinCollation;
else if (collationName == "traditional")
return MLocale::TraditionalCollation;
else if (collationName == "stroke")
return MLocale::StrokeCollation;
else if (collationName == "direct")
return MLocale::DirectCollation;
else if (collationName == "posix")
return MLocale::PosixCollation;
else if (collationName == "big5han")
return MLocale::Big5hanCollation;
else if (collationName == "gb2312han")
return MLocale::Gb2312hanCollation;
else
return MLocale::DefaultCollation;
}
QString MIcuConversions::calendarToString(MLocale::CalendarType calendarType)
{
QString calendarTypeName;
switch(calendarType) {
case MLocale::GregorianCalendar:
calendarTypeName = "gregorian";
break;
case MLocale::IslamicCalendar:
calendarTypeName = "islamic";
break;
case MLocale::ChineseCalendar:
calendarTypeName = "chinese";
break;
case MLocale::IslamicCivilCalendar:
calendarTypeName = "islamic-civil";
break;
case MLocale::HebrewCalendar:
calendarTypeName = "hebrew";
break;
case MLocale::JapaneseCalendar:
calendarTypeName = "japanese";
break;
case MLocale::BuddhistCalendar:
calendarTypeName = "buddhist";
break;
case MLocale::PersianCalendar:
calendarTypeName = "persian";
break;
case MLocale::CopticCalendar:
calendarTypeName = "coptic";
break;
case MLocale::EthiopicCalendar:
calendarTypeName = "ethiopic";
break;
case MLocale::DefaultCalendar:
default:
calendarTypeName = "";
break;
}
return calendarTypeName;
}
MLocale::CalendarType MIcuConversions::stringToCalendar(QString calendarTypeName)
{
if(calendarTypeName == "gregorian")
return MLocale::GregorianCalendar;
else if (calendarTypeName == "islamic")
return MLocale::IslamicCalendar;
else if (calendarTypeName == "chinese")
return MLocale::ChineseCalendar;
else if (calendarTypeName == "islamic-civil")
return MLocale::IslamicCivilCalendar;
else if (calendarTypeName == "hebrew")
return MLocale::HebrewCalendar;
else if (calendarTypeName == "japanese")
return MLocale::JapaneseCalendar;
else if (calendarTypeName == "buddhist")
return MLocale::BuddhistCalendar;
else if (calendarTypeName == "persian")
return MLocale::PersianCalendar;
else if (calendarTypeName == "coptic")
return MLocale::CopticCalendar;
else if (calendarTypeName == "ethiopic")
return MLocale::EthiopicCalendar;
else
return MLocale::DefaultCalendar;
}
icu::DateFormatSymbols::DtContextType
MIcuConversions::mDateContextToIcu(MLocale::DateSymbolContext context)
{
icu::DateFormatSymbols::DtContextType icuContext;
if (context == MLocale::DateSymbolFormat) {
icuContext = icu::DateFormatSymbols::FORMAT;
} else {
icuContext = icu::DateFormatSymbols::STANDALONE;
}
return icuContext;
}
icu::DateFormatSymbols::DtWidthType
MIcuConversions::mDateWidthToIcu(MLocale::DateSymbolLength length)
{
icu::DateFormatSymbols::DtWidthType icuWidth;
switch (length) {
case MLocale::DateSymbolAbbreviated:
icuWidth = icu::DateFormatSymbols::ABBREVIATED;
break;
case MLocale::DateSymbolWide:
icuWidth = icu::DateFormatSymbols::WIDE;
break;
case MLocale::DateSymbolNarrow:
default:
icuWidth = icu::DateFormatSymbols::NARROW;
break;
}
return icuWidth;
}
UCalendarDaysOfWeek MIcuConversions::icuWeekday(int mWeekday)
{
int weekdayNum = mWeekday;
// sunday = 1, monday = 2 etc on the array
if (mWeekday == MLocale::Sunday) {
weekdayNum = 1;
} else {
weekdayNum++;
}
return static_cast<UCalendarDaysOfWeek>(weekdayNum);
}
int MIcuConversions::mWeekday(int icuWeekday)
{
if (icuWeekday == UCAL_SUNDAY) {
return MLocale::Sunday; // 1 -> 7
} else {
return icuWeekday - 1;
}
}
MLocale::WeekdayType MIcuConversions::mWeekdayType(UCalendarWeekdayType icuWeekdayType)
{
if (icuWeekdayType == UCAL_WEEKDAY)
return MLocale::WeekdayTypeWeekday;
else if (icuWeekdayType == UCAL_WEEKEND)
return MLocale::WeekdayTypeWeekend;
else if (icuWeekdayType == UCAL_WEEKEND_ONSET)
return MLocale::WeekdayTypeWeekendOnset;
else if (icuWeekdayType == UCAL_WEEKEND_CEASE)
return MLocale::WeekdayTypeWeekendCease;
else
return MLocale::WeekdayTypeWeekday;
}
QString MIcuConversions::icuDatePatternEscaped(const QString &str)
{
QString result = str;
return result.replace('\'', "''");
}
QString MIcuConversions::parseOption(const QString &localeName, const QString &option)
{
QString value;
QRegExp regexp("^[^@]+@.*"+QRegExp::escape(option)+"=([^@=;]+)($|;.*$)");
if(regexp.indexIn(localeName) >= 0 && regexp.capturedTexts().size() == 3) {
value = regexp.capturedTexts().at(1);
}
return value;
}
QString MIcuConversions::setOption(const QString &localeName, const QString &option, const QString &value)
{
QString newLocaleName = localeName;
if(!newLocaleName.isEmpty() && !option.isEmpty()) {
if(value.isEmpty()) { // remove option completely
if(newLocaleName.contains('@') && newLocaleName.contains(option)) {
QRegExp regexp("^([^@]+@.*)"+QRegExp::escape(option)+"=[^@=;]+($|;.*$)");
newLocaleName.replace(regexp, "\\1\\2");
newLocaleName.replace(QLatin1String(";;"), QLatin1String(";"));
newLocaleName.replace(QLatin1String("@;"), QLatin1String("@"));
while(newLocaleName.endsWith(';'))
newLocaleName.chop(1);
if(newLocaleName.endsWith('@'))
newLocaleName.chop(1);
}
}
else { // replace option value
if(!newLocaleName.contains('@')) {
newLocaleName += '@' + option + '=' + value;
}
else if(!newLocaleName.contains(option)) {
if(newLocaleName.endsWith(';'))
newLocaleName += option + '=' + value;
else
newLocaleName += ';' + option + '=' + value;
}
else {
QRegExp regexp("^([^@]+@.*"+QRegExp::escape(option)+"=)[^@=;]+($|;.*$)");
newLocaleName.replace(regexp, "\\1"+value+"\\2");
}
}
}
return newLocaleName;
}
Qt::LayoutDirection MIcuConversions::parseLayoutDirectionOption(const QString &localeName)
{
return MIcuConversions::stringToLayoutDirection(
MIcuConversions::parseOption(localeName, "layout-direction"));
}
QString MIcuConversions::setLayoutDirectionOption(const QString &localeName, Qt::LayoutDirection layoutDirection)
{
return MIcuConversions::setOption(
localeName, "layout-direction",
MIcuConversions::layoutDirectionToString(layoutDirection));
}
MLocale::CalendarType MIcuConversions::parseCalendarOption(const QString &localeName)
{
return MIcuConversions::stringToCalendar(
MIcuConversions::parseOption(localeName, "calendar"));
}
QString MIcuConversions::setCalendarOption(const QString &localeName, MLocale::CalendarType calendarType)
{
return MIcuConversions::setOption(
localeName, "calendar",
MIcuConversions::calendarToString(calendarType));
}
MLocale::Collation MIcuConversions::parseCollationOption(const QString &localeName)
{
return MIcuConversions::stringToCollation(
MIcuConversions::parseOption(localeName, "collation"));
}
QString MIcuConversions::setCollationOption(const QString &localeName, MLocale::Collation collation)
{
return MIcuConversions::setOption(
localeName, "collation",
MIcuConversions::collationToString(collation));
}
}