1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qqmllocale_p.h" |
5 | #include <private/qqmlcontext_p.h> |
6 | #include <QtCore/qnumeric.h> |
7 | #include <QtCore/qdatetime.h> |
8 | #include <QtCore/qtimezone.h> |
9 | |
10 | #include <private/qlocale_p.h> |
11 | #include <private/qlocale_data_p.h> |
12 | |
13 | #include <private/qv4dateobject_p.h> |
14 | #include <private/qv4numberobject_p.h> |
15 | #include <private/qv4stringobject_p.h> |
16 | |
17 | QT_BEGIN_NAMESPACE |
18 | |
19 | using namespace QV4; |
20 | |
21 | DEFINE_OBJECT_VTABLE(QQmlLocaleData); |
22 | |
23 | #define THROW_ERROR(string) \ |
24 | do { \ |
25 | return scope.engine->throwError(QString::fromUtf8(string)); \ |
26 | } while (false) |
27 | |
28 | |
29 | #define GET_LOCALE_DATA_RESOURCE(OBJECT) \ |
30 | QV4::Scoped<QQmlLocaleData> r(scope, OBJECT.as<QQmlLocaleData>()); \ |
31 | if (!r) \ |
32 | THROW_ERROR("Not a valid Locale object") |
33 | |
34 | static bool isLocaleObject(const QV4::Value &val) |
35 | { |
36 | return val.as<QQmlLocaleData>(); |
37 | } |
38 | |
39 | //-------------- |
40 | // Date extension |
41 | |
42 | void QQmlDateExtension::registerExtension(QV4::ExecutionEngine *engine) |
43 | { |
44 | engine->datePrototype()->defineDefaultProperty(name: engine->id_toLocaleString(), code: method_toLocaleString); |
45 | engine->datePrototype()->defineDefaultProperty(QStringLiteral("toLocaleTimeString" ), code: method_toLocaleTimeString); |
46 | engine->datePrototype()->defineDefaultProperty(QStringLiteral("toLocaleDateString" ), code: method_toLocaleDateString); |
47 | engine->dateCtor()->defineDefaultProperty(QStringLiteral("fromLocaleString" ), code: method_fromLocaleString); |
48 | engine->dateCtor()->defineDefaultProperty(QStringLiteral("fromLocaleTimeString" ), code: method_fromLocaleTimeString); |
49 | engine->dateCtor()->defineDefaultProperty(QStringLiteral("fromLocaleDateString" ), code: method_fromLocaleDateString); |
50 | engine->dateCtor()->defineDefaultProperty(QStringLiteral("timeZoneUpdated" ), code: method_timeZoneUpdated); |
51 | } |
52 | |
53 | ReturnedValue QQmlDateExtension::method_toLocaleString(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
54 | { |
55 | Scope scope(b); |
56 | if (argc > 2) |
57 | return QV4::DatePrototype::method_toLocaleString(b, thisObject, argv, argc); |
58 | |
59 | const QV4::DateObject *date = thisObject->as<DateObject>(); |
60 | if (!date) |
61 | return QV4::DatePrototype::method_toLocaleString(b, thisObject, argv, argc); |
62 | |
63 | QDateTime dt = date->toQDateTime(); |
64 | |
65 | if (argc == 0) { |
66 | // Use QLocale for standard toLocaleString() function |
67 | QLocale locale; |
68 | RETURN_RESULT(scope.engine->newString(locale.toString(dt))); |
69 | } |
70 | |
71 | if (!isLocaleObject(val: argv[0])) |
72 | return QV4::DatePrototype::method_toLocaleString(b, thisObject, argv, argc); // Use the default Date toLocaleString() |
73 | |
74 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
75 | |
76 | QLocale::FormatType enumFormat = QLocale::LongFormat; |
77 | QString formattedDt; |
78 | if (argc == 2) { |
79 | if (String *s = argv[1].stringValue()) { |
80 | QString format = s->toQString(); |
81 | formattedDt = r->d()->locale->toString(dateTime: dt, format); |
82 | } else if (argv[1].isNumber()) { |
83 | quint32 intFormat = argv[1].toNumber(); |
84 | QLocale::FormatType format = QLocale::FormatType(intFormat); |
85 | formattedDt = r->d()->locale->toString(dateTime: dt, format); |
86 | } else { |
87 | THROW_ERROR("Locale: Date.toLocaleString(): Invalid datetime format" ); |
88 | } |
89 | } else { |
90 | formattedDt = r->d()->locale->toString(dateTime: dt, format: enumFormat); |
91 | } |
92 | |
93 | RETURN_RESULT(scope.engine->newString(formattedDt)); |
94 | } |
95 | |
96 | ReturnedValue QQmlDateExtension::method_toLocaleTimeString(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
97 | { |
98 | Scope scope(b); |
99 | if (argc > 2) |
100 | return QV4::DatePrototype::method_toLocaleTimeString(b, thisObject, argv, argc); |
101 | |
102 | const QV4::DateObject *date = thisObject->as<DateObject>(); |
103 | if (!date) |
104 | return QV4::DatePrototype::method_toLocaleTimeString(b, thisObject, argv, argc); |
105 | |
106 | QDateTime dt = date->toQDateTime(); |
107 | QTime time = dt.time(); |
108 | |
109 | if (argc == 0) { |
110 | // Use QLocale for standard toLocaleString() function |
111 | QLocale locale; |
112 | RETURN_RESULT(scope.engine->newString(locale.toString(time))); |
113 | } |
114 | |
115 | if (!isLocaleObject(val: argv[0])) |
116 | return QV4::DatePrototype::method_toLocaleTimeString(b, thisObject, argv, argc); // Use the default Date toLocaleTimeString() |
117 | |
118 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
119 | |
120 | QLocale::FormatType enumFormat = QLocale::LongFormat; |
121 | QString formattedTime; |
122 | if (argc == 2) { |
123 | if (String *s = argv[1].stringValue()) { |
124 | QString format = s->toQString(); |
125 | formattedTime = r->d()->locale->toString(time, format); |
126 | } else if (argv[1].isNumber()) { |
127 | quint32 intFormat = argv[1].toNumber(); |
128 | QLocale::FormatType format = QLocale::FormatType(intFormat); |
129 | formattedTime = r->d()->locale->toString(time, format); |
130 | } else { |
131 | THROW_ERROR("Locale: Date.toLocaleTimeString(): Invalid time format" ); |
132 | } |
133 | } else { |
134 | formattedTime = r->d()->locale->toString(time, format: enumFormat); |
135 | } |
136 | |
137 | RETURN_RESULT(scope.engine->newString(formattedTime)); |
138 | } |
139 | |
140 | ReturnedValue QQmlDateExtension::method_toLocaleDateString(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
141 | { |
142 | Scope scope(b); |
143 | if (argc > 2) |
144 | return QV4::DatePrototype::method_toLocaleDateString(b, thisObject, argv, argc); |
145 | |
146 | const QV4::DateObject *dateObj = thisObject->as<DateObject>(); |
147 | if (!dateObj) |
148 | return QV4::DatePrototype::method_toLocaleDateString(b, thisObject, argv, argc); |
149 | |
150 | QDateTime dt = dateObj->toQDateTime(); |
151 | QDate date = dt.date(); |
152 | |
153 | if (argc == 0) { |
154 | // Use QLocale for standard toLocaleString() function |
155 | QLocale locale; |
156 | RETURN_RESULT(scope.engine->newString(locale.toString(date))); |
157 | } |
158 | |
159 | if (!isLocaleObject(val: argv[0])) |
160 | return QV4::DatePrototype::method_toLocaleDateString(b, thisObject, argv, argc); // Use the default Date toLocaleDateString() |
161 | |
162 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
163 | |
164 | QLocale::FormatType enumFormat = QLocale::LongFormat; |
165 | QString formattedDate; |
166 | if (argc == 2) { |
167 | if (String *s = argv[1].stringValue()) { |
168 | QString format = s->toQString(); |
169 | formattedDate = r->d()->locale->toString(date, format); |
170 | } else if (argv[1].isNumber()) { |
171 | quint32 intFormat = argv[1].toNumber(); |
172 | QLocale::FormatType format = QLocale::FormatType(intFormat); |
173 | formattedDate = r->d()->locale->toString(date, format); |
174 | } else { |
175 | THROW_ERROR("Locale: Date.loLocaleDateString(): Invalid date format" ); |
176 | } |
177 | } else { |
178 | formattedDate = r->d()->locale->toString(date, format: enumFormat); |
179 | } |
180 | |
181 | RETURN_RESULT(scope.engine->newString(formattedDate)); |
182 | } |
183 | |
184 | ReturnedValue QQmlDateExtension::method_fromLocaleString(const QV4::FunctionObject *b, const QV4::Value *, const QV4::Value *argv, int argc) |
185 | { |
186 | QV4::Scope scope(b); |
187 | QV4::ExecutionEngine * const engine = scope.engine; |
188 | if (argc == 1) { |
189 | if (String *s = argv[0].stringValue()) { |
190 | QLocale locale; |
191 | QString dateString = s->toQString(); |
192 | QDateTime dt = locale.toDateTime(string: dateString); |
193 | RETURN_RESULT(engine->newDateObject(dt)); |
194 | } |
195 | } |
196 | |
197 | if (argc < 1 || argc > 3 || !isLocaleObject(val: argv[0])) |
198 | THROW_ERROR("Locale: Date.fromLocaleString(): Invalid arguments" ); |
199 | |
200 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
201 | |
202 | QLocale::FormatType enumFormat = QLocale::LongFormat; |
203 | QDateTime dt; |
204 | QString dateString = argv[1].toQStringNoThrow(); |
205 | if (argc == 3) { |
206 | if (String *s = argv[2].stringValue()) { |
207 | QString format = s->toQString(); |
208 | dt = r->d()->locale->toDateTime(string: dateString, format); |
209 | } else if (argv[2].isNumber()) { |
210 | quint32 intFormat = argv[2].toNumber(); |
211 | QLocale::FormatType format = QLocale::FormatType(intFormat); |
212 | dt = r->d()->locale->toDateTime(string: dateString, format); |
213 | } else { |
214 | THROW_ERROR("Locale: Date.fromLocaleString(): Invalid datetime format" ); |
215 | } |
216 | } else { |
217 | dt = r->d()->locale->toDateTime(string: dateString, format: enumFormat); |
218 | } |
219 | |
220 | RETURN_RESULT(engine->newDateObject(dt)); |
221 | } |
222 | |
223 | ReturnedValue QQmlDateExtension::method_fromLocaleTimeString(const QV4::FunctionObject *b, const QV4::Value *, const QV4::Value *argv, int argc) |
224 | { |
225 | QV4::Scope scope(b); |
226 | QV4::ExecutionEngine * const engine = scope.engine; |
227 | |
228 | if (argc == 1) { |
229 | if (String *s = argv[0].stringValue()) { |
230 | QLocale locale; |
231 | QString timeString = s->toQString(); |
232 | QTime time = locale.toTime(string: timeString); |
233 | QDateTime dt = QDateTime::currentDateTime(); |
234 | dt.setTime(time); |
235 | RETURN_RESULT(engine->newDateObject(dt)); |
236 | } |
237 | } |
238 | |
239 | if (argc < 1 || argc > 3 || !isLocaleObject(val: argv[0])) |
240 | THROW_ERROR("Locale: Date.fromLocaleTimeString(): Invalid arguments" ); |
241 | |
242 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
243 | |
244 | QLocale::FormatType enumFormat = QLocale::LongFormat; |
245 | QTime tm; |
246 | QString dateString = argv[1].toQStringNoThrow(); |
247 | if (argc == 3) { |
248 | if (String *s = argv[2].stringValue()) { |
249 | QString format = s->toQString(); |
250 | tm = r->d()->locale->toTime(string: dateString, format); |
251 | } else if (argv[2].isNumber()) { |
252 | quint32 intFormat = argv[2].toNumber(); |
253 | QLocale::FormatType format = QLocale::FormatType(intFormat); |
254 | tm = r->d()->locale->toTime(string: dateString, format); |
255 | } else { |
256 | THROW_ERROR("Locale: Date.fromLocaleTimeString(): Invalid datetime format" ); |
257 | } |
258 | } else { |
259 | tm = r->d()->locale->toTime(string: dateString, enumFormat); |
260 | } |
261 | |
262 | QDateTime dt; |
263 | if (tm.isValid()) { |
264 | dt = QDateTime::currentDateTime(); |
265 | dt.setTime(tm); |
266 | } |
267 | |
268 | RETURN_RESULT(engine->newDateObject(dt)); |
269 | } |
270 | |
271 | ReturnedValue QQmlDateExtension::method_fromLocaleDateString(const QV4::FunctionObject *b, const QV4::Value *, const QV4::Value *argv, int argc) |
272 | { |
273 | QV4::Scope scope(b); |
274 | QV4::ExecutionEngine * const engine = scope.engine; |
275 | |
276 | if (argc == 1) { |
277 | if (String *s = argv[0].stringValue()) { |
278 | QLocale locale; |
279 | QString dateString = s->toQString(); |
280 | QDate date = locale.toDate(string: dateString); |
281 | RETURN_RESULT(engine->newDateObject(date.startOfDay(QTimeZone::UTC))); |
282 | } |
283 | } |
284 | |
285 | if (argc < 1 || argc > 3 || !isLocaleObject(val: argv[0])) |
286 | THROW_ERROR("Locale: Date.fromLocaleDateString(): Invalid arguments" ); |
287 | |
288 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
289 | |
290 | QLocale::FormatType enumFormat = QLocale::LongFormat; |
291 | QDate dt; |
292 | QString dateString = argv[1].toQStringNoThrow(); |
293 | if (argc == 3) { |
294 | if (String *s = argv[2].stringValue()) { |
295 | QString format = s->toQString(); |
296 | dt = r->d()->locale->toDate(string: dateString, format); |
297 | } else if (argv[2].isNumber()) { |
298 | quint32 intFormat = argv[2].toNumber(); |
299 | QLocale::FormatType format = QLocale::FormatType(intFormat); |
300 | dt = r->d()->locale->toDate(string: dateString, format); |
301 | } else { |
302 | THROW_ERROR("Locale: Date.fromLocaleDateString(): Invalid datetime format" ); |
303 | } |
304 | } else { |
305 | dt = r->d()->locale->toDate(string: dateString, enumFormat); |
306 | } |
307 | |
308 | RETURN_RESULT(engine->newDateObject(dt.startOfDay(QTimeZone::UTC))); |
309 | } |
310 | |
311 | ReturnedValue QQmlDateExtension::method_timeZoneUpdated(const QV4::FunctionObject *b, const QV4::Value *, const QV4::Value *, int argc) |
312 | { |
313 | QV4::Scope scope(b); |
314 | if (argc != 0) |
315 | THROW_ERROR("Locale: Date.timeZoneUpdated(): Invalid arguments" ); |
316 | |
317 | QV4::DatePrototype::timezoneUpdated(e: scope.engine); |
318 | |
319 | RETURN_UNDEFINED(); |
320 | } |
321 | |
322 | //----------------- |
323 | // Number extension |
324 | |
325 | void QQmlNumberExtension::registerExtension(QV4::ExecutionEngine *engine) |
326 | { |
327 | engine->numberPrototype()->defineDefaultProperty(name: engine->id_toLocaleString(), code: method_toLocaleString); |
328 | engine->numberPrototype()->defineDefaultProperty(QStringLiteral("toLocaleCurrencyString" ), code: method_toLocaleCurrencyString); |
329 | engine->numberCtor()->defineDefaultProperty(QStringLiteral("fromLocaleString" ), code: method_fromLocaleString); |
330 | } |
331 | |
332 | QV4::ReturnedValue QQmlNumberExtension::method_toLocaleString(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
333 | { |
334 | QV4::Scope scope(b); |
335 | if (argc > 3) |
336 | THROW_ERROR("Locale: Number.toLocaleString(): Invalid arguments" ); |
337 | |
338 | double number = thisObject->toNumber(); |
339 | |
340 | if (argc == 0) { |
341 | // Use QLocale for standard toLocaleString() function |
342 | QLocale locale; |
343 | RETURN_RESULT(scope.engine->newString(locale.toString(number))); |
344 | } |
345 | |
346 | if (!isLocaleObject(val: argv[0])) |
347 | return QV4::NumberPrototype::method_toLocaleString(b, thisObject, argv, argc); // Use the default Number toLocaleString() |
348 | |
349 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
350 | |
351 | quint16 format = 'f'; |
352 | if (argc > 1) { |
353 | if (!argv[1].isString()) |
354 | THROW_ERROR("Locale: Number.toLocaleString(): Invalid arguments" ); |
355 | QString fs = argv[1].toQString(); |
356 | if (fs.size()) |
357 | format = fs.at(i: 0).unicode(); |
358 | } |
359 | int prec = 2; |
360 | if (argc > 2) { |
361 | if (!argv[2].isNumber()) |
362 | THROW_ERROR("Locale: Number.toLocaleString(): Invalid arguments" ); |
363 | prec = argv[2].toInt32(); |
364 | } |
365 | |
366 | RETURN_RESULT(scope.engine->newString(r->d()->locale->toString(number, (char)format, prec))); |
367 | } |
368 | |
369 | ReturnedValue QQmlNumberExtension::method_toLocaleCurrencyString(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
370 | { |
371 | QV4::Scope scope(b); |
372 | if (argc > 2) |
373 | THROW_ERROR("Locale: Number.toLocaleCurrencyString(): Invalid arguments" ); |
374 | |
375 | double number = thisObject->toNumber(); |
376 | |
377 | if (argc == 0) { |
378 | // Use QLocale for standard toLocaleString() function |
379 | QLocale locale; |
380 | RETURN_RESULT(scope.engine->newString(locale.toString(number))); |
381 | } |
382 | |
383 | if (!isLocaleObject(val: argv[0])) |
384 | THROW_ERROR("Locale: Number.toLocaleCurrencyString(): Invalid arguments" ); |
385 | |
386 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
387 | |
388 | QString symbol; |
389 | if (argc > 1) { |
390 | if (!argv[1].isString()) |
391 | THROW_ERROR("Locale: Number.toLocaleString(): Invalid arguments" ); |
392 | symbol = argv[1].toQStringNoThrow(); |
393 | } |
394 | |
395 | RETURN_RESULT(scope.engine->newString(r->d()->locale->toCurrencyString(number, symbol))); |
396 | } |
397 | |
398 | ReturnedValue QQmlNumberExtension::method_fromLocaleString(const QV4::FunctionObject *b, const QV4::Value *, const QV4::Value *argv, int argc) |
399 | { |
400 | QV4::Scope scope(b); |
401 | if (argc < 1 || argc > 2) |
402 | THROW_ERROR("Locale: Number.fromLocaleString(): Invalid arguments" ); |
403 | |
404 | int numberIdx = 0; |
405 | QLocale locale; |
406 | |
407 | if (argc == 2) { |
408 | if (!isLocaleObject(val: argv[0])) |
409 | THROW_ERROR("Locale: Number.fromLocaleString(): Invalid arguments" ); |
410 | |
411 | GET_LOCALE_DATA_RESOURCE(argv[0]); |
412 | locale = *r->d()->locale; |
413 | |
414 | numberIdx = 1; |
415 | } |
416 | |
417 | QString ns = argv[numberIdx].toQString(); |
418 | if (!ns.size()) |
419 | RETURN_RESULT(QV4::Encode(Q_QNAN)); |
420 | |
421 | bool ok = false; |
422 | double val = locale.toDouble(s: ns, ok: &ok); |
423 | |
424 | if (!ok) |
425 | THROW_ERROR("Locale: Number.fromLocaleString(): Invalid format" ); |
426 | |
427 | RETURN_RESULT(QV4::Encode(val)); |
428 | } |
429 | |
430 | //-------------- |
431 | // Locale object |
432 | |
433 | ReturnedValue QQmlLocaleData::method_get_firstDayOfWeek(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int) |
434 | { |
435 | QV4::Scope scope(b); |
436 | const QLocale *locale = getThisLocale(scope, thisObject); |
437 | if (!locale) |
438 | return Encode::undefined(); |
439 | int fdow = int(locale->firstDayOfWeek()); |
440 | if (fdow == 7) |
441 | fdow = 0; // Qt::Sunday = 7, but Sunday is 0 in JS Date |
442 | RETURN_RESULT(fdow); |
443 | } |
444 | |
445 | ReturnedValue QQmlLocaleData::method_get_numberOptions(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int) { |
446 | QV4::Scope scope(b); |
447 | const QLocale *locale = getThisLocale(scope, thisObject); |
448 | if (!locale) |
449 | return Encode::undefined(); |
450 | int numberOptions = int(locale->numberOptions()); |
451 | RETURN_RESULT(numberOptions); |
452 | } |
453 | |
454 | ReturnedValue QQmlLocaleData::method_set_numberOptions(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) { |
455 | QV4::Scope scope(b); |
456 | QLocale *locale = getThisLocale(scope, thisObject); |
457 | int const numberOptions = argc ? int(argv[0].toNumber()) : QLocale::DefaultNumberOptions; |
458 | locale->setNumberOptions(QLocale::NumberOptions {numberOptions}); |
459 | return Encode::undefined(); |
460 | } |
461 | |
462 | ReturnedValue QQmlLocaleData::method_get_formattedDataSize(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
463 | { |
464 | QV4::Scope scope(b); |
465 | const QLocale *locale = getThisLocale(scope, thisObject); |
466 | if (!locale) |
467 | return Encode::undefined(); |
468 | |
469 | if (argc < 1 || argc > 3) { |
470 | THROW_ERROR(QString::fromLatin1( |
471 | "Locale: formattedDataSize(): Expected 1-3 arguments, but received %1" ).arg(argc).toLatin1()); |
472 | } |
473 | |
474 | const qint64 bytes = static_cast<qint64>(argv[0].toInteger()); |
475 | if (argc == 1) |
476 | RETURN_RESULT(scope.engine->newString(locale->formattedDataSize(bytes))); |
477 | |
478 | int precision = 0; |
479 | if (argc >= 2) { |
480 | if (!argv[1].isInteger()) |
481 | THROW_ERROR("Locale: formattedDataSize(): Invalid argument ('precision' must be an int)" ); |
482 | |
483 | precision = argv[1].toInt32(); |
484 | if (argc == 2) |
485 | RETURN_RESULT(scope.engine->newString(locale->formattedDataSize(bytes, precision))); |
486 | } |
487 | |
488 | // argc >= 3 |
489 | if (!argv[2].isNumber()) |
490 | THROW_ERROR("Locale: formattedDataSize(): Invalid argument ('format' must be DataSizeFormat)" ); |
491 | |
492 | const quint32 intFormat = argv[2].toUInt32(); |
493 | const auto format = QLocale::DataSizeFormats(intFormat); |
494 | RETURN_RESULT(scope.engine->newString(locale->formattedDataSize(bytes, precision, format))); |
495 | } |
496 | |
497 | ReturnedValue QQmlLocaleData::method_get_measurementSystem(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int) |
498 | { |
499 | QV4::Scope scope(b); |
500 | const QLocale *locale = getThisLocale(scope, thisObject); |
501 | if (!locale) |
502 | return Encode::undefined(); |
503 | return QV4::Encode(locale->measurementSystem()); |
504 | } |
505 | |
506 | ReturnedValue QQmlLocaleData::method_get_textDirection(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int) |
507 | { |
508 | QV4::Scope scope(b); |
509 | const QLocale *locale = getThisLocale(scope, thisObject); |
510 | if (!locale) |
511 | return Encode::undefined(); |
512 | |
513 | return QV4::Encode(locale->textDirection()); |
514 | } |
515 | |
516 | ReturnedValue QQmlLocaleData::method_get_weekDays(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int) |
517 | { |
518 | QV4::Scope scope(b); |
519 | const QLocale *locale = getThisLocale(scope, thisObject); |
520 | if (!locale) |
521 | return Encode::undefined(); |
522 | |
523 | QList<Qt::DayOfWeek> days = locale->weekdays(); |
524 | |
525 | QV4::ScopedArrayObject result(scope, scope.engine->newArrayObject()); |
526 | result->arrayReserve(n: days.size()); |
527 | for (int i = 0; i < days.size(); ++i) { |
528 | int day = days.at(i); |
529 | if (day == 7) // JS Date days in range 0(Sunday) to 6(Saturday) |
530 | day = 0; |
531 | result->arrayPut(index: i, value: QV4::Value::fromInt32(i: day)); |
532 | } |
533 | result->setArrayLengthUnchecked(days.size()); |
534 | |
535 | return result.asReturnedValue(); |
536 | } |
537 | |
538 | ReturnedValue QQmlLocaleData::method_get_uiLanguages(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int) |
539 | { |
540 | QV4::Scope scope(b); |
541 | const QLocale *locale = getThisLocale(scope, thisObject); |
542 | if (!locale) |
543 | return Encode::undefined(); |
544 | |
545 | QStringList langs = locale->uiLanguages(); |
546 | QV4::ScopedArrayObject result(scope, scope.engine->newArrayObject()); |
547 | result->arrayReserve(n: langs.size()); |
548 | QV4::ScopedValue v(scope); |
549 | for (int i = 0; i < langs.size(); ++i) |
550 | result->arrayPut(index: i, value: (v = scope.engine->newString(s: langs.at(i)))); |
551 | |
552 | result->setArrayLengthUnchecked(langs.size()); |
553 | |
554 | return result.asReturnedValue(); |
555 | } |
556 | |
557 | ReturnedValue QQmlLocaleData::method_currencySymbol(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
558 | { |
559 | QV4::Scope scope(b); |
560 | const QLocale *locale = getThisLocale(scope, thisObject); |
561 | if (!locale) |
562 | return Encode::undefined(); |
563 | |
564 | if (argc > 1) |
565 | THROW_ERROR("Locale: currencySymbol(): Invalid arguments" ); |
566 | |
567 | QLocale::CurrencySymbolFormat format = QLocale::CurrencySymbol; |
568 | if (argc == 1) { |
569 | quint32 intFormat = argv[0].toNumber(); |
570 | format = QLocale::CurrencySymbolFormat(intFormat); |
571 | } |
572 | |
573 | RETURN_RESULT(scope.engine->newString(locale->currencySymbol(format))); |
574 | } |
575 | |
576 | #define LOCALE_FORMAT(FUNC) \ |
577 | ReturnedValue QQmlLocaleData::method_ ##FUNC (const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) { \ |
578 | QV4::Scope scope(b); \ |
579 | const QLocale *locale = getThisLocale(scope, thisObject); \ |
580 | if (!locale) \ |
581 | return Encode::undefined(); \ |
582 | if (argc > 1) \ |
583 | THROW_ERROR("Locale: " #FUNC "(): Invalid arguments"); \ |
584 | QLocale::FormatType format = QLocale::LongFormat;\ |
585 | if (argc == 1) { \ |
586 | quint32 intFormat = argv[0].toUInt32(); \ |
587 | format = QLocale::FormatType(intFormat); \ |
588 | } \ |
589 | RETURN_RESULT(scope.engine->newString(locale-> FUNC (format))); \ |
590 | } |
591 | |
592 | LOCALE_FORMAT(dateTimeFormat) |
593 | LOCALE_FORMAT(timeFormat) |
594 | LOCALE_FORMAT(dateFormat) |
595 | |
596 | // +1 added to idx because JS is 0-based, whereas QLocale months begin at 1. |
597 | #define LOCALE_FORMATTED_MONTHNAME(VARIABLE) \ |
598 | ReturnedValue QQmlLocaleData::method_ ## VARIABLE (const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) {\ |
599 | Scope scope(b); \ |
600 | const QLocale *locale = getThisLocale(scope, thisObject); \ |
601 | if (!locale) \ |
602 | return Encode::undefined(); \ |
603 | if (argc < 1 || argc > 2) \ |
604 | THROW_ERROR("Locale: " #VARIABLE "(): Invalid arguments"); \ |
605 | QLocale::FormatType enumFormat = QLocale::LongFormat; \ |
606 | int idx = argv[0].toInt32() + 1; \ |
607 | if (idx < 1 || idx > 12) \ |
608 | THROW_ERROR("Locale: Invalid month"); \ |
609 | QString name; \ |
610 | if (argc == 2) { \ |
611 | if (argv[1].isNumber()) { \ |
612 | quint32 intFormat = argv[1].toUInt32(); \ |
613 | QLocale::FormatType format = QLocale::FormatType(intFormat); \ |
614 | name = locale-> VARIABLE(idx, format); \ |
615 | } else { \ |
616 | THROW_ERROR("Locale: Invalid datetime format"); \ |
617 | } \ |
618 | } else { \ |
619 | name = locale-> VARIABLE(idx, enumFormat); \ |
620 | } \ |
621 | RETURN_RESULT(scope.engine->newString(name)); \ |
622 | } |
623 | |
624 | // 0 -> 7 as Qt::Sunday is 7, but Sunday is 0 in JS Date |
625 | #define LOCALE_FORMATTED_DAYNAME(VARIABLE) \ |
626 | ReturnedValue QQmlLocaleData::method_ ## VARIABLE (const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) {\ |
627 | Scope scope(b); \ |
628 | const QLocale *locale = getThisLocale(scope, thisObject); \ |
629 | if (!locale) \ |
630 | return Encode::undefined(); \ |
631 | if (argc < 1 || argc > 2) \ |
632 | THROW_ERROR("Locale: " #VARIABLE "(): Invalid arguments"); \ |
633 | QLocale::FormatType enumFormat = QLocale::LongFormat; \ |
634 | int idx = argv[0].toInt32(); \ |
635 | if (idx < 0 || idx > 7) \ |
636 | THROW_ERROR("Locale: Invalid day"); \ |
637 | if (idx == 0) idx = 7; \ |
638 | QString name; \ |
639 | if (argc == 2) { \ |
640 | if (argv[1].isNumber()) { \ |
641 | quint32 intFormat = argv[1].toUInt32(); \ |
642 | QLocale::FormatType format = QLocale::FormatType(intFormat); \ |
643 | name = locale-> VARIABLE(idx, format); \ |
644 | } else { \ |
645 | THROW_ERROR("Locale: Invalid datetime format"); \ |
646 | } \ |
647 | } else { \ |
648 | name = locale-> VARIABLE(idx, enumFormat); \ |
649 | } \ |
650 | RETURN_RESULT(scope.engine->newString(name)); \ |
651 | } |
652 | |
653 | LOCALE_FORMATTED_MONTHNAME(monthName) |
654 | LOCALE_FORMATTED_MONTHNAME(standaloneMonthName) |
655 | LOCALE_FORMATTED_DAYNAME(dayName) |
656 | LOCALE_FORMATTED_DAYNAME(standaloneDayName) |
657 | |
658 | ReturnedValue QQmlLocaleData::method_toString(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
659 | { |
660 | Scope scope(b); |
661 | const QLocale *locale = getThisLocale(scope, thisObject); |
662 | if (!locale) |
663 | return Encode::undefined(); |
664 | |
665 | if (argc == 0) { |
666 | // As a special (undocumented) case, when called with no arguments, |
667 | // just forward to QDebug. This makes it consistent with other types |
668 | // in JS that can be converted to a string via toString(). |
669 | RETURN_RESULT(scope.engine->newString(QDebug::toString(*locale))); |
670 | } |
671 | |
672 | if (argc > 3) { |
673 | return scope.engine->throwError(message: QString::fromLatin1( |
674 | ba: "Locale: toString(): Expected 1-3 arguments, but received %1" ).arg(a: argc)); |
675 | } |
676 | |
677 | if (argv[0].isNumber()) { |
678 | if (argv[0].isInteger()) { |
679 | // toString(int) |
680 | RETURN_RESULT(scope.engine->newString(locale->toString(argv[0].toInt32()))); |
681 | } else { |
682 | // toString(double[, char][, int]) |
683 | const double number = argv[0].toNumber(); |
684 | if (argc == 1) |
685 | RETURN_RESULT(scope.engine->newString(locale->toString(number))); |
686 | |
687 | if (!argv[1].isString()) { |
688 | THROW_ERROR("Locale: the second argument to the toString overload " |
689 | "whose first argument is a double should be a char" ); |
690 | } |
691 | const char format = argv[1].toQString().at(i: 0).toLatin1(); |
692 | |
693 | switch (argc) { |
694 | case 2: |
695 | RETURN_RESULT(scope.engine->newString(locale->toString(number, format))); |
696 | case 3: |
697 | if (!argv[2].isInteger()) { |
698 | THROW_ERROR("Locale: the third argument to the toString overload " |
699 | "whose first argument is a double should be an int" ); |
700 | } |
701 | |
702 | const int precision = argv[2].toInt32(); |
703 | RETURN_RESULT(scope.engine->newString(locale->toString(number, format, precision))); |
704 | } |
705 | } |
706 | } else if (const DateObject *dateObject = argv[0].as<DateObject>()) { |
707 | // toString(Date, string) or toString(Date[, FormatType]) |
708 | if (argc > 2) { |
709 | return scope.engine->throwError(message: QString::fromLatin1( |
710 | ba: "Locale: the toString() overload that takes a Date as its first " |
711 | "argument expects 1 or 2 arguments, but received %1" ).arg(a: argc)); |
712 | } |
713 | |
714 | if (argc == 2 && argv[1].isString()) { |
715 | RETURN_RESULT(scope.engine->newString(locale->toString( |
716 | dateObject->toQDateTime(), argv[1].toQString()))); |
717 | } |
718 | |
719 | if (argc == 2 && !argv[1].isNumber()) { |
720 | THROW_ERROR("Locale: the second argument to the toString overloads whose " |
721 | "first argument is a Date should be a string or FormatType" ); |
722 | } |
723 | |
724 | const QLocale::FormatType format = argc == 2 |
725 | ? QLocale::FormatType(argv[1].toNumber()) : QLocale::LongFormat; |
726 | RETURN_RESULT(scope.engine->newString(locale->toString(dateObject->toQDateTime(), format))); |
727 | } |
728 | |
729 | THROW_ERROR("Locale: toString() expects either an int, double, or Date as its first argument" ); |
730 | } |
731 | |
732 | #define LOCALE_STRING_PROPERTY(VARIABLE) \ |
733 | ReturnedValue QQmlLocaleData::method_get_ ## VARIABLE (const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *, int) \ |
734 | { \ |
735 | Scope scope(b); \ |
736 | const QLocale *locale = getThisLocale(scope, thisObject); \ |
737 | if (!locale) \ |
738 | return Encode::undefined(); \ |
739 | RETURN_RESULT(scope.engine->newString(locale-> VARIABLE()));\ |
740 | } |
741 | |
742 | LOCALE_STRING_PROPERTY(name) |
743 | LOCALE_STRING_PROPERTY(nativeLanguageName) |
744 | #if QT_DEPRECATED_SINCE(6, 6) |
745 | QT_IGNORE_DEPRECATIONS(LOCALE_STRING_PROPERTY(nativeCountryName)) |
746 | #endif |
747 | LOCALE_STRING_PROPERTY(nativeTerritoryName) |
748 | LOCALE_STRING_PROPERTY(decimalPoint) |
749 | LOCALE_STRING_PROPERTY(groupSeparator) |
750 | LOCALE_STRING_PROPERTY(percent) |
751 | LOCALE_STRING_PROPERTY(zeroDigit) |
752 | LOCALE_STRING_PROPERTY(negativeSign) |
753 | LOCALE_STRING_PROPERTY(positiveSign) |
754 | LOCALE_STRING_PROPERTY(exponential) |
755 | LOCALE_STRING_PROPERTY(amText) |
756 | LOCALE_STRING_PROPERTY(pmText) |
757 | |
758 | class QV4LocaleDataDeletable : public QV4::ExecutionEngine::Deletable |
759 | { |
760 | public: |
761 | QV4LocaleDataDeletable(QV4::ExecutionEngine *engine); |
762 | ~QV4LocaleDataDeletable(); |
763 | |
764 | QV4::PersistentValue prototype; |
765 | }; |
766 | |
767 | QV4LocaleDataDeletable::QV4LocaleDataDeletable(QV4::ExecutionEngine *engine) |
768 | { |
769 | QV4::Scope scope(engine); |
770 | QV4::Scoped<QV4::Object> o(scope, engine->newObject()); |
771 | |
772 | o->defineDefaultProperty(QStringLiteral("dateFormat" ), code: QQmlLocaleData::method_dateFormat, argumentCount: 0); |
773 | o->defineDefaultProperty(QStringLiteral("standaloneDayName" ), code: QQmlLocaleData::method_standaloneDayName, argumentCount: 0); |
774 | o->defineDefaultProperty(QStringLiteral("standaloneMonthName" ), code: QQmlLocaleData::method_standaloneMonthName, argumentCount: 0); |
775 | o->defineDefaultProperty(QStringLiteral("dayName" ), code: QQmlLocaleData::method_dayName, argumentCount: 0); |
776 | o->defineDefaultProperty(QStringLiteral("timeFormat" ), code: QQmlLocaleData::method_timeFormat, argumentCount: 0); |
777 | o->defineDefaultProperty(QStringLiteral("monthName" ), code: QQmlLocaleData::method_monthName, argumentCount: 0); |
778 | o->defineDefaultProperty(QStringLiteral("toString" ), code: QQmlLocaleData::method_toString, argumentCount: 0); |
779 | o->defineDefaultProperty(QStringLiteral("currencySymbol" ), code: QQmlLocaleData::method_currencySymbol, argumentCount: 0); |
780 | o->defineDefaultProperty(QStringLiteral("dateTimeFormat" ), code: QQmlLocaleData::method_dateTimeFormat, argumentCount: 0); |
781 | o->defineDefaultProperty(QStringLiteral("formattedDataSize" ), code: QQmlLocaleData::method_get_formattedDataSize, argumentCount: 0); |
782 | o->defineAccessorProperty(QStringLiteral("name" ), getter: QQmlLocaleData::method_get_name, setter: nullptr); |
783 | o->defineAccessorProperty(QStringLiteral("positiveSign" ), getter: QQmlLocaleData::method_get_positiveSign, setter: nullptr); |
784 | o->defineAccessorProperty(QStringLiteral("uiLanguages" ), getter: QQmlLocaleData::method_get_uiLanguages, setter: nullptr); |
785 | o->defineAccessorProperty(QStringLiteral("firstDayOfWeek" ), getter: QQmlLocaleData::method_get_firstDayOfWeek, setter: nullptr); |
786 | o->defineAccessorProperty(QStringLiteral("pmText" ), getter: QQmlLocaleData::method_get_pmText, setter: nullptr); |
787 | o->defineAccessorProperty(QStringLiteral("percent" ), getter: QQmlLocaleData::method_get_percent, setter: nullptr); |
788 | o->defineAccessorProperty(QStringLiteral("textDirection" ), getter: QQmlLocaleData::method_get_textDirection, setter: nullptr); |
789 | o->defineAccessorProperty(QStringLiteral("weekDays" ), getter: QQmlLocaleData::method_get_weekDays, setter: nullptr); |
790 | o->defineAccessorProperty(QStringLiteral("negativeSign" ), getter: QQmlLocaleData::method_get_negativeSign, setter: nullptr); |
791 | o->defineAccessorProperty(QStringLiteral("groupSeparator" ), getter: QQmlLocaleData::method_get_groupSeparator, setter: nullptr); |
792 | o->defineAccessorProperty(QStringLiteral("decimalPoint" ), getter: QQmlLocaleData::method_get_decimalPoint, setter: nullptr); |
793 | o->defineAccessorProperty(QStringLiteral("nativeLanguageName" ), getter: QQmlLocaleData::method_get_nativeLanguageName, setter: nullptr); |
794 | #if QT_DEPRECATED_SINCE(6, 6) |
795 | o->defineAccessorProperty(QStringLiteral("nativeCountryName" ), getter: QQmlLocaleData::method_get_nativeCountryName, setter: nullptr); |
796 | #endif |
797 | o->defineAccessorProperty(QStringLiteral("nativeTerritoryName" ), getter: QQmlLocaleData::method_get_nativeTerritoryName, setter: nullptr); |
798 | o->defineAccessorProperty(QStringLiteral("zeroDigit" ), getter: QQmlLocaleData::method_get_zeroDigit, setter: nullptr); |
799 | o->defineAccessorProperty(QStringLiteral("amText" ), getter: QQmlLocaleData::method_get_amText, setter: nullptr); |
800 | o->defineAccessorProperty(QStringLiteral("measurementSystem" ), getter: QQmlLocaleData::method_get_measurementSystem, setter: nullptr); |
801 | o->defineAccessorProperty(QStringLiteral("exponential" ), getter: QQmlLocaleData::method_get_exponential, setter: nullptr); |
802 | o->defineAccessorProperty(QStringLiteral("numberOptions" ), getter: QQmlLocaleData::method_get_numberOptions, setter: QQmlLocaleData::method_set_numberOptions); |
803 | |
804 | prototype.set(engine, value: o); |
805 | } |
806 | |
807 | QV4LocaleDataDeletable::~QV4LocaleDataDeletable() |
808 | { |
809 | } |
810 | |
811 | V4_DEFINE_EXTENSION(QV4LocaleDataDeletable, localeV4Data); |
812 | |
813 | /*! |
814 | \qmltype Locale |
815 | //! \instantiates QQmlLocale |
816 | \inqmlmodule QtQml |
817 | \brief Provides locale specific properties and formatted data. |
818 | |
819 | The Locale object may only be created via the \l{QtQml::Qt::locale()}{Qt.locale()} function. |
820 | It cannot be created directly. |
821 | |
822 | The \l{QtQml::Qt::locale()}{Qt.locale()} function returns a JS Locale object representing the |
823 | locale with the specified name, which has the format |
824 | "language[_territory][.codeset][@modifier]" or "C". |
825 | |
826 | Locale supports the concept of a default locale, which is |
827 | determined from the system's locale settings at application |
828 | startup. If no parameter is passed to Qt.locale() the default |
829 | locale object is returned. |
830 | |
831 | The Locale object provides a number of functions and properties |
832 | providing data for the specified locale. |
833 | |
834 | The Locale object may also be passed to the \l Date and \l Number toLocaleString() |
835 | and fromLocaleString() methods in order to convert to/from strings using |
836 | the specified locale. |
837 | |
838 | This example shows the current date formatted for the German locale: |
839 | |
840 | \code |
841 | import QtQuick 2.0 |
842 | |
843 | Text { |
844 | text: "The date is: " + Date().toLocaleString(Qt.locale("de_DE")) |
845 | } |
846 | \endcode |
847 | |
848 | The following example displays the specified number |
849 | in the correct format for the default locale: |
850 | |
851 | \code |
852 | import QtQuick 2.0 |
853 | |
854 | Text { |
855 | text: "The value is: " + Number(23443.34).toLocaleString(Qt.locale()) |
856 | } |
857 | \endcode |
858 | |
859 | Qt Quick Locale's data is based on Common Locale Data Repository v1.8.1. |
860 | |
861 | |
862 | \target FormatType |
863 | \section2 Locale String Format Types |
864 | |
865 | The monthName(), standaloneMonthName(), dayName() and standaloneDayName() |
866 | can use the following enumeration values to specify the formatting of |
867 | the string representation for a Date object. |
868 | |
869 | \value Locale.LongFormat The long version of day and month names; for |
870 | example, returning "January" as a month name. |
871 | \value Locale.ShortFormat The short version of day and month names; for |
872 | example, returning "Jan" as a month name. |
873 | \value Locale.NarrowFormat A special version of day and month names for |
874 | use when space is limited; for example, returning "J" as a month |
875 | name. Note that the narrow format might contain the same text for |
876 | different months and days or it can even be an empty string if the |
877 | locale doesn't support narrow names, so you should avoid using it |
878 | for date formatting. Also, for the system locale this format is |
879 | the same as ShortFormat. |
880 | |
881 | |
882 | Additionally the double-to-string and string-to-double conversion functions are |
883 | covered by the following licenses: |
884 | |
885 | \legalese |
886 | Copyright (c) 1991 by AT&T. |
887 | |
888 | Permission to use, copy, modify, and distribute this software for any |
889 | purpose without fee is hereby granted, provided that this entire notice |
890 | is included in all copies of any software which is or includes a copy |
891 | or modification of this software and in all copies of the supporting |
892 | documentation for such software. |
893 | |
894 | THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED |
895 | WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY |
896 | REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY |
897 | OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. |
898 | |
899 | This product includes software developed by the University of |
900 | California, Berkeley and its contributors. |
901 | |
902 | \sa Date, Number |
903 | */ |
904 | |
905 | QV4::ReturnedValue QQmlLocale::locale(ExecutionEngine *engine, const QString &localeName) |
906 | { |
907 | QLocale qlocale; |
908 | if (!localeName.isEmpty()) |
909 | qlocale = QLocale(localeName); |
910 | return wrap(engine, locale: qlocale); |
911 | } |
912 | |
913 | QV4::ReturnedValue QQmlLocale::wrap(ExecutionEngine *v4, const QLocale &locale) |
914 | { |
915 | QV4::Scope scope(v4); |
916 | QV4LocaleDataDeletable *d = localeV4Data(engine: scope.engine); |
917 | QV4::Scoped<QQmlLocaleData> wrapper(scope, v4->memoryManager->allocate<QQmlLocaleData>()); |
918 | *wrapper->d()->locale = locale; |
919 | QV4::ScopedObject p(scope, d->prototype.value()); |
920 | wrapper->setPrototypeOf(p); |
921 | return wrapper.asReturnedValue(); |
922 | } |
923 | |
924 | void QQmlLocale::registerStringLocaleCompare(QV4::ExecutionEngine *engine) |
925 | { |
926 | engine->stringPrototype()->defineDefaultProperty(QStringLiteral("localeCompare" ), code: method_localeCompare); |
927 | } |
928 | |
929 | ReturnedValue QQmlLocale::method_localeCompare(const QV4::FunctionObject *b, const QV4::Value *thisObject, const QV4::Value *argv, int argc) |
930 | { |
931 | if (argc != 1 || (!argv[0].isString() && !argv[0].as<StringObject>())) |
932 | return QV4::StringPrototype::method_localeCompare(b, thisObject, argv, argc); |
933 | |
934 | if (!thisObject->isString() && !thisObject->as<StringObject>()) |
935 | return QV4::StringPrototype::method_localeCompare(b, thisObject, argv, argc); |
936 | |
937 | QString thisString = thisObject->toQStringNoThrow(); |
938 | QString thatString = argv[0].toQStringNoThrow(); |
939 | |
940 | return QV4::Encode(QString::localeAwareCompare(s1: thisString, s2: thatString)); |
941 | } |
942 | |
943 | /*! |
944 | \qmlproperty string QtQml::Locale::name |
945 | |
946 | Holds the language and territory of this locale as a |
947 | string of the form "language_territory", where |
948 | language is a lowercase, two-letter ISO 639 language code, |
949 | and territory is an uppercase, two- or three-letter ISO 3166 territory code. |
950 | */ |
951 | |
952 | /*! |
953 | \qmlproperty string QtQml::Locale::decimalPoint |
954 | |
955 | Holds the decimal point character of this locale. |
956 | */ |
957 | |
958 | /*! |
959 | \qmlproperty string QtQml::Locale::groupSeparator |
960 | |
961 | Holds the group separator character of this locale. |
962 | */ |
963 | |
964 | /*! |
965 | \qmlproperty enumeration QtQml::Locale::numberOptions |
966 | |
967 | Holds a set of options for number-to-string and |
968 | string-to-number conversions. |
969 | |
970 | \sa Number::toLocaleString() |
971 | \sa Number::fromLocaleString() |
972 | */ |
973 | |
974 | /*! |
975 | \qmlproperty string QtQml::Locale::percent |
976 | |
977 | Holds the percent character of this locale. |
978 | */ |
979 | |
980 | |
981 | /*! |
982 | \qmlproperty string QtQml::Locale::zeroDigit |
983 | |
984 | Holds Returns the zero digit character of this locale. |
985 | */ |
986 | |
987 | /*! |
988 | \qmlproperty string QtQml::Locale::negativeSign |
989 | |
990 | Holds the negative sign character of this locale. |
991 | */ |
992 | |
993 | /*! |
994 | \qmlproperty string QtQml::Locale::positiveSign |
995 | |
996 | Holds the positive sign character of this locale. |
997 | */ |
998 | |
999 | /*! |
1000 | \qmlproperty string QtQml::Locale::exponential |
1001 | |
1002 | Holds the exponential character of this locale. |
1003 | */ |
1004 | |
1005 | /*! |
1006 | \qmlmethod string QtQml::Locale::dateTimeFormat(type) |
1007 | |
1008 | Returns the date time format used for the current locale. |
1009 | \a type specifies the FormatType to return. |
1010 | |
1011 | \sa Date |
1012 | */ |
1013 | |
1014 | /*! |
1015 | \qmlmethod string QtQml::Locale::dateFormat(type) |
1016 | |
1017 | Returns the date format used for the current locale. |
1018 | \a type specifies the FormatType to return. |
1019 | |
1020 | \sa Date |
1021 | */ |
1022 | |
1023 | /*! |
1024 | \qmlmethod string QtQml::Locale::timeFormat(type) |
1025 | |
1026 | Returns the time format used for the current locale. |
1027 | \a type specifies the FormatType to return. |
1028 | |
1029 | \sa Date |
1030 | */ |
1031 | |
1032 | /*! |
1033 | \qmlmethod string QtQml::Locale::formattedDataSize(int bytes, int precision, DataSizeFormat format) |
1034 | \since 6.2 |
1035 | |
1036 | Converts a size in \a bytes to a human-readable localized string, comprising a |
1037 | number and a quantified unit. |
1038 | |
1039 | The \a precision and \a format arguments are optional. |
1040 | |
1041 | For more information, see \l QLocale::formattedDataSize(). |
1042 | |
1043 | \sa QLocale::DataSizeFormats |
1044 | */ |
1045 | |
1046 | /*! |
1047 | \qmlmethod string QtQml::Locale::monthName(month, type) |
1048 | |
1049 | Returns the localized name of \a month (0-11), in the optional |
1050 | \l FormatType specified by \a type. |
1051 | |
1052 | \note the QLocale C++ API expects a range of (1-12), however Locale.monthName() |
1053 | expects 0-11 as per the JS Date object. |
1054 | |
1055 | \sa dayName(), standaloneMonthName() |
1056 | */ |
1057 | |
1058 | /*! |
1059 | \qmlmethod string QtQml::Locale::standaloneMonthName(month, type) |
1060 | |
1061 | Returns the localized name of \a month (0-11) that is used as a |
1062 | standalone text, in the optional \l FormatType specified by \a type. |
1063 | |
1064 | If the locale information doesn't specify the standalone month |
1065 | name then return value is the same as in monthName(). |
1066 | |
1067 | \note the QLocale C++ API expects a range of (1-12), however Locale.standaloneMonthName() |
1068 | expects 0-11 as per the JS Date object. |
1069 | |
1070 | \sa monthName(), standaloneDayName() |
1071 | */ |
1072 | |
1073 | /*! |
1074 | \qmlmethod string QtQml::Locale::dayName(day, type) |
1075 | |
1076 | Returns the localized name of the \a day (where 0 represents |
1077 | Sunday, 1 represents Monday and so on), in the optional |
1078 | \l FormatType specified by \a type. |
1079 | |
1080 | \sa monthName(), standaloneDayName() |
1081 | */ |
1082 | |
1083 | /*! |
1084 | \qmlmethod string QtQml::Locale::standaloneDayName(day, type) |
1085 | |
1086 | Returns the localized name of the \a day (where 0 represents |
1087 | Sunday, 1 represents Monday and so on) that is used as a |
1088 | standalone text, in the \l FormatType specified by \a type. |
1089 | |
1090 | If the locale information does not specify the standalone day |
1091 | name then return value is the same as in dayName(). |
1092 | |
1093 | \sa dayName(), standaloneMonthName() |
1094 | */ |
1095 | |
1096 | /*! |
1097 | \qmlproperty enumeration QtQml::Locale::firstDayOfWeek |
1098 | |
1099 | Holds the first day of the week according to the current locale. |
1100 | |
1101 | \value Locale.Sunday 0 |
1102 | \value Locale.Monday 1 |
1103 | \value Locale.Tuesday 2 |
1104 | \value Locale.Wednesday 3 |
1105 | \value Locale.Thursday 4 |
1106 | \value Locale.Friday 5 |
1107 | \value Locale.Saturday 6 |
1108 | |
1109 | \note that these values match the JS Date API which is different |
1110 | from the Qt C++ API where Qt::Sunday = 7. |
1111 | */ |
1112 | |
1113 | /*! |
1114 | \qmlproperty Array<int> QtQml::Locale::weekDays |
1115 | |
1116 | Holds an array of days that are considered week days according to the current locale, |
1117 | where Sunday is 0 and Saturday is 6. |
1118 | |
1119 | \sa firstDayOfWeek |
1120 | */ |
1121 | |
1122 | /*! |
1123 | \qmlmethod string QtQml::Locale::toString(int i) |
1124 | \since 6.5 |
1125 | |
1126 | Returns a localized string representation of \a i. |
1127 | |
1128 | \sa QLocale::toString(int) |
1129 | */ |
1130 | |
1131 | /*! |
1132 | \qmlmethod string QtQml::Locale::toString(double f, char format = 'g', int precision = 6) |
1133 | \overload |
1134 | \since 6.5 |
1135 | |
1136 | Returns a string representing the floating-point number \a f. |
1137 | |
1138 | The form of the representation is controlled by the optional \a format and |
1139 | \a precision parameters. |
1140 | |
1141 | See \l {QLocale::toString(double, char, int)} for more information. |
1142 | */ |
1143 | |
1144 | /*! |
1145 | \qmlmethod string QtQml::Locale::toString(Date date, string format) |
1146 | \overload |
1147 | \since 6.5 |
1148 | |
1149 | Returns a localized string representation of the given \a date in the |
1150 | specified \a format. If \c format is an empty string, an empty string is |
1151 | returned. |
1152 | |
1153 | \sa QLocale::toString(QDate, QStringView) |
1154 | */ |
1155 | |
1156 | /*! |
1157 | \qmlmethod string QtQml::Locale::toString(Date date, FormatType format = LongFormat) |
1158 | \overload |
1159 | \since 6.5 |
1160 | |
1161 | Returns a localized string representation of the given \a date in the |
1162 | specified \a format. If \c format is omitted, \c Locale.LongFormat is used. |
1163 | |
1164 | \sa QLocale::toString(QDate, QLocale::FormatType) |
1165 | */ |
1166 | |
1167 | /*! |
1168 | \qmlproperty Array<string> QtQml::Locale::uiLanguages |
1169 | |
1170 | Returns an ordered list of locale names for translation purposes in |
1171 | preference order. |
1172 | |
1173 | The return value represents locale names that the user expects to see the |
1174 | UI translation in. |
1175 | |
1176 | The first item in the list is the most preferred one. |
1177 | */ |
1178 | |
1179 | /*! |
1180 | \qmlproperty enumeration QtQml::Locale::textDirection |
1181 | |
1182 | Holds the text direction of the language: |
1183 | |
1184 | \value Qt.LeftToRight Text normally begins at the left side. |
1185 | \value Qt.RightToLeft Text normally begins at the right side. |
1186 | */ |
1187 | |
1188 | /*! |
1189 | \qmlproperty string QtQml::Locale::amText |
1190 | |
1191 | The localized name of the "AM" suffix for times specified using the conventions of the 12-hour clock. |
1192 | */ |
1193 | |
1194 | /*! |
1195 | \qmlproperty string QtQml::Locale::pmText |
1196 | |
1197 | The localized name of the "PM" suffix for times specified using the conventions of the 12-hour clock. |
1198 | */ |
1199 | |
1200 | /*! |
1201 | \qmlmethod string QtQml::Locale::currencySymbol(format) |
1202 | |
1203 | Returns the currency symbol for the specified \a format: |
1204 | |
1205 | \value Locale.CurrencyIsoCode a ISO-4217 code of the currency. |
1206 | \value Locale.CurrencySymbol a currency symbol. |
1207 | \value Locale.CurrencyDisplayName a user readable name of the currency. |
1208 | |
1209 | \sa Number::toLocaleCurrencyString() |
1210 | */ |
1211 | |
1212 | /*! |
1213 | \qmlproperty string QtQml::Locale::nativeLanguageName |
1214 | |
1215 | Holds a native name of the language for the locale. For example |
1216 | "Schwiizertüütsch" for Swiss-German locale. |
1217 | |
1218 | \sa nativeTerritoryName |
1219 | */ |
1220 | |
1221 | /*! |
1222 | \qmlproperty string QtQml::Locale::nativeCountryName |
1223 | \deprecated [6.4] Use nativeTerritoryName instead. |
1224 | |
1225 | Holds a native name of the country for the locale. For example |
1226 | "España" for Spanish/Spain locale. |
1227 | |
1228 | \sa nativeLanguageName |
1229 | */ |
1230 | |
1231 | /*! |
1232 | \qmlproperty string QtQml::Locale::nativeTerritoryName |
1233 | |
1234 | Holds a native name of the territory for the locale. For example |
1235 | "España" for Spanish/Spain locale. |
1236 | |
1237 | \sa nativeLanguageName |
1238 | */ |
1239 | |
1240 | /*! |
1241 | \qmlproperty enumeration QtQml::Locale::measurementSystem |
1242 | |
1243 | This property defines which units are used for measurement. |
1244 | |
1245 | \value Locale.MetricSystem This value indicates metric units, such as meters, |
1246 | centimeters and millimeters. |
1247 | \value Locale.ImperialUSSystem This value indicates imperial units, such as |
1248 | inches and miles as they are used in the United States. |
1249 | \value Locale.ImperialUKSystem This value indicates imperial units, such as |
1250 | inches and miles as they are used in the United Kingdom. |
1251 | \value Locale.ImperialSystem Provided for compatibility. The same as Locale.ImperialUSSystem. |
1252 | */ |
1253 | |
1254 | QT_END_NAMESPACE |
1255 | |
1256 | #include "moc_qqmllocale_p.cpp" |
1257 | |