1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #if !defined(QWS) && defined(Q_OS_MAC)
43 # include "private/qcore_mac_p.h"
44 # include <CoreFoundation/CoreFoundation.h>
49 #include "qplatformdefs.h"
51 #include "qdatastream.h"
55 #include "qlocale_p.h"
56 #include "qlocale_tools_p.h"
57 #include "qdatetime_p.h"
58 #include "qnamespace.h"
59 #include "qdatetime.h"
60 #include "qstringlist.h"
62 #include "qstringbuilder.h"
63 #include "private/qnumeric_p.h"
64 #include "private/qsystemlibrary_p.h"
66 # include <qt_windows.h>
72 #ifndef QT_NO_SYSTEMLOCALE
73 static QSystemLocale *_systemLocale = 0;
74 class QSystemLocaleSingleton: public QSystemLocale
77 QSystemLocaleSingleton() : QSystemLocale(true) {}
80 Q_GLOBAL_STATIC(QSystemLocaleSingleton, QSystemLocale_globalSystemLocale)
81 static QLocalePrivate *system_lp = 0;
82 Q_GLOBAL_STATIC(QLocalePrivate, globalLocalePrivate)
86 extern bool qt_initIcu(const QString &localeName);
87 extern bool qt_u_strToUpper(const QString &str, QString *out, const QLocale &locale);
88 extern bool qt_u_strToLower(const QString &str, QString *out, const QLocale &locale);
91 /******************************************************************************
92 ** Helpers for accessing Qt locale database
95 QT_BEGIN_INCLUDE_NAMESPACE
96 #include "qlocale_data_p.h"
97 QT_END_INCLUDE_NAMESPACE
99 QLocale::Language QLocalePrivate::codeToLanguage(const QString &code)
101 int len = code.length();
102 if (len != 2 && len != 3)
104 ushort uc1 = len-- > 0 ? code[0].toLower().unicode() : 0;
105 ushort uc2 = len-- > 0 ? code[1].toLower().unicode() : 0;
106 ushort uc3 = len-- > 0 ? code[2].toLower().unicode() : 0;
108 if (uc1 == 'n' && uc2 == 'o' && uc3 == 0)
111 const unsigned char *c = language_code_list;
112 for (; *c != 0; c += 3) {
113 if (uc1 == c[0] && uc2 == c[1] && uc3 == c[2])
114 return QLocale::Language((c - language_code_list)/3);
120 QLocale::Script QLocalePrivate::codeToScript(const QString &code)
122 int len = code.length();
124 return QLocale::AnyScript;
126 // script is titlecased in our data
127 unsigned char c0 = code.at(0).toUpper().toLatin1();
128 unsigned char c1 = code.at(1).toLower().toLatin1();
129 unsigned char c2 = code.at(2).toLower().toLatin1();
130 unsigned char c3 = code.at(3).toLower().toLatin1();
132 const unsigned char *c = script_code_list;
133 for (int i = 0; i < QLocale::LastScript; ++i, c += 4) {
134 if (c0 == c[0] && c1 == c[1] && c2 == c[2] && c3 == c[3])
135 return QLocale::Script(i);
137 return QLocale::AnyScript;
140 QLocale::Country QLocalePrivate::codeToCountry(const QString &code)
142 int len = code.length();
143 if (len != 2 && len != 3)
144 return QLocale::AnyCountry;
145 ushort uc1 = len-- > 0 ? code[0].toUpper().unicode() : 0;
146 ushort uc2 = len-- > 0 ? code[1].toUpper().unicode() : 0;
147 ushort uc3 = len-- > 0 ? code[2].toUpper().unicode() : 0;
149 const unsigned char *c = country_code_list;
150 for (; *c != 0; c += 3) {
151 if (uc1 == c[0] && uc2 == c[1] && uc3 == c[2])
152 return QLocale::Country((c - country_code_list)/3);
155 return QLocale::AnyCountry;
158 QString QLocalePrivate::languageCode() const
160 if (m_language_id == QLocale::AnyLanguage)
162 if (m_language_id == QLocale::C)
163 return QLatin1String("C");
165 const unsigned char *c = language_code_list + 3*(uint(m_language_id));
167 QString code(c[2] == 0 ? 2 : 3, Qt::Uninitialized);
169 code[0] = ushort(c[0]);
170 code[1] = ushort(c[1]);
172 code[2] = ushort(c[2]);
177 QString QLocalePrivate::scriptCode() const
179 if (m_script_id == QLocale::AnyScript || m_script_id > QLocale::LastScript)
181 const unsigned char *c = script_code_list + 4*(uint(m_script_id));
182 return QString::fromLatin1((const char *)c, 4);
185 QString QLocalePrivate::countryCode() const
187 if (m_country_id == QLocale::AnyCountry)
190 const unsigned char *c = country_code_list + 3*(uint(m_country_id));
192 QString code(c[2] == 0 ? 2 : 3, Qt::Uninitialized);
194 code[0] = ushort(c[0]);
195 code[1] = ushort(c[1]);
197 code[2] = ushort(c[2]);
202 QString QLocalePrivate::bcp47Name() const
204 if (m_language_id == QLocale::AnyLanguage)
206 if (m_language_id == QLocale::C)
207 return QLatin1String("C");
208 const unsigned char *lang = language_code_list + 3*(uint(m_language_id));
209 const unsigned char *script =
210 (m_script_id != QLocale::AnyScript ? script_code_list + 4*(uint(m_script_id)) : 0);
211 const unsigned char *country =
212 (m_country_id != QLocale::AnyCountry ? country_code_list + 3*(uint(m_country_id)) : 0);
213 char len = (lang[2] != 0 ? 3 : 2) + (script ? 4+1 : 0) + (country ? (country[2] != 0 ? 3 : 2)+1 : 0);
214 QString name(len, Qt::Uninitialized);
215 QChar *uc = name.data();
216 *uc++ = ushort(lang[0]);
217 *uc++ = ushort(lang[1]);
219 *uc++ = ushort(lang[2]);
221 *uc++ = QLatin1Char('-');
222 *uc++ = ushort(script[0]);
223 *uc++ = ushort(script[1]);
224 *uc++ = ushort(script[2]);
225 *uc++ = ushort(script[3]);
228 *uc++ = QLatin1Char('-');
229 *uc++ = ushort(country[0]);
230 *uc++ = ushort(country[1]);
232 *uc++ = ushort(country[2]);
237 const QLocalePrivate *QLocalePrivate::findLocale(QLocale::Language language, QLocale::Script script, QLocale::Country country)
239 const unsigned language_id = language;
240 const unsigned script_id = script;
241 const unsigned country_id = country;
243 uint idx = locale_index[language_id];
245 const QLocalePrivate *d = locale_data + idx;
247 if (idx == 0) // default language has no associated country
250 if (script == QLocale::AnyScript && country == QLocale::AnyCountry)
253 Q_ASSERT(d->languageId() == language_id);
255 if (country == QLocale::AnyCountry) {
256 while (d->m_language_id == language_id && d->m_script_id != script_id)
258 if (d->m_language_id == language_id && d->m_script_id == script_id)
260 } else if (script == QLocale::AnyScript) {
261 while (d->m_language_id == language_id) {
262 if (d->m_script_id == script_id && d->m_country_id == country_id)
267 // both script and country are explicitly specified
268 while (d->m_language_id == language_id) {
269 if (d->m_script_id == script_id && d->m_country_id == country_id)
275 return locale_data + idx;
278 static bool parse_locale_tag(const QString &input, int &i, QString *result, const QString &separators)
280 *result = QString(8, Qt::Uninitialized); // worst case according to BCP47
281 QChar *pch = result->data();
282 const QChar *uc = input.data() + i;
283 const int l = input.length();
285 for (; i < l && size < 8; ++i, ++size) {
286 if (separators.contains(*uc))
288 if (! ((uc->unicode() >= 'a' && uc->unicode() <= 'z') ||
289 (uc->unicode() >= 'A' && uc->unicode() <= 'Z') ||
290 (uc->unicode() >= '0' && uc->unicode() <= '9')) ) // latin only
294 result->truncate(size);
298 bool qt_splitLocaleName(const QString &name, QString &lang, QString &script, QString &cntry)
300 const int length = name.length();
302 lang = script = cntry = QString();
304 const QString separators = QLatin1String("_-.@");
305 enum ParserState { NoState, LangState, ScriptState, CountryState };
306 ParserState state = LangState;
307 for (int i = 0; i < length && state != NoState; ) {
309 if (!parse_locale_tag(name, i, &value, separators) ||value.isEmpty())
311 QChar sep = i < length ? name.at(i) : QChar();
314 if (!sep.isNull() && !separators.contains(sep)) {
320 // just language was specified
327 QString scripts = QString::fromLatin1((const char *)script_code_list, sizeof(script_code_list));
328 if (value.length() == 4 && scripts.indexOf(value) % 4 == 0) {
329 // script name is always 4 characters
331 state = CountryState;
333 // it wasn't a script, maybe it is a country then?
345 qWarning("QLocale: This should never happen");
350 return lang.length() == 2 || lang.length() == 3;
353 void QLocalePrivate::getLangAndCountry(const QString &name, QLocale::Language &lang,
354 QLocale::Script &script, QLocale::Country &cntry)
357 script = QLocale::AnyScript;
358 cntry = QLocale::AnyCountry;
363 if (!qt_splitLocaleName(name, lang_code, script_code, cntry_code))
366 lang = QLocalePrivate::codeToLanguage(lang_code);
367 if (lang == QLocale::C)
369 script = QLocalePrivate::codeToScript(script_code);
370 cntry = QLocalePrivate::codeToCountry(cntry_code);
373 static const QLocalePrivate *findLocale(const QString &name)
375 QLocale::Language lang;
376 QLocale::Script script;
377 QLocale::Country cntry;
378 QLocalePrivate::getLangAndCountry(name, lang, script, cntry);
380 return QLocalePrivate::findLocale(lang, script, cntry);
383 QString qt_readEscapedFormatString(const QString &format, int *idx)
387 Q_ASSERT(format.at(i) == QLatin1Char('\''));
389 if (i == format.size())
391 if (format.at(i).unicode() == '\'') { // "''" outside of a quoted stirng
393 return QLatin1String("'");
398 while (i < format.size()) {
399 if (format.at(i).unicode() == '\'') {
400 if (i + 1 < format.size() && format.at(i + 1).unicode() == '\'') {
401 // "''" inside of a quoted string
402 result.append(QLatin1Char('\''));
408 result.append(format.at(i++));
411 if (i < format.size())
417 int qt_repeatCount(const QString &s, int i)
421 while (j < s.size() && s.at(j) == c)
426 static const QLocalePrivate *default_lp = 0;
427 static uint default_number_options = 0;
429 #ifndef QT_NO_SYSTEMLOCALE
432 /******************************************************************************
433 ** Default system locale behavior
437 Constructs a QSystemLocale object. The constructor will automatically
438 install this object as the system locale and remove any earlier installed
441 QSystemLocale::QSystemLocale()
443 delete _systemLocale;
444 _systemLocale = this;
447 system_lp->m_language_id = 0;
451 QSystemLocale::QSystemLocale(bool)
457 QSystemLocale::~QSystemLocale()
459 if (_systemLocale == this) {
463 system_lp->m_language_id = 0;
467 static const QSystemLocale *systemLocale()
470 return _systemLocale;
471 return QSystemLocale_globalSystemLocale();
474 void QLocalePrivate::updateSystemPrivate()
476 const QSystemLocale *sys_locale = systemLocale();
478 system_lp = globalLocalePrivate();
480 // tell the object that the system locale has changed.
481 sys_locale->query(QSystemLocale::LocaleChanged, QVariant());
483 *system_lp = *sys_locale->fallbackLocale().d();
485 QVariant res = sys_locale->query(QSystemLocale::LanguageId, QVariant());
487 system_lp->m_language_id = res.toInt();
488 system_lp->m_script_id = QLocale::AnyScript; // default for compatibility
490 res = sys_locale->query(QSystemLocale::CountryId, QVariant());
492 system_lp->m_country_id = res.toInt();
493 system_lp->m_script_id = QLocale::AnyScript; // default for compatibility
495 res = sys_locale->query(QSystemLocale::ScriptId, QVariant());
497 system_lp->m_script_id = res.toInt();
499 res = sys_locale->query(QSystemLocale::DecimalPoint, QVariant());
501 system_lp->m_decimal = res.toString().at(0).unicode();
503 res = sys_locale->query(QSystemLocale::GroupSeparator, QVariant());
505 system_lp->m_group = res.toString().at(0).unicode();
507 res = sys_locale->query(QSystemLocale::ZeroDigit, QVariant());
509 system_lp->m_zero = res.toString().at(0).unicode();
511 res = sys_locale->query(QSystemLocale::NegativeSign, QVariant());
513 system_lp->m_minus = res.toString().at(0).unicode();
515 res = sys_locale->query(QSystemLocale::PositiveSign, QVariant());
517 system_lp->m_plus = res.toString().at(0).unicode();
521 qt_initIcu(system_lp->bcp47Name());
527 static const QLocalePrivate *systemPrivate()
529 #ifndef QT_NO_SYSTEMLOCALE
530 // copy over the information from the fallback locale and modify
531 if (!system_lp || system_lp->m_language_id == 0)
532 QLocalePrivate::updateSystemPrivate();
540 static const QLocalePrivate *defaultPrivate()
543 default_lp = systemPrivate();
547 static QString getLocaleListData(const ushort *data, int size, int index)
549 static const ushort separator = ';';
550 while (index && size > 0) {
551 while (*data != separator)
557 const ushort *end = data;
558 while (size > 0 && *end != separator)
562 return QString::fromRawData(reinterpret_cast<const QChar*>(data), end-data);
565 static inline QString getLocaleData(const ushort *data, int size)
567 return size ? QString::fromRawData(reinterpret_cast<const QChar*>(data), size) : QString();
571 #ifndef QT_NO_DATASTREAM
572 QDataStream &operator<<(QDataStream &ds, const QLocale &l)
578 QDataStream &operator>>(QDataStream &ds, QLocale &l)
585 #endif // QT_NO_DATASTREAM
588 static const int locale_data_size = sizeof(locale_data)/sizeof(QLocalePrivate) - 1;
590 static const QLocalePrivate *dataPointerHelper(quint16 index)
592 #ifndef QT_NO_SYSTEMLOCALE
593 Q_ASSERT(index <= locale_data_size);
594 if (index == locale_data_size)
597 Q_ASSERT(index < locale_data_size);
600 return &locale_data[index];
603 static quint16 localePrivateIndex(const QLocalePrivate *p)
605 #ifndef QT_NO_SYSTEMLOCALE
606 Q_ASSERT((p >= locale_data && p - locale_data < locale_data_size)
607 || (p != 0 && p == system_lp));
608 quint16 index = p == system_lp ? locale_data_size : p - locale_data;
610 Q_ASSERT(p >= locale_data && p - locale_data < locale_data_size);
611 quint16 index = p - locale_data;
618 Constructs a QLocale object with the specified \a name,
620 "language[_script][_country][.codeset][@modifier]" or "C", where:
623 \li language is a lowercase, two-letter, ISO 639 language code,
624 \li script is a titlecase, four-letter, ISO 15924 script code,
625 \li country is an uppercase, two- or three-letter, ISO 3166 country code (also "419" as defined by United Nations),
626 \li and codeset and modifier are ignored.
629 The separator can be either underscore or a minus sign.
631 If the string violates the locale format, or language is not
632 a valid ISO 369 code, the "C" locale is used instead. If country
633 is not present, or is not a valid ISO 3166 code, the most
634 appropriate country is chosen for the specified language.
636 The language, script and country codes are converted to their respective
637 \c Language, \c Script and \c Country enums. After this conversion is
638 performed the constructor behaves exactly like QLocale(Country, Script,
641 This constructor is much slower than QLocale(Country, Script, Language).
646 QLocale::QLocale(const QString &name)
650 p.index = localePrivateIndex(findLocale(name));
654 Constructs a QLocale object initialized with the default locale. If
655 no default locale was set using setDefaultLocale(), this locale will
656 be the same as the one returned by system().
664 p.numberOptions = default_number_options;
665 p.index = localePrivateIndex(defaultPrivate());
669 Constructs a QLocale object with the specified \a language and \a
673 \li If the language/country pair is found in the database, it is used.
674 \li If the language is found but the country is not, or if the country
675 is \c AnyCountry, the language is used with the most
676 appropriate available country (for example, Germany for German),
677 \li If neither the language nor the country are found, QLocale
678 defaults to the default locale (see setDefault()).
681 The language and country that are actually used can be queried
682 using language() and country().
684 \sa setDefault() language() country()
687 QLocale::QLocale(Language language, Country country)
690 const QLocalePrivate *d = QLocalePrivate::findLocale(language, QLocale::AnyScript, country);
692 // If not found, should default to system
693 if (d->languageId() == QLocale::C && language != QLocale::C) {
694 p.numberOptions = default_number_options;
695 p.index = localePrivateIndex(defaultPrivate());
698 p.index = localePrivateIndex(d);
705 Constructs a QLocale object with the specified \a language, \a script and
709 \li If the language/script/country is found in the database, it is used.
710 \li If both \a script is AnyScript and \a country is AnyCountry, the
711 language is used with the most appropriate available script and country
712 (for example, Germany for German),
713 \li If either \a script is AnyScript or \a country is AnyCountry, the
714 language is used with the first locale that matches the given \a script
716 \li If neither the language nor the country are found, QLocale
717 defaults to the default locale (see setDefault()).
720 The language, script and country that are actually used can be queried
721 using language(), script() and country().
723 \sa setDefault() language() script() country()
726 QLocale::QLocale(Language language, Script script, Country country)
729 const QLocalePrivate *d = QLocalePrivate::findLocale(language, script, country);
731 // If not found, should default to system
732 if (d->languageId() == QLocale::C && language != QLocale::C) {
733 p.numberOptions = default_number_options;
734 p.index = localePrivateIndex(defaultPrivate());
737 p.index = localePrivateIndex(d);
742 Constructs a QLocale object as a copy of \a other.
745 QLocale::QLocale(const QLocale &other)
750 const QLocalePrivate *QLocale::d() const
752 return dataPointerHelper(p.index);
756 Assigns \a other to this QLocale object and returns a reference
757 to this QLocale object.
760 QLocale &QLocale::operator=(const QLocale &other)
766 bool QLocale::operator==(const QLocale &other) const
768 return d() == other.d() && numberOptions() == other.numberOptions();
771 bool QLocale::operator!=(const QLocale &other) const
773 return d() != other.d() || numberOptions() != other.numberOptions();
779 Sets the \a options related to number conversions for this
782 void QLocale::setNumberOptions(NumberOptions options)
784 p.numberOptions = options;
790 Returns the options related to number conversions for this
793 By default, no options are set for the standard locales.
795 QLocale::NumberOptions QLocale::numberOptions() const
797 return static_cast<NumberOption>(p.numberOptions);
803 Returns \a str quoted according to the current locale using the given
806 QString QLocale::quoteString(const QString &str, QuotationStyle style) const
808 return quoteString(&str, style);
816 QString QLocale::quoteString(const QStringRef &str, QuotationStyle style) const
818 #ifndef QT_NO_SYSTEMLOCALE
819 if (d() == systemPrivate()) {
821 if (style == QLocale::AlternateQuotation)
822 res = systemLocale()->query(QSystemLocale::StringToAlternateQuotation, QVariant::fromValue(str));
823 if (res.isNull() || style == QLocale::StandardQuotation)
824 res = systemLocale()->query(QSystemLocale::StringToStandardQuotation, QVariant::fromValue(str));
826 return res.toString();
830 if (style == QLocale::StandardQuotation)
831 return QChar(d()->m_quotation_start) % str % QChar(d()->m_quotation_end);
833 return QChar(d()->m_alternate_quotation_start) % str % QChar(d()->m_alternate_quotation_end);
839 Returns a string that represents a join of a given \a list of strings with
840 a separator defined by the locale.
842 QString QLocale::createSeparatedList(const QStringList &list) const
844 #ifndef QT_NO_SYSTEMLOCALE
845 if (d() == systemPrivate()) {
847 res = systemLocale()->query(QSystemLocale::ListToSeparatedString, QVariant::fromValue(list));
850 return res.toString();
854 const int size = list.size();
857 } else if (size == 2) {
858 QString format = getLocaleData(list_pattern_part_data + d()->m_list_pattern_part_two_idx, d()->m_list_pattern_part_two_size);
859 return format.arg(list.at(0), list.at(1));
860 } else if (size > 2) {
861 QString formatStart = getLocaleData(list_pattern_part_data + d()->m_list_pattern_part_start_idx, d()->m_list_pattern_part_start_size);
862 QString formatMid = getLocaleData(list_pattern_part_data + d()->m_list_pattern_part_mid_idx, d()->m_list_pattern_part_mid_size);
863 QString formatEnd = getLocaleData(list_pattern_part_data + d()->m_list_pattern_part_end_idx, d()->m_list_pattern_part_end_size);
864 QString result = formatStart.arg(list.at(0), list.at(1));
865 for (int i = 2; i < size - 1; ++i)
866 result = formatMid.arg(result, list.at(i));
867 result = formatEnd.arg(result, list.at(size - 1));
877 Sets the global default locale to \a locale. These
878 values are used when a QLocale object is constructed with
879 no arguments. If this function is not called, the system's
882 \warning In a multithreaded application, the default locale
883 should be set at application startup, before any non-GUI threads
889 void QLocale::setDefault(const QLocale &locale)
891 default_lp = locale.d();
892 default_number_options = locale.numberOptions();
895 qt_initIcu(locale.bcp47Name());
900 Returns the language of this locale.
902 \sa script(), country(), languageToString(), bcp47Name()
904 QLocale::Language QLocale::language() const
906 return Language(d()->languageId());
912 Returns the script of this locale.
914 \sa language(), country(), languageToString(), scriptToString(), bcp47Name()
916 QLocale::Script QLocale::script() const
918 return Script(d()->m_script_id);
922 Returns the country of this locale.
924 \sa language(), script(), countryToString(), bcp47Name()
926 QLocale::Country QLocale::country() const
928 return Country(d()->countryId());
932 Returns the language and country of this locale as a
933 string of the form "language_country", where
934 language is a lowercase, two-letter ISO 639 language code,
935 and country is an uppercase, two- or three-letter ISO 3166 country code.
937 Note that even if QLocale object was constructed with an explicit script,
938 name() will not contain it for compatibility reasons. Use bcp47Name() instead
939 if you need a full locale name.
941 \sa QLocale(), language(), script(), country(), bcp47Name()
944 QString QLocale::name() const
946 Language l = language();
948 QString result = d()->languageCode();
953 Country c = country();
957 result.append(QLatin1Char('_'));
958 result.append(d()->countryCode());
966 Returns the dash-separated language, script and country (and possibly other BCP47 fields)
967 of this locale as a string.
969 Unlike the uiLanguages() the returned value of the bcp47Name() represents
970 the locale name of the QLocale data but not the language the user-interface
973 This function tries to conform the locale name to BCP47.
975 \sa language(), country(), script(), uiLanguages()
977 QString QLocale::bcp47Name() const
979 return d()->bcp47Name();
983 Returns a QString containing the name of \a language.
985 \sa countryToString(), scriptToString(), bcp47Name()
988 QString QLocale::languageToString(Language language)
990 if (uint(language) > uint(QLocale::LastLanguage))
991 return QLatin1String("Unknown");
992 return QLatin1String(language_name_list + language_name_index[language]);
996 Returns a QString containing the name of \a country.
998 \sa languageToString(), scriptToString(), country(), bcp47Name()
1001 QString QLocale::countryToString(Country country)
1003 if (uint(country) > uint(QLocale::LastCountry))
1004 return QLatin1String("Unknown");
1005 return QLatin1String(country_name_list + country_name_index[country]);
1011 Returns a QString containing the name of \a script.
1013 \sa languageToString(), countryToString(), script(), bcp47Name()
1015 QString QLocale::scriptToString(QLocale::Script script)
1017 if (uint(script) > uint(QLocale::LastScript))
1018 return QLatin1String("Unknown");
1019 return QLatin1String(script_name_list + script_name_index[script]);
1023 Returns the short int represented by the localized string \a s.
1025 If the conversion fails the function returns 0.
1027 If \a ok is not 0, failure is reported by setting *ok to false, and
1028 success by setting *ok to true.
1030 This function ignores leading and trailing whitespace.
1032 \sa toUShort(), toString()
1035 short QLocale::toShort(const QString &s, bool *ok) const
1037 qlonglong i = toLongLong(s, ok);
1038 if (i < SHRT_MIN || i > SHRT_MAX) {
1047 Returns the unsigned short int represented by the localized string \a s.
1049 If the conversion fails the function returns 0.
1051 If \a ok is not 0, failure is reported by setting *ok to false, and
1052 success by setting *ok to true.
1054 This function ignores leading and trailing whitespace.
1056 \sa toShort(), toString()
1059 ushort QLocale::toUShort(const QString &s, bool *ok) const
1061 qulonglong i = toULongLong(s, ok);
1062 if (i > USHRT_MAX) {
1071 Returns the int represented by the localized string \a s.
1073 If the conversion fails the function returns 0.
1075 If \a ok is not 0, failure is reported by setting *ok to false, and
1076 success by setting *ok to true.
1078 This function ignores leading and trailing whitespace.
1080 \sa toUInt(), toString()
1083 int QLocale::toInt(const QString &s, bool *ok) const
1085 qlonglong i = toLongLong(s, ok);
1086 if (i < INT_MIN || i > INT_MAX) {
1095 Returns the unsigned int represented by the localized string \a s.
1097 If the conversion fails the function returns 0.
1099 If \a ok is not 0, failure is reported by setting *ok to false, and
1100 success by setting *ok to true.
1102 This function ignores leading and trailing whitespace.
1104 \sa toInt(), toString()
1107 uint QLocale::toUInt(const QString &s, bool *ok) const
1109 qulonglong i = toULongLong(s, ok);
1119 Returns the long long int represented by the localized string \a s.
1121 If the conversion fails the function returns 0.
1123 If \a ok is not 0, failure is reported by setting *ok to false, and
1124 success by setting *ok to true.
1126 This function ignores leading and trailing whitespace.
1128 \sa toInt(), toULongLong(), toDouble(), toString()
1132 qlonglong QLocale::toLongLong(const QString &s, bool *ok) const
1134 QLocalePrivate::GroupSeparatorMode mode
1135 = p.numberOptions & RejectGroupSeparator
1136 ? QLocalePrivate::FailOnGroupSeparators
1137 : QLocalePrivate::ParseGroupSeparators;
1139 return d()->stringToLongLong(s, 10, ok, mode);
1143 Returns the unsigned long long int represented by the localized
1146 If the conversion fails the function returns 0.
1148 If \a ok is not 0, failure is reported by setting *ok to false, and
1149 success by setting *ok to true.
1151 This function ignores leading and trailing whitespace.
1153 \sa toLongLong(), toInt(), toDouble(), toString()
1156 qulonglong QLocale::toULongLong(const QString &s, bool *ok) const
1158 QLocalePrivate::GroupSeparatorMode mode
1159 = p.numberOptions & RejectGroupSeparator
1160 ? QLocalePrivate::FailOnGroupSeparators
1161 : QLocalePrivate::ParseGroupSeparators;
1163 return d()->stringToUnsLongLong(s, 10, ok, mode);
1167 Returns the float represented by the localized string \a s, or 0.0
1168 if the conversion failed.
1170 If \a ok is not 0, reports failure by setting
1171 *ok to false and success by setting *ok to true.
1173 This function ignores leading and trailing whitespace.
1175 \sa toDouble(), toInt(), toString()
1178 #define QT_MAX_FLOAT 3.4028234663852886e+38
1180 float QLocale::toFloat(const QString &s, bool *ok) const
1183 double d = toDouble(s, &myOk);
1184 if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
1195 Returns the double represented by the localized string \a s, or
1196 0.0 if the conversion failed.
1198 If \a ok is not 0, reports failure by setting
1199 *ok to false and success by setting *ok to true.
1201 Unlike QString::toDouble(), this function does not fall back to
1202 the "C" locale if the string cannot be interpreted in this
1205 \snippet doc/src/snippets/code/src_corelib_tools_qlocale.cpp 3
1207 Notice that the last conversion returns 1234.0, because '.' is the
1208 thousands group separator in the German locale.
1210 This function ignores leading and trailing whitespace.
1212 \sa toFloat(), toInt(), toString()
1215 double QLocale::toDouble(const QString &s, bool *ok) const
1217 QLocalePrivate::GroupSeparatorMode mode
1218 = p.numberOptions & RejectGroupSeparator
1219 ? QLocalePrivate::FailOnGroupSeparators
1220 : QLocalePrivate::ParseGroupSeparators;
1222 return d()->stringToDouble(s, ok, mode);
1226 Returns a localized string representation of \a i.
1231 QString QLocale::toString(qlonglong i) const
1233 int flags = p.numberOptions & OmitGroupSeparator
1235 : QLocalePrivate::ThousandsGroup;
1237 return d()->longLongToString(i, -1, 10, -1, flags);
1246 QString QLocale::toString(qulonglong i) const
1248 int flags = p.numberOptions & OmitGroupSeparator
1250 : QLocalePrivate::ThousandsGroup;
1252 return d()->unsLongLongToString(i, -1, 10, -1, flags);
1256 Returns a localized string representation of the given \a date in the
1257 specified \a format.
1258 If \a format is an empty string, an empty string is returned.
1261 QString QLocale::toString(const QDate &date, const QString &format) const
1263 return d()->dateTimeToString(format, &date, 0, this);
1267 Returns a localized string representation of the given \a date according
1268 to the specified \a format.
1271 QString QLocale::toString(const QDate &date, FormatType format) const
1273 if (!date.isValid())
1276 #ifndef QT_NO_SYSTEMLOCALE
1277 if (d() == systemPrivate()) {
1278 QVariant res = systemLocale()->query(format == LongFormat
1279 ? QSystemLocale::DateToStringLong : QSystemLocale::DateToStringShort,
1282 return res.toString();
1286 QString format_str = dateFormat(format);
1287 return toString(date, format_str);
1290 static bool timeFormatContainsAP(const QString &format)
1293 while (i < format.size()) {
1294 if (format.at(i).unicode() == '\'') {
1295 qt_readEscapedFormatString(format, &i);
1299 if (format.at(i).toLower().unicode() == 'a')
1307 static QString timeZone()
1309 #if defined(Q_OS_WINCE)
1310 TIME_ZONE_INFORMATION info;
1311 DWORD res = GetTimeZoneInformation(&info);
1312 if (res == TIME_ZONE_ID_UNKNOWN)
1314 return QString::fromWCharArray(info.StandardName);
1315 #elif defined(Q_OS_WIN)
1317 # if defined(_MSC_VER) && _MSC_VER >= 1400
1318 size_t returnSize = 0;
1319 char timeZoneName[512];
1320 if (_get_tzname(&returnSize, timeZoneName, 512, 1))
1322 return QString::fromLocal8Bit(timeZoneName);
1324 return QString::fromLocal8Bit(_tzname[1]);
1326 #elif defined(Q_OS_VXWORKS)
1330 return QString::fromLocal8Bit(tzname[1]);
1335 Returns a localized string representation of the given \a time according
1336 to the specified \a format.
1337 If \a format is an empty string, an empty string is returned.
1339 QString QLocale::toString(const QTime &time, const QString &format) const
1341 return d()->dateTimeToString(format, 0, &time, this);
1347 Returns a localized string representation of the given \a dateTime according
1348 to the specified \a format.
1349 If \a format is an empty string, an empty string is returned.
1352 QString QLocale::toString(const QDateTime &dateTime, const QString &format) const
1354 const QDate dt = dateTime.date();
1355 const QTime tm = dateTime.time();
1356 return d()->dateTimeToString(format, &dt, &tm, this);
1362 Returns a localized string representation of the given \a dateTime according
1363 to the specified \a format.
1366 QString QLocale::toString(const QDateTime &dateTime, FormatType format) const
1368 if (!dateTime.isValid())
1371 #ifndef QT_NO_SYSTEMLOCALE
1372 if (d() == systemPrivate()) {
1373 QVariant res = systemLocale()->query(format == LongFormat
1374 ? QSystemLocale::DateTimeToStringLong
1375 : QSystemLocale::DateTimeToStringShort,
1378 return res.toString();
1382 const QString format_str = dateTimeFormat(format);
1383 return toString(dateTime, format_str);
1388 Returns a localized string representation of the given \a time in the
1389 specified \a format.
1392 QString QLocale::toString(const QTime &time, FormatType format) const
1394 if (!time.isValid())
1397 #ifndef QT_NO_SYSTEMLOCALE
1398 if (d() == systemPrivate()) {
1399 QVariant res = systemLocale()->query(format == LongFormat
1400 ? QSystemLocale::TimeToStringLong : QSystemLocale::TimeToStringShort,
1403 return res.toString();
1407 QString format_str = timeFormat(format);
1408 return toString(time, format_str);
1414 Returns the date format used for the current locale.
1416 If \a format is LongFormat the format will be a long version.
1417 Otherwise it uses a shorter version.
1419 \sa QDate::toString(), QDate::fromString()
1422 QString QLocale::dateFormat(FormatType format) const
1424 #ifndef QT_NO_SYSTEMLOCALE
1425 if (d() == systemPrivate()) {
1426 QVariant res = systemLocale()->query(format == LongFormat
1427 ? QSystemLocale::DateFormatLong : QSystemLocale::DateFormatShort,
1430 return res.toString();
1437 idx = d()->m_long_date_format_idx;
1438 size = d()->m_long_date_format_size;
1441 idx = d()->m_short_date_format_idx;
1442 size = d()->m_short_date_format_size;
1445 return getLocaleData(date_format_data + idx, size);
1451 Returns the time format used for the current locale.
1453 If \a format is LongFormat the format will be a long version.
1454 Otherwise it uses a shorter version.
1456 \sa QTime::toString(), QTime::fromString()
1459 QString QLocale::timeFormat(FormatType format) const
1461 #ifndef QT_NO_SYSTEMLOCALE
1462 if (d() == systemPrivate()) {
1463 QVariant res = systemLocale()->query(format == LongFormat
1464 ? QSystemLocale::TimeFormatLong : QSystemLocale::TimeFormatShort,
1467 return res.toString();
1474 idx = d()->m_long_time_format_idx;
1475 size = d()->m_long_time_format_size;
1478 idx = d()->m_short_time_format_idx;
1479 size = d()->m_short_time_format_size;
1482 return getLocaleData(time_format_data + idx, size);
1488 Returns the date time format used for the current locale.
1490 If \a format is ShortFormat the format will be a short version.
1491 Otherwise it uses a longer version.
1493 \sa QDateTime::toString(), QDateTime::fromString()
1496 QString QLocale::dateTimeFormat(FormatType format) const
1498 #ifndef QT_NO_SYSTEMLOCALE
1499 if (d() == systemPrivate()) {
1500 QVariant res = systemLocale()->query(format == LongFormat
1501 ? QSystemLocale::DateTimeFormatLong
1502 : QSystemLocale::DateTimeFormatShort,
1504 if (!res.isNull()) {
1505 return res.toString();
1509 return dateFormat(format) + QLatin1Char(' ') + timeFormat(format);
1515 Parses the time string given in \a string and returns the
1516 time. The format of the time string is chosen according to the
1517 \a format parameter (see timeFormat()).
1519 If the time could not be parsed, returns an invalid time.
1521 \sa timeFormat(), toDate(), toDateTime(), QTime::fromString()
1523 #ifndef QT_NO_DATESTRING
1524 QTime QLocale::toTime(const QString &string, FormatType format) const
1526 return toTime(string, timeFormat(format));
1533 Parses the date string given in \a string and returns the
1534 date. The format of the date string is chosen according to the
1535 \a format parameter (see dateFormat()).
1537 If the date could not be parsed, returns an invalid date.
1539 \sa dateFormat(), toTime(), toDateTime(), QDate::fromString()
1541 #ifndef QT_NO_DATESTRING
1542 QDate QLocale::toDate(const QString &string, FormatType format) const
1544 return toDate(string, dateFormat(format));
1551 Parses the date/time string given in \a string and returns the
1552 time. The format of the date/time string is chosen according to the
1553 \a format parameter (see dateTimeFormat()).
1555 If the string could not be parsed, returns an invalid QDateTime.
1557 \sa dateTimeFormat(), toTime(), toDate(), QDateTime::fromString()
1560 #ifndef QT_NO_DATESTRING
1561 QDateTime QLocale::toDateTime(const QString &string, FormatType format) const
1563 return toDateTime(string, dateTimeFormat(format));
1570 Parses the time string given in \a string and returns the
1571 time. See QTime::fromString() for information on what is a valid
1574 If the time could not be parsed, returns an invalid time.
1576 \sa timeFormat(), toDate(), toDateTime(), QTime::fromString()
1578 #ifndef QT_NO_DATESTRING
1579 QTime QLocale::toTime(const QString &string, const QString &format) const
1582 #ifndef QT_BOOTSTRAPPED
1583 QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString);
1584 dt.defaultLocale = *this;
1585 if (dt.parseFormat(format))
1586 dt.fromString(string, 0, &time);
1598 Parses the date string given in \a string and returns the
1599 date. See QDate::fromString() for information on the expressions
1600 that can be used with this function.
1602 This function searches month names and the names of the days of
1603 the week in the current locale.
1605 If the date could not be parsed, returns an invalid date.
1607 \sa dateFormat(), toTime(), toDateTime(), QDate::fromString()
1609 #ifndef QT_NO_DATESTRING
1610 QDate QLocale::toDate(const QString &string, const QString &format) const
1613 #ifndef QT_BOOTSTRAPPED
1614 QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString);
1615 dt.defaultLocale = *this;
1616 if (dt.parseFormat(format))
1617 dt.fromString(string, &date, 0);
1629 Parses the date/time string given in \a string and returns the
1630 time. See QDateTime::fromString() for information on the expressions
1631 that can be used with this function.
1633 \note The month and day names used must be given in the user's local
1636 If the string could not be parsed, returns an invalid QDateTime.
1638 \sa dateTimeFormat(), toTime(), toDate(), QDateTime::fromString()
1640 #ifndef QT_NO_DATESTRING
1641 QDateTime QLocale::toDateTime(const QString &string, const QString &format) const
1643 #ifndef QT_BOOTSTRAPPED
1647 QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString);
1648 dt.defaultLocale = *this;
1649 if (dt.parseFormat(format) && dt.fromString(string, &date, &time))
1650 return QDateTime(date, time);
1655 return QDateTime(QDate(), QTime(-1, -1, -1));
1663 Returns the decimal point character of this locale.
1665 QChar QLocale::decimalPoint() const
1667 return d()->decimal();
1673 Returns the group separator character of this locale.
1675 QChar QLocale::groupSeparator() const
1677 return d()->group();
1683 Returns the percent character of this locale.
1685 QChar QLocale::percent() const
1687 return d()->percent();
1693 Returns the zero digit character of this locale.
1695 QChar QLocale::zeroDigit() const
1703 Returns the negative sign character of this locale.
1705 QChar QLocale::negativeSign() const
1707 return d()->minus();
1713 Returns the positive sign character of this locale.
1715 QChar QLocale::positiveSign() const
1723 Returns the exponential character of this locale.
1725 QChar QLocale::exponential() const
1727 return d()->exponential();
1730 static bool qIsUpper(char c)
1732 return c >= 'A' && c <= 'Z';
1735 static char qToLower(char c)
1737 if (c >= 'A' && c <= 'Z')
1738 return c - 'A' + 'a';
1746 \a f and \a prec have the same meaning as in QString::number(double, char, int).
1751 QString QLocale::toString(double i, char f, int prec) const
1753 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
1757 flags = QLocalePrivate::CapitalEorX;
1762 form = QLocalePrivate::DFDecimal;
1765 form = QLocalePrivate::DFExponent;
1768 form = QLocalePrivate::DFSignificantDigits;
1774 if (!(p.numberOptions & OmitGroupSeparator))
1775 flags |= QLocalePrivate::ThousandsGroup;
1776 return d()->doubleToString(i, prec, form, -1, flags);
1780 \fn QLocale QLocale::c()
1782 Returns a QLocale object initialized to the "C" locale.
1788 Returns a QLocale object initialized to the system locale.
1790 On Windows and Mac, this locale will use the decimal/grouping characters and date/time
1791 formats specified in the system configuration panel.
1796 QLocale QLocale::system()
1799 result.p.index = localePrivateIndex(systemPrivate());
1807 Returns a list of valid locale objects that match the given \a language, \a
1808 script and \a country.
1810 Getting a list of all locales:
1811 QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
1813 QList<QLocale> QLocale::matchingLocales(QLocale::Language language,
1814 QLocale::Script script,
1815 QLocale::Country country)
1817 if (uint(language) > QLocale::LastLanguage || uint(script) > QLocale::LastScript ||
1818 uint(country) > QLocale::LastCountry)
1819 return QList<QLocale>();
1821 QList<QLocale> result;
1822 const QLocalePrivate *d = locale_data;
1823 if (language == QLocale::AnyLanguage && script == QLocale::AnyScript && country == QLocale::AnyCountry)
1824 result.reserve(locale_data_size);
1825 if (language != QLocale::C)
1826 d += locale_index[language];
1827 while ( (d != locale_data + locale_data_size)
1828 && (language == QLocale::AnyLanguage || d->m_language_id == uint(language))) {
1829 QLocale locale(QLocale::C);
1830 locale.p.index = localePrivateIndex(d);
1831 result.append(locale);
1841 Returns the list of countries that have entires for \a language in Qt's locale
1842 database. If the result is an empty list, then \a language is not represented in
1843 Qt's locale database.
1845 \sa matchingLocales()
1847 QList<QLocale::Country> QLocale::countriesForLanguage(Language language)
1849 QList<Country> result;
1851 unsigned language_id = language;
1852 uint idx = locale_index[language_id];
1854 if (language == C) {
1855 result << AnyCountry;
1859 const QLocalePrivate *d = locale_data + idx;
1861 while (d->languageId() == language_id) {
1862 result << static_cast<Country>(d->countryId());
1872 Returns the localized name of \a month, in the format specified
1875 \sa dayName(), standaloneMonthName()
1877 QString QLocale::monthName(int month, FormatType type) const
1879 if (month < 1 || month > 12)
1882 #ifndef QT_NO_SYSTEMLOCALE
1883 if (d() == systemPrivate()) {
1884 QVariant res = systemLocale()->query(type == LongFormat
1885 ? QSystemLocale::MonthNameLong : QSystemLocale::MonthNameShort,
1888 return res.toString();
1894 case QLocale::LongFormat:
1895 idx = d()->m_long_month_names_idx;
1896 size = d()->m_long_month_names_size;
1898 case QLocale::ShortFormat:
1899 idx = d()->m_short_month_names_idx;
1900 size = d()->m_short_month_names_size;
1902 case QLocale::NarrowFormat:
1903 idx = d()->m_narrow_month_names_idx;
1904 size = d()->m_narrow_month_names_size;
1909 return getLocaleListData(months_data + idx, size, month - 1);
1915 Returns the localized name of \a month that is used as a
1916 standalone text, in the format specified by \a type.
1918 If the locale information doesn't specify the standalone month
1919 name then return value is the same as in monthName().
1921 \sa monthName(), standaloneDayName()
1923 QString QLocale::standaloneMonthName(int month, FormatType type) const
1925 if (month < 1 || month > 12)
1928 #ifndef QT_NO_SYSTEMLOCALE
1929 if (d() == systemPrivate()) {
1930 QVariant res = systemLocale()->query(type == LongFormat
1931 ? QSystemLocale::MonthNameLong : QSystemLocale::MonthNameShort,
1934 return res.toString();
1940 case QLocale::LongFormat:
1941 idx = d()->m_standalone_long_month_names_idx;
1942 size = d()->m_standalone_long_month_names_size;
1944 case QLocale::ShortFormat:
1945 idx = d()->m_standalone_short_month_names_idx;
1946 size = d()->m_standalone_short_month_names_size;
1948 case QLocale::NarrowFormat:
1949 idx = d()->m_standalone_narrow_month_names_idx;
1950 size = d()->m_standalone_narrow_month_names_size;
1955 QString name = getLocaleListData(months_data + idx, size, month - 1);
1957 return monthName(month, type);
1964 Returns the localized name of the \a day (where 1 represents
1965 Monday, 2 represents Tuesday and so on), in the format specified
1968 \sa monthName(), standaloneDayName()
1970 QString QLocale::dayName(int day, FormatType type) const
1972 if (day < 1 || day > 7)
1975 #ifndef QT_NO_SYSTEMLOCALE
1976 if (d() == systemPrivate()) {
1977 QVariant res = systemLocale()->query(type == LongFormat
1978 ? QSystemLocale::DayNameLong : QSystemLocale::DayNameShort,
1981 return res.toString();
1989 case QLocale::LongFormat:
1990 idx = d()->m_long_day_names_idx;
1991 size = d()->m_long_day_names_size;
1993 case QLocale::ShortFormat:
1994 idx = d()->m_short_day_names_idx;
1995 size = d()->m_short_day_names_size;
1997 case QLocale::NarrowFormat:
1998 idx = d()->m_narrow_day_names_idx;
1999 size = d()->m_narrow_day_names_size;
2004 return getLocaleListData(days_data + idx, size, day);
2010 Returns the localized name of the \a day (where 1 represents
2011 Monday, 2 represents Tuesday and so on) that is used as a
2012 standalone text, in the format specified by \a type.
2014 If the locale information does not specify the standalone day
2015 name then return value is the same as in dayName().
2017 \sa dayName(), standaloneMonthName()
2019 QString QLocale::standaloneDayName(int day, FormatType type) const
2021 if (day < 1 || day > 7)
2024 #ifndef QT_NO_SYSTEMLOCALE
2025 if (d() == systemPrivate()) {
2026 QVariant res = systemLocale()->query(type == LongFormat
2027 ? QSystemLocale::DayNameLong : QSystemLocale::DayNameShort,
2030 return res.toString();
2038 case QLocale::LongFormat:
2039 idx = d()->m_standalone_long_day_names_idx;
2040 size = d()->m_standalone_long_day_names_size;
2042 case QLocale::ShortFormat:
2043 idx = d()->m_standalone_short_day_names_idx;
2044 size = d()->m_standalone_short_day_names_size;
2046 case QLocale::NarrowFormat:
2047 idx = d()->m_standalone_narrow_day_names_idx;
2048 size = d()->m_standalone_narrow_day_names_size;
2053 QString name = getLocaleListData(days_data + idx, size, day);
2055 return dayName(day == 0 ? 7 : day, type);
2062 Returns the first day of the week according to the current locale.
2064 Qt::DayOfWeek QLocale::firstDayOfWeek() const
2066 #ifndef QT_NO_SYSTEMLOCALE
2067 if (d() == systemPrivate()) {
2068 QVariant res = systemLocale()->query(QSystemLocale::FirstDayOfWeek, QVariant());
2070 return static_cast<Qt::DayOfWeek>(res.toUInt());
2073 return static_cast<Qt::DayOfWeek>(d()->m_first_day_of_week);
2076 QLocale::MeasurementSystem QLocalePrivate::measurementSystem() const
2078 for (int i = 0; i < ImperialMeasurementSystemsCount; ++i) {
2079 if (ImperialMeasurementSystems[i].languageId == m_language_id
2080 && ImperialMeasurementSystems[i].countryId == m_country_id) {
2081 return ImperialMeasurementSystems[i].system;
2084 return QLocale::MetricSystem;
2090 Returns a list of days that are considered weekdays according to the current locale.
2092 QList<Qt::DayOfWeek> QLocale::weekdays() const
2094 #ifndef QT_NO_SYSTEMLOCALE
2095 if (d() == systemPrivate()) {
2096 QVariant res = systemLocale()->query(QSystemLocale::Weekdays, QVariant());
2098 return static_cast<QList<Qt::DayOfWeek> >(res.value<QList<Qt::DayOfWeek> >());
2101 QList<Qt::DayOfWeek> weekdays;
2102 quint16 weekendStart = d()->m_weekend_start;
2103 quint16 weekendEnd = d()->m_weekend_end;
2104 for (int day = Qt::Monday; day <= Qt::Sunday; day++) {
2105 if ((weekendEnd >= weekendStart && (day < weekendStart || day > weekendEnd)) ||
2106 (weekendEnd < weekendStart && (day > weekendEnd && day < weekendStart)))
2107 weekdays << static_cast<Qt::DayOfWeek>(day);
2115 Returns the measurement system for the locale.
2117 QLocale::MeasurementSystem QLocale::measurementSystem() const
2119 #ifndef QT_NO_SYSTEMLOCALE
2120 if (d() == systemPrivate()) {
2121 QVariant res = systemLocale()->query(QSystemLocale::MeasurementSystem, QVariant());
2123 return MeasurementSystem(res.toInt());
2127 return d()->measurementSystem();
2133 Returns the text direction of the language.
2135 Qt::LayoutDirection QLocale::textDirection() const
2137 Language lang = language();
2138 if (lang == QLocale::Arabic ||
2139 lang == QLocale::Hebrew ||
2140 lang == QLocale::Persian ||
2141 lang == QLocale::Urdu ||
2142 lang == QLocale::Syriac)
2143 return Qt::RightToLeft;
2145 return Qt::LeftToRight;
2151 Returns an uppercase copy of \a str.
2153 QString QLocale::toUpper(const QString &str) const
2158 if (qt_u_strToUpper(str, &result, *this))
2160 // else fall through and use Qt's toUpper
2163 return str.toUpper();
2169 Returns a lowercase copy of \a str.
2171 QString QLocale::toLower(const QString &str) const
2176 if (qt_u_strToLower(str, &result, *this))
2178 // else fall through and use Qt's toUpper
2181 return str.toLower();
2188 Returns the localized name of the "AM" suffix for times specified using
2189 the conventions of the 12-hour clock.
2193 QString QLocale::amText() const
2195 #ifndef QT_NO_SYSTEMLOCALE
2196 if (d() == systemPrivate()) {
2197 QVariant res = systemLocale()->query(QSystemLocale::AMText, QVariant());
2199 return res.toString();
2202 return getLocaleData(am_data + d()->m_am_idx, d()->m_am_size);
2208 Returns the localized name of the "PM" suffix for times specified using
2209 the conventions of the 12-hour clock.
2213 QString QLocale::pmText() const
2215 #ifndef QT_NO_SYSTEMLOCALE
2216 if (d() == systemPrivate()) {
2217 QVariant res = systemLocale()->query(QSystemLocale::PMText, QVariant());
2219 return res.toString();
2222 return getLocaleData(pm_data + d()->m_pm_idx, d()->m_pm_size);
2226 QString QLocalePrivate::dateTimeToString(const QString &format, const QDate *date, const QTime *time,
2227 const QLocale *q) const
2229 Q_ASSERT(date || time);
2230 if ((date && !date->isValid()) || (time && !time->isValid()))
2232 const bool format_am_pm = time && timeFormatContainsAP(format);
2234 enum { AM, PM } am_pm = AM;
2235 int hour12 = time ? time->hour() : -1;
2240 } else if (hour12 < 12) {
2242 } else if (hour12 == 12) {
2253 while (i < format.size()) {
2254 if (format.at(i).unicode() == '\'') {
2255 result.append(qt_readEscapedFormatString(format, &i));
2259 const QChar c = format.at(i);
2260 int repeat = qt_repeatCount(format, i);
2263 switch (c.unicode()) {
2268 else if (repeat >= 2)
2273 result.append(longLongToString(date->year()));
2276 result.append(longLongToString(date->year() % 100, -1, 10, 2,
2277 QLocalePrivate::ZeroPadded));
2288 repeat = qMin(repeat, 4);
2291 result.append(longLongToString(date->month()));
2294 result.append(longLongToString(date->month(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2297 result.append(q->monthName(date->month(), QLocale::ShortFormat));
2300 result.append(q->monthName(date->month(), QLocale::LongFormat));
2307 repeat = qMin(repeat, 4);
2310 result.append(longLongToString(date->day()));
2313 result.append(longLongToString(date->day(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2316 result.append(q->dayName(date->dayOfWeek(), QLocale::ShortFormat));
2319 result.append(q->dayName(date->dayOfWeek(), QLocale::LongFormat));
2328 if (!used && time) {
2329 switch (c.unicode()) {
2332 repeat = qMin(repeat, 2);
2333 const int hour = format_am_pm ? hour12 : time->hour();
2337 result.append(longLongToString(hour));
2340 result.append(longLongToString(hour, -1, 10, 2, QLocalePrivate::ZeroPadded));
2347 repeat = qMin(repeat, 2);
2350 result.append(longLongToString(time->hour()));
2353 result.append(longLongToString(time->hour(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2360 repeat = qMin(repeat, 2);
2363 result.append(longLongToString(time->minute()));
2366 result.append(longLongToString(time->minute(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2373 repeat = qMin(repeat, 2);
2376 result.append(longLongToString(time->second()));
2379 result.append(longLongToString(time->second(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2386 if (i + 1 < format.length() && format.at(i + 1).unicode() == 'p') {
2391 result.append(am_pm == AM ? q->amText().toLower() : q->pmText().toLower());
2396 if (i + 1 < format.length() && format.at(i + 1).unicode() == 'P') {
2401 result.append(am_pm == AM ? q->amText().toUpper() : q->pmText().toUpper());
2413 result.append(longLongToString(time->msec()));
2416 result.append(longLongToString(time->msec(), -1, 10, 3, QLocalePrivate::ZeroPadded));
2424 result.append(timeZone());
2431 result.append(QString(repeat, c));
2439 QString QLocalePrivate::doubleToString(double d,
2443 unsigned flags) const
2445 return QLocalePrivate::doubleToString(zero(), plus(), minus(), exponential(),
2447 d, precision, form, width, flags);
2450 QString QLocalePrivate::doubleToString(const QChar _zero, const QChar plus, const QChar minus,
2451 const QChar exponential, const QChar group, const QChar decimal,
2458 if (precision == -1)
2463 bool negative = false;
2464 bool special_number = false; // nan, +/-inf
2467 // Detect special numbers (nan, +/-inf)
2469 num_str = QString::fromLatin1("inf");
2470 special_number = true;
2472 } else if (qt_is_nan(d)) {
2473 num_str = QString::fromLatin1("nan");
2474 special_number = true;
2477 // Handle normal numbers
2478 if (!special_number) {
2482 #ifdef QT_QLOCALE_USES_FCVT
2484 if (form == DFDecimal) {
2485 digits = QLatin1String(fcvt(d, precision, &decpt, &sign));
2488 if (form == DFExponent)
2490 else if (form == DFSignificantDigits && pr == 0)
2492 digits = QLatin1String(ecvt(d, pr, &decpt, &sign));
2494 // Chop trailing zeros
2495 if (digits.length() > 0) {
2496 int last_nonzero_idx = digits.length() - 1;
2497 while (last_nonzero_idx > 0
2498 && digits.unicode()[last_nonzero_idx] == QLatin1Char('0'))
2500 digits.truncate(last_nonzero_idx + 1);
2507 if (form == DFDecimal)
2512 /* This next bit is a bit quirky. In DFExponent form, the precision
2513 is the number of digits after decpt. So that would suggest using
2514 mode=3 for qdtoa. But qdtoa behaves strangely when mode=3 and
2515 precision=0. So we get around this by using mode=2 and reasoning
2516 that we want precision+1 significant digits, since the decimal
2517 point in this mode is always after the first digit. */
2519 if (form == DFExponent)
2525 digits = QLatin1String(qdtoa(d, mode, pr, &decpt, &sign, &rve, &buff));
2533 #endif // QT_QLOCALE_USES_FCVT
2535 if (_zero.unicode() != '0') {
2536 ushort z = _zero.unicode() - '0';
2537 for (int i = 0; i < digits.length(); ++i)
2538 reinterpret_cast<ushort *>(digits.data())[i] += z;
2541 bool always_show_decpt = (flags & Alternate || flags & ForcePoint);
2544 num_str = exponentForm(_zero, decimal, exponential, group, plus, minus,
2545 digits, decpt, precision, PMDecimalDigits,
2550 num_str = decimalForm(_zero, decimal, group,
2551 digits, decpt, precision, PMDecimalDigits,
2552 always_show_decpt, flags & ThousandsGroup);
2555 case DFSignificantDigits: {
2556 PrecisionMode mode = (flags & Alternate) ?
2557 PMSignificantDigits : PMChopTrailingZeros;
2559 if (decpt != digits.length() && (decpt <= -4 || decpt > precision))
2560 num_str = exponentForm(_zero, decimal, exponential, group, plus, minus,
2561 digits, decpt, precision, mode,
2564 num_str = decimalForm(_zero, decimal, group,
2565 digits, decpt, precision, mode,
2566 always_show_decpt, flags & ThousandsGroup);
2571 negative = sign != 0 && !isZero(d);
2574 // pad with zeros. LeftAdjusted overrides this flag). Also, we don't
2575 // pad special numbers
2576 if (flags & QLocalePrivate::ZeroPadded
2577 && !(flags & QLocalePrivate::LeftAdjusted)
2578 && !special_number) {
2579 int num_pad_chars = width - num_str.length();
2580 // leave space for the sign
2582 || flags & QLocalePrivate::AlwaysShowSign
2583 || flags & QLocalePrivate::BlankBeforePositive)
2586 for (int i = 0; i < num_pad_chars; ++i)
2587 num_str.prepend(_zero);
2592 num_str.prepend(minus);
2593 else if (flags & QLocalePrivate::AlwaysShowSign)
2594 num_str.prepend(plus);
2595 else if (flags & QLocalePrivate::BlankBeforePositive)
2596 num_str.prepend(QLatin1Char(' '));
2598 if (flags & QLocalePrivate::CapitalEorX)
2599 num_str = num_str.toUpper();
2604 QString QLocalePrivate::longLongToString(qlonglong l, int precision,
2605 int base, int width,
2606 unsigned flags) const
2608 return QLocalePrivate::longLongToString(zero(), group(), plus(), minus(),
2609 l, precision, base, width, flags);
2612 QString QLocalePrivate::longLongToString(const QChar zero, const QChar group,
2613 const QChar plus, const QChar minus,
2614 qlonglong l, int precision,
2615 int base, int width,
2618 bool precision_not_specified = false;
2619 if (precision == -1) {
2620 precision_not_specified = true;
2624 bool negative = l < 0;
2626 // these are not supported by sprintf for octal and hex
2627 flags &= ~AlwaysShowSign;
2628 flags &= ~BlankBeforePositive;
2629 negative = false; // neither are negative numbers
2634 num_str = qlltoa(l, base, zero);
2636 num_str = qulltoa(l, base, zero);
2638 uint cnt_thousand_sep = 0;
2639 if (flags & ThousandsGroup && base == 10) {
2640 for (int i = num_str.length() - 3; i > 0; i -= 3) {
2641 num_str.insert(i, group);
2646 for (int i = num_str.length()/* - cnt_thousand_sep*/; i < precision; ++i)
2647 num_str.prepend(base == 10 ? zero : QChar::fromLatin1('0'));
2649 if ((flags & Alternate || flags & ShowBase)
2651 && (num_str.isEmpty() || num_str[0].unicode() != QLatin1Char('0')))
2652 num_str.prepend(QLatin1Char('0'));
2654 // LeftAdjusted overrides this flag ZeroPadded. sprintf only padds
2655 // when precision is not specified in the format string
2656 bool zero_padded = flags & ZeroPadded
2657 && !(flags & LeftAdjusted)
2658 && precision_not_specified;
2661 int num_pad_chars = width - num_str.length();
2663 // leave space for the sign
2665 || flags & AlwaysShowSign
2666 || flags & BlankBeforePositive)
2669 // leave space for optional '0x' in hex form
2670 if (base == 16 && (flags & Alternate || flags & ShowBase))
2672 // leave space for optional '0b' in binary form
2673 else if (base == 2 && (flags & Alternate || flags & ShowBase))
2676 for (int i = 0; i < num_pad_chars; ++i)
2677 num_str.prepend(base == 10 ? zero : QChar::fromLatin1('0'));
2680 if (flags & CapitalEorX)
2681 num_str = num_str.toUpper();
2683 if (base == 16 && (flags & Alternate || flags & ShowBase))
2684 num_str.prepend(QLatin1String(flags & UppercaseBase ? "0X" : "0x"));
2685 if (base == 2 && (flags & Alternate || flags & ShowBase))
2686 num_str.prepend(QLatin1String(flags & UppercaseBase ? "0B" : "0b"));
2690 num_str.prepend(minus);
2691 else if (flags & AlwaysShowSign)
2692 num_str.prepend(plus);
2693 else if (flags & BlankBeforePositive)
2694 num_str.prepend(QLatin1Char(' '));
2699 QString QLocalePrivate::unsLongLongToString(qulonglong l, int precision,
2700 int base, int width,
2701 unsigned flags) const
2703 return QLocalePrivate::unsLongLongToString(zero(), group(), plus(),
2704 l, precision, base, width, flags);
2707 QString QLocalePrivate::unsLongLongToString(const QChar zero, const QChar group,
2709 qulonglong l, int precision,
2710 int base, int width,
2713 bool precision_not_specified = false;
2714 if (precision == -1) {
2715 precision_not_specified = true;
2719 QString num_str = qulltoa(l, base, zero);
2721 uint cnt_thousand_sep = 0;
2722 if (flags & ThousandsGroup && base == 10) {
2723 for (int i = num_str.length() - 3; i > 0; i -=3) {
2724 num_str.insert(i, group);
2729 for (int i = num_str.length()/* - cnt_thousand_sep*/; i < precision; ++i)
2730 num_str.prepend(base == 10 ? zero : QChar::fromLatin1('0'));
2732 if ((flags & Alternate || flags & ShowBase)
2734 && (num_str.isEmpty() || num_str[0].unicode() != QLatin1Char('0')))
2735 num_str.prepend(QLatin1Char('0'));
2737 // LeftAdjusted overrides this flag ZeroPadded. sprintf only padds
2738 // when precision is not specified in the format string
2739 bool zero_padded = flags & ZeroPadded
2740 && !(flags & LeftAdjusted)
2741 && precision_not_specified;
2744 int num_pad_chars = width - num_str.length();
2746 // leave space for optional '0x' in hex form
2747 if (base == 16 && flags & Alternate)
2749 // leave space for optional '0b' in binary form
2750 else if (base == 2 && flags & Alternate)
2753 for (int i = 0; i < num_pad_chars; ++i)
2754 num_str.prepend(base == 10 ? zero : QChar::fromLatin1('0'));
2757 if (flags & CapitalEorX)
2758 num_str = num_str.toUpper();
2760 if (base == 16 && (flags & Alternate || flags & ShowBase))
2761 num_str.prepend(QLatin1String(flags & UppercaseBase ? "0X" : "0x"));
2762 else if (base == 2 && (flags & Alternate || flags & ShowBase))
2763 num_str.prepend(QLatin1String(flags & UppercaseBase ? "0B" : "0b"));
2766 if (flags & AlwaysShowSign)
2767 num_str.prepend(plus);
2768 else if (flags & BlankBeforePositive)
2769 num_str.prepend(QLatin1Char(' '));
2775 Converts a number in locale to its representation in the C locale.
2776 Only has to guarantee that a string that is a correct representation of
2777 a number will be converted. If junk is passed in, junk will be passed
2778 out and the error will be detected during the actual conversion to a
2779 number. We can't detect junk here, since we don't even know the base
2782 bool QLocalePrivate::numberToCLocale(const QString &num,
2783 GroupSeparatorMode group_sep_mode,
2784 CharBuff *result) const
2786 const QChar *uc = num.unicode();
2787 int l = num.length();
2791 while (idx < l && uc[idx].isSpace())
2797 const QChar in = uc[idx];
2799 char out = digitToCLocale(in);
2803 else if (in == percent())
2805 // for handling base-x numbers
2806 else if (in.unicode() >= 'A' && in.unicode() <= 'Z')
2807 out = in.toLower().toLatin1();
2808 else if (in.unicode() >= 'a' && in.unicode() <= 'z')
2809 out = in.toLatin1();
2814 result->append(out);
2819 // Check trailing whitespace
2820 for (; idx < l; ++idx) {
2821 if (!uc[idx].isSpace())
2825 result->append('\0');
2828 if (group_sep_mode == ParseGroupSeparators
2829 && !removeGroupSeparators(result))
2836 bool QLocalePrivate::validateChars(const QString &str, NumberMode numMode, QByteArray *buff,
2837 int decDigits) const
2840 buff->reserve(str.length());
2842 const bool scientific = numMode == DoubleScientificMode;
2843 bool lastWasE = false;
2844 bool lastWasDigit = false;
2846 int decPointCnt = 0;
2848 int decDigitCnt = 0;
2850 for (int i = 0; i < str.length(); ++i) {
2851 char c = digitToCLocale(str.at(i));
2853 if (c >= '0' && c <= '9') {
2854 if (numMode != IntegerMode) {
2855 // If a double has too many digits after decpt, it shall be Invalid.
2856 if (dec && decDigits != -1 && decDigits < ++decDigitCnt)
2859 lastWasDigit = true;
2863 if (numMode == IntegerMode) {
2864 // If an integer has a decimal point, it shall be Invalid.
2867 // If a double has more than one decimal point, it shall be Invalid.
2868 if (++decPointCnt > 1)
2871 // If a double with no decimal digits has a decimal point, it shall be
2875 #endif // On second thoughts, it shall be Valid.
2884 // If a scientific has a sign that's not at the beginning or after
2885 // an 'e', it shall be Invalid.
2886 if (i != 0 && !lastWasE)
2889 // If a non-scientific has a sign that's not at the beginning,
2890 // it shall be Invalid.
2897 //it can only be placed after a digit which is before the decimal point
2898 if (!lastWasDigit || decPointCnt > 0)
2904 // If a scientific has more than one 'e', it shall be Invalid.
2909 // If a non-scientific has an 'e', it shall be Invalid.
2915 // If it's not a valid digit, it shall be Invalid.
2918 lastWasDigit = false;
2921 lastWasE = c == 'e';
2929 double QLocalePrivate::stringToDouble(const QString &number, bool *ok,
2930 GroupSeparatorMode group_sep_mode) const
2933 if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
2934 group_sep_mode, &buff)) {
2939 return bytearrayToDouble(buff.constData(), ok);
2942 qlonglong QLocalePrivate::stringToLongLong(const QString &number, int base,
2943 bool *ok, GroupSeparatorMode group_sep_mode) const
2946 if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
2947 group_sep_mode, &buff)) {
2953 return bytearrayToLongLong(buff.constData(), base, ok);
2956 qulonglong QLocalePrivate::stringToUnsLongLong(const QString &number, int base,
2957 bool *ok, GroupSeparatorMode group_sep_mode) const
2960 if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
2961 group_sep_mode, &buff)) {
2967 return bytearrayToUnsLongLong(buff.constData(), base, ok);
2971 double QLocalePrivate::bytearrayToDouble(const char *num, bool *ok, bool *overflow)
2984 if (qstrcmp(num, "nan") == 0)
2987 if (qstrcmp(num, "+inf") == 0 || qstrcmp(num, "inf") == 0)
2990 if (qstrcmp(num, "-inf") == 0)
2995 double d = qstrtod(num, &endptr, &_ok);
2998 // the only way strtod can fail with *endptr != '\0' on a non-empty
2999 // input string is overflow
3003 *overflow = *endptr != '\0';
3007 if (*endptr != '\0') {
3008 // we stopped at a non-digit character after converting some digits
3023 qlonglong QLocalePrivate::bytearrayToLongLong(const char *num, int base, bool *ok, bool *overflow)
3036 qlonglong l = qstrtoll(num, &endptr, base, &_ok);
3041 if (overflow != 0) {
3042 // the only way qstrtoll can fail with *endptr != '\0' on a non-empty
3043 // input string is overflow
3044 *overflow = *endptr != '\0';
3049 if (*endptr != '\0') {
3050 // we stopped at a non-digit character after converting some digits
3065 qulonglong QLocalePrivate::bytearrayToUnsLongLong(const char *num, int base, bool *ok)
3069 qulonglong l = qstrtoull(num, &endptr, base, &_ok);
3071 if (!_ok || *endptr != '\0') {
3085 \enum QLocale::CurrencySymbolFormat
3087 Specifies the format of the currency symbol.
3089 \value CurrencyIsoCode a ISO-4217 code of the currency.
3090 \value CurrencySymbol a currency symbol.
3091 \value CurrencyDisplayName a user readable name of the currency.
3096 Returns a currency symbol according to the \a format.
3098 QString QLocale::currencySymbol(QLocale::CurrencySymbolFormat format) const
3100 #ifndef QT_NO_SYSTEMLOCALE
3101 if (d() == systemPrivate()) {
3102 QVariant res = systemLocale()->query(QSystemLocale::CurrencySymbol, format);
3104 return res.toString();
3109 case CurrencySymbol:
3110 idx = d()->m_currency_symbol_idx;
3111 size = d()->m_currency_symbol_size;
3112 return getLocaleData(currency_symbol_data + idx, size);
3113 case CurrencyDisplayName:
3114 idx = d()->m_currency_display_name_idx;
3115 size = d()->m_currency_display_name_size;
3116 return getLocaleListData(currency_display_name_data + idx, size, 0);
3117 case CurrencyIsoCode: {
3119 const QLocalePrivate *d = this->d();
3120 for (; len < 3; ++len)
3121 if (!d->m_currency_iso_code[len])
3123 return len ? QString::fromLatin1(d->m_currency_iso_code, len) : QString();
3132 Returns a localized string representation of \a value as a currency.
3133 If the \a symbol is provided it is used instead of the default currency symbol.
3135 \sa currencySymbol()
3137 QString QLocale::toCurrencyString(qlonglong value, const QString &symbol) const
3139 #ifndef QT_NO_SYSTEMLOCALE
3140 if (d() == systemPrivate()) {
3141 QSystemLocale::CurrencyToStringArgument arg(value, symbol);
3142 QVariant res = systemLocale()->query(QSystemLocale::CurrencyToString, QVariant::fromValue(arg));
3144 return res.toString();
3147 const QLocalePrivate *d = this->d();
3148 quint8 idx = d->m_currency_format_idx;
3149 quint8 size = d->m_currency_format_size;
3150 if (d->m_currency_negative_format_size && value < 0) {
3151 idx = d->m_currency_negative_format_idx;
3152 size = d->m_currency_negative_format_size;
3155 QString str = d->longLongToString(value);
3156 QString sym = symbol.isNull() ? currencySymbol() : symbol;
3158 sym = currencySymbol(QLocale::CurrencyIsoCode);
3159 QString format = getLocaleData(currency_format_data + idx, size);
3160 return format.arg(str, sym);
3167 QString QLocale::toCurrencyString(qulonglong value, const QString &symbol) const
3169 #ifndef QT_NO_SYSTEMLOCALE
3170 if (d() == systemPrivate()) {
3171 QSystemLocale::CurrencyToStringArgument arg(value, symbol);
3172 QVariant res = systemLocale()->query(QSystemLocale::CurrencyToString, QVariant::fromValue(arg));
3174 return res.toString();
3177 const QLocalePrivate *d = this->d();
3178 quint8 idx = d->m_currency_format_idx;
3179 quint8 size = d->m_currency_format_size;
3180 QString str = d->unsLongLongToString(value);
3181 QString sym = symbol.isNull() ? currencySymbol() : symbol;
3183 sym = currencySymbol(QLocale::CurrencyIsoCode);
3184 QString format = getLocaleData(currency_format_data + idx, size);
3185 return format.arg(str, sym);
3192 QString QLocale::toCurrencyString(double value, const QString &symbol) const
3194 #ifndef QT_NO_SYSTEMLOCALE
3195 if (d() == systemPrivate()) {
3196 QSystemLocale::CurrencyToStringArgument arg(value, symbol);
3197 QVariant res = systemLocale()->query(QSystemLocale::CurrencyToString, QVariant::fromValue(arg));
3199 return res.toString();
3202 const QLocalePrivate *d = this->d();
3203 quint8 idx = d->m_currency_format_idx;
3204 quint8 size = d->m_currency_format_size;
3205 if (d->m_currency_negative_format_size && value < 0) {
3206 idx = d->m_currency_negative_format_idx;
3207 size = d->m_currency_negative_format_size;
3210 QString str = d->doubleToString(value, d->m_currency_digits,
3211 QLocalePrivate::DFDecimal);
3212 QString sym = symbol.isNull() ? currencySymbol() : symbol;
3214 sym = currencySymbol(QLocale::CurrencyIsoCode);
3215 QString format = getLocaleData(currency_format_data + idx, size);
3216 return format.arg(str, sym);
3222 Returns an ordered list of locale names for translation purposes in
3225 The return value represents locale names that the user expects to see the
3228 Most like you do not need to use this function directly, but just pass the
3229 QLocale object to the QTranslator::load() function.
3231 The first item in the list is the most preferred one.
3233 \sa QTranslator, bcp47Name()
3235 QStringList QLocale::uiLanguages() const
3237 #ifndef QT_NO_SYSTEMLOCALE
3238 if (d() == systemPrivate()) {
3239 QVariant res = systemLocale()->query(QSystemLocale::UILanguages, QVariant());
3240 if (!res.isNull()) {
3241 QStringList result = res.toStringList();
3242 if (!result.isEmpty())
3247 return QStringList(bcp47Name());
3253 Returns a native name of the language for the locale. For example
3254 "Schwiizertüütsch" for Swiss-German locale.
3256 \sa nativeCountryName(), languageToString()
3258 QString QLocale::nativeLanguageName() const
3260 #ifndef QT_NO_SYSTEMLOCALE
3261 if (d() == systemPrivate()) {
3262 QVariant res = systemLocale()->query(QSystemLocale::NativeLanguageName, QVariant());
3264 return res.toString();
3267 return getLocaleData(endonyms_data + d()->m_language_endonym_idx, d()->m_language_endonym_size);
3273 Returns a native name of the country for the locale. For example
3274 "España" for Spanish/Spain locale.
3276 \sa nativeLanguageName(), countryToString()
3278 QString QLocale::nativeCountryName() const
3280 #ifndef QT_NO_SYSTEMLOCALE
3281 if (d() == systemPrivate()) {
3282 QVariant res = systemLocale()->query(QSystemLocale::NativeCountryName, QVariant());
3284 return res.toString();
3287 return getLocaleData(endonyms_data + d()->m_country_endonym_idx, d()->m_country_endonym_size);
3290 #ifndef QT_NO_DEBUG_STREAM
3291 QDebug operator<<(QDebug dbg, const QLocale &l)
3293 dbg.nospace() << "QLocale(" << qPrintable(QLocale::languageToString(l.language()))
3294 << ", " << qPrintable(QLocale::scriptToString(l.script()))
3295 << ", " << qPrintable(QLocale::countryToString(l.country())) << ')';