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 QLocaleData *system_data = 0;
82 Q_GLOBAL_STATIC(QLocaleData, globalLocaleData)
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_data->m_language_id == QLocale::AnyLanguage)
162 if (m_data->m_language_id == QLocale::C)
163 return QLatin1String("C");
165 const unsigned char *c = language_code_list + 3*(uint(m_data->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_data->m_script_id == QLocale::AnyScript || m_data->m_script_id > QLocale::LastScript)
181 const unsigned char *c = script_code_list + 4*(uint(m_data->m_script_id));
182 return QString::fromLatin1((const char *)c, 4);
185 QString QLocalePrivate::countryCode() const
187 if (m_data->m_country_id == QLocale::AnyCountry)
190 const unsigned char *c = country_code_list + 3*(uint(m_data->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_data->m_language_id == QLocale::AnyLanguage)
206 if (m_data->m_language_id == QLocale::C)
207 return QLatin1String("C");
208 const unsigned char *lang = language_code_list + 3*(uint(m_data->m_language_id));
209 const unsigned char *script =
210 (m_data->m_script_id != QLocale::AnyScript ? script_code_list + 4*(uint(m_data->m_script_id)) : 0);
211 const unsigned char *country =
212 (m_data->m_country_id != QLocale::AnyCountry ? country_code_list + 3*(uint(m_data->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 QLocaleData *QLocaleData::findLocaleData(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 QLocaleData *data = locale_data + idx;
247 if (idx == 0) // default language has no associated country
250 if (script == QLocale::AnyScript && country == QLocale::AnyCountry)
253 Q_ASSERT(data->m_language_id == language_id);
255 if (country == QLocale::AnyCountry) {
256 while (data->m_language_id == language_id && data->m_script_id != script_id)
258 if (data->m_language_id == language_id && data->m_script_id == script_id)
260 } else if (script == QLocale::AnyScript) {
261 while (data->m_language_id == language_id) {
262 if (data->m_script_id == script_id && data->m_country_id == country_id)
267 // both script and country are explicitly specified
268 while (data->m_language_id == language_id) {
269 if (data->m_script_id == script_id && data->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 QLocaleData *findLocaleData(const QString &name)
375 QLocale::Language lang;
376 QLocale::Script script;
377 QLocale::Country cntry;
378 QLocalePrivate::getLangAndCountry(name, lang, script, cntry);
380 return QLocaleData::findLocaleData(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 QLocaleData *default_data = 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_data->m_language_id = 0;
451 QSystemLocale::QSystemLocale(bool)
457 QSystemLocale::~QSystemLocale()
459 if (_systemLocale == this) {
463 system_data->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_data = globalLocaleData();
480 // tell the object that the system locale has changed.
481 sys_locale->query(QSystemLocale::LocaleChanged, QVariant());
483 *system_data = *sys_locale->fallbackUiLocale().d->m_data;
485 QVariant res = sys_locale->query(QSystemLocale::LanguageId, QVariant());
487 system_data->m_language_id = res.toInt();
488 system_data->m_script_id = QLocale::AnyScript; // default for compatibility
490 res = sys_locale->query(QSystemLocale::CountryId, QVariant());
492 system_data->m_country_id = res.toInt();
493 system_data->m_script_id = QLocale::AnyScript; // default for compatibility
495 res = sys_locale->query(QSystemLocale::ScriptId, QVariant());
497 system_data->m_script_id = res.toInt();
499 res = sys_locale->query(QSystemLocale::DecimalPoint, QVariant());
501 system_data->m_decimal = res.toString().at(0).unicode();
503 res = sys_locale->query(QSystemLocale::GroupSeparator, QVariant());
505 system_data->m_group = res.toString().at(0).unicode();
507 res = sys_locale->query(QSystemLocale::ZeroDigit, QVariant());
509 system_data->m_zero = res.toString().at(0).unicode();
511 res = sys_locale->query(QSystemLocale::NegativeSign, QVariant());
513 system_data->m_minus = res.toString().at(0).unicode();
515 res = sys_locale->query(QSystemLocale::PositiveSign, QVariant());
517 system_data->m_plus = res.toString().at(0).unicode();
521 qt_initIcu(sys_locale->fallbackUiLocale().bcp47Name());
527 static const QLocaleData *systemData()
529 #ifndef QT_NO_SYSTEMLOCALE
530 // copy over the information from the fallback locale and modify
531 if (!system_data || system_data->m_language_id == 0)
532 QLocalePrivate::updateSystemPrivate();
540 static const QLocaleData *defaultData()
543 default_data = systemData();
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(QLocaleData) - 1;
590 static const QLocaleData *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 localeDataIndex(const QLocaleData *p)
605 #ifndef QT_NO_SYSTEMLOCALE
606 Q_ASSERT((p >= locale_data && p - locale_data < locale_data_size)
607 || (p != 0 && p == system_data));
608 quint16 index = p == system_data ? 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)
647 : d(new QLocalePrivate())
649 d->m_numberOptions = 0;
650 d->m_index = localeDataIndex(findLocaleData(name));
651 d->m_data = dataPointerHelper(d->m_index);
655 Constructs a QLocale object initialized with the default locale. If
656 no default locale was set using setDefaultLocale(), this locale will
657 be the same as the one returned by system().
663 : d(new QLocalePrivate())
665 d->m_numberOptions = default_number_options;
666 d->m_index = localeDataIndex(defaultData());
667 d->m_data = dataPointerHelper(d->m_index);
671 Constructs a QLocale object with the specified \a language and \a
675 \li If the language/country pair is found in the database, it is used.
676 \li If the language is found but the country is not, or if the country
677 is \c AnyCountry, the language is used with the most
678 appropriate available country (for example, Germany for German),
679 \li If neither the language nor the country are found, QLocale
680 defaults to the default locale (see setDefault()).
683 The language and country that are actually used can be queried
684 using language() and country().
686 \sa setDefault(), language(), country()
689 QLocale::QLocale(Language language, Country country)
690 : d(new QLocalePrivate())
692 const QLocaleData *data = QLocaleData::findLocaleData(language, QLocale::AnyScript, country);
694 // If not found, should default to system
695 if (data->m_language_id == QLocale::C && language != QLocale::C) {
696 d->m_numberOptions = default_number_options;
697 d->m_index = localeDataIndex(defaultData());
699 d->m_numberOptions = 0;
700 d->m_index = localeDataIndex(data);
702 d->m_data = dataPointerHelper(d->m_index);
708 Constructs a QLocale object with the specified \a language, \a script and
712 \li If the language/script/country is found in the database, it is used.
713 \li If both \a script is AnyScript and \a country is AnyCountry, the
714 language is used with the most appropriate available script and country
715 (for example, Germany for German),
716 \li If either \a script is AnyScript or \a country is AnyCountry, the
717 language is used with the first locale that matches the given \a script
719 \li If neither the language nor the country are found, QLocale
720 defaults to the default locale (see setDefault()).
723 The language, script and country that are actually used can be queried
724 using language(), script() and country().
726 \sa setDefault(), language(), script(), country()
729 QLocale::QLocale(Language language, Script script, Country country)
730 : d(new QLocalePrivate())
732 const QLocaleData *data = QLocaleData::findLocaleData(language, script, country);
734 // If not found, should default to system
735 if (data->m_language_id == QLocale::C && language != QLocale::C) {
736 d->m_numberOptions = default_number_options;
737 d->m_index = localeDataIndex(defaultData());
739 d->m_numberOptions = 0;
740 d->m_index = localeDataIndex(data);
742 d->m_data = dataPointerHelper(d->m_index);
746 Constructs a QLocale object as a copy of \a other.
749 QLocale::QLocale(const QLocale &other)
763 Assigns \a other to this QLocale object and returns a reference
764 to this QLocale object.
767 QLocale &QLocale::operator=(const QLocale &other)
773 bool QLocale::operator==(const QLocale &other) const
775 return d->m_data == other.d->m_data && d->m_numberOptions == other.d->m_numberOptions;
778 bool QLocale::operator!=(const QLocale &other) const
780 return d->m_data != other.d->m_data || d->m_numberOptions != other.d->m_numberOptions;
786 Sets the \a options related to number conversions for this
789 void QLocale::setNumberOptions(NumberOptions options)
791 d->m_numberOptions = options;
797 Returns the options related to number conversions for this
800 By default, no options are set for the standard locales.
802 QLocale::NumberOptions QLocale::numberOptions() const
804 return static_cast<NumberOption>(d->m_numberOptions);
810 Returns \a str quoted according to the current locale using the given
813 QString QLocale::quoteString(const QString &str, QuotationStyle style) const
815 return quoteString(&str, style);
823 QString QLocale::quoteString(const QStringRef &str, QuotationStyle style) const
825 #ifndef QT_NO_SYSTEMLOCALE
826 if (d->m_data == systemData()) {
828 if (style == QLocale::AlternateQuotation)
829 res = systemLocale()->query(QSystemLocale::StringToAlternateQuotation, QVariant::fromValue(str));
830 if (res.isNull() || style == QLocale::StandardQuotation)
831 res = systemLocale()->query(QSystemLocale::StringToStandardQuotation, QVariant::fromValue(str));
833 return res.toString();
837 if (style == QLocale::StandardQuotation)
838 return QChar(d->m_data->m_quotation_start) % str % QChar(d->m_data->m_quotation_end);
840 return QChar(d->m_data->m_alternate_quotation_start) % str % QChar(d->m_data->m_alternate_quotation_end);
846 Returns a string that represents a join of a given \a list of strings with
847 a separator defined by the locale.
849 QString QLocale::createSeparatedList(const QStringList &list) const
851 #ifndef QT_NO_SYSTEMLOCALE
852 if (d->m_data == systemData()) {
854 res = systemLocale()->query(QSystemLocale::ListToSeparatedString, QVariant::fromValue(list));
857 return res.toString();
861 const int size = list.size();
864 } else if (size == 2) {
865 QString format = getLocaleData(list_pattern_part_data + d->m_data->m_list_pattern_part_two_idx, d->m_data->m_list_pattern_part_two_size);
866 return format.arg(list.at(0), list.at(1));
867 } else if (size > 2) {
868 QString formatStart = getLocaleData(list_pattern_part_data + d->m_data->m_list_pattern_part_start_idx, d->m_data->m_list_pattern_part_start_size);
869 QString formatMid = getLocaleData(list_pattern_part_data + d->m_data->m_list_pattern_part_mid_idx, d->m_data->m_list_pattern_part_mid_size);
870 QString formatEnd = getLocaleData(list_pattern_part_data + d->m_data->m_list_pattern_part_end_idx, d->m_data->m_list_pattern_part_end_size);
871 QString result = formatStart.arg(list.at(0), list.at(1));
872 for (int i = 2; i < size - 1; ++i)
873 result = formatMid.arg(result, list.at(i));
874 result = formatEnd.arg(result, list.at(size - 1));
884 Sets the global default locale to \a locale. These
885 values are used when a QLocale object is constructed with
886 no arguments. If this function is not called, the system's
889 \warning In a multithreaded application, the default locale
890 should be set at application startup, before any non-GUI threads
896 void QLocale::setDefault(const QLocale &locale)
898 default_data = locale.d->m_data;
899 default_number_options = locale.numberOptions();
902 qt_initIcu(locale.bcp47Name());
907 Returns the language of this locale.
909 \sa script(), country(), languageToString(), bcp47Name()
911 QLocale::Language QLocale::language() const
913 return Language(d->languageId());
919 Returns the script of this locale.
921 \sa language(), country(), languageToString(), scriptToString(), bcp47Name()
923 QLocale::Script QLocale::script() const
925 return Script(d->m_data->m_script_id);
929 Returns the country of this locale.
931 \sa language(), script(), countryToString(), bcp47Name()
933 QLocale::Country QLocale::country() const
935 return Country(d->countryId());
939 Returns the language and country of this locale as a
940 string of the form "language_country", where
941 language is a lowercase, two-letter ISO 639 language code,
942 and country is an uppercase, two- or three-letter ISO 3166 country code.
944 Note that even if QLocale object was constructed with an explicit script,
945 name() will not contain it for compatibility reasons. Use bcp47Name() instead
946 if you need a full locale name.
948 \sa QLocale(), language(), script(), country(), bcp47Name()
951 QString QLocale::name() const
953 Language l = language();
955 QString result = d->languageCode();
960 Country c = country();
964 result.append(QLatin1Char('_'));
965 result.append(d->countryCode());
973 Returns the dash-separated language, script and country (and possibly other BCP47 fields)
974 of this locale as a string.
976 Unlike the uiLanguages() the returned value of the bcp47Name() represents
977 the locale name of the QLocale data but not the language the user-interface
980 This function tries to conform the locale name to BCP47.
982 \sa language(), country(), script(), uiLanguages()
984 QString QLocale::bcp47Name() const
986 return d->bcp47Name();
990 Returns a QString containing the name of \a language.
992 \sa countryToString(), scriptToString(), bcp47Name()
995 QString QLocale::languageToString(Language language)
997 if (uint(language) > uint(QLocale::LastLanguage))
998 return QLatin1String("Unknown");
999 return QLatin1String(language_name_list + language_name_index[language]);
1003 Returns a QString containing the name of \a country.
1005 \sa languageToString(), scriptToString(), country(), bcp47Name()
1008 QString QLocale::countryToString(Country country)
1010 if (uint(country) > uint(QLocale::LastCountry))
1011 return QLatin1String("Unknown");
1012 return QLatin1String(country_name_list + country_name_index[country]);
1018 Returns a QString containing the name of \a script.
1020 \sa languageToString(), countryToString(), script(), bcp47Name()
1022 QString QLocale::scriptToString(QLocale::Script script)
1024 if (uint(script) > uint(QLocale::LastScript))
1025 return QLatin1String("Unknown");
1026 return QLatin1String(script_name_list + script_name_index[script]);
1030 Returns the short int represented by the localized string \a s.
1032 If the conversion fails the function returns 0.
1034 If \a ok is not 0, failure is reported by setting *ok to false, and
1035 success by setting *ok to true.
1037 This function ignores leading and trailing whitespace.
1039 \sa toUShort(), toString()
1042 short QLocale::toShort(const QString &s, bool *ok) const
1044 qlonglong i = toLongLong(s, ok);
1045 if (i < SHRT_MIN || i > SHRT_MAX) {
1054 Returns the unsigned short int represented by the localized string \a s.
1056 If the conversion fails the function returns 0.
1058 If \a ok is not 0, failure is reported by setting *ok to false, and
1059 success by setting *ok to true.
1061 This function ignores leading and trailing whitespace.
1063 \sa toShort(), toString()
1066 ushort QLocale::toUShort(const QString &s, bool *ok) const
1068 qulonglong i = toULongLong(s, ok);
1069 if (i > USHRT_MAX) {
1078 Returns the int represented by the localized string \a s.
1080 If the conversion fails the function returns 0.
1082 If \a ok is not 0, failure is reported by setting *ok to false, and
1083 success by setting *ok to true.
1085 This function ignores leading and trailing whitespace.
1087 \sa toUInt(), toString()
1090 int QLocale::toInt(const QString &s, bool *ok) const
1092 qlonglong i = toLongLong(s, ok);
1093 if (i < INT_MIN || i > INT_MAX) {
1102 Returns the unsigned int represented by the localized string \a s.
1104 If the conversion fails the function returns 0.
1106 If \a ok is not 0, failure is reported by setting *ok to false, and
1107 success by setting *ok to true.
1109 This function ignores leading and trailing whitespace.
1111 \sa toInt(), toString()
1114 uint QLocale::toUInt(const QString &s, bool *ok) const
1116 qulonglong i = toULongLong(s, ok);
1126 Returns the long long int represented by the localized string \a s.
1128 If the conversion fails the function returns 0.
1130 If \a ok is not 0, failure is reported by setting *ok to false, and
1131 success by setting *ok to true.
1133 This function ignores leading and trailing whitespace.
1135 \sa toInt(), toULongLong(), toDouble(), toString()
1139 qlonglong QLocale::toLongLong(const QString &s, bool *ok) const
1141 QLocalePrivate::GroupSeparatorMode mode
1142 = d->m_numberOptions & RejectGroupSeparator
1143 ? QLocalePrivate::FailOnGroupSeparators
1144 : QLocalePrivate::ParseGroupSeparators;
1146 return d->stringToLongLong(s, 10, ok, mode);
1150 Returns the unsigned long long int represented by the localized
1153 If the conversion fails the function returns 0.
1155 If \a ok is not 0, failure is reported by setting *ok to false, and
1156 success by setting *ok to true.
1158 This function ignores leading and trailing whitespace.
1160 \sa toLongLong(), toInt(), toDouble(), toString()
1163 qulonglong QLocale::toULongLong(const QString &s, bool *ok) const
1165 QLocalePrivate::GroupSeparatorMode mode
1166 = d->m_numberOptions & RejectGroupSeparator
1167 ? QLocalePrivate::FailOnGroupSeparators
1168 : QLocalePrivate::ParseGroupSeparators;
1170 return d->stringToUnsLongLong(s, 10, ok, mode);
1174 Returns the float represented by the localized string \a s, or 0.0
1175 if the conversion failed.
1177 If \a ok is not 0, reports failure by setting
1178 *ok to false and success by setting *ok to true.
1180 This function ignores leading and trailing whitespace.
1182 \sa toDouble(), toInt(), toString()
1185 #define QT_MAX_FLOAT 3.4028234663852886e+38
1187 float QLocale::toFloat(const QString &s, bool *ok) const
1190 double d = toDouble(s, &myOk);
1191 if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
1202 Returns the double represented by the localized string \a s, or
1203 0.0 if the conversion failed.
1205 If \a ok is not 0, reports failure by setting
1206 *ok to false and success by setting *ok to true.
1208 Unlike QString::toDouble(), this function does not fall back to
1209 the "C" locale if the string cannot be interpreted in this
1212 \snippet code/src_corelib_tools_qlocale.cpp 3
1214 Notice that the last conversion returns 1234.0, because '.' is the
1215 thousands group separator in the German locale.
1217 This function ignores leading and trailing whitespace.
1219 \sa toFloat(), toInt(), toString()
1222 double QLocale::toDouble(const QString &s, bool *ok) const
1224 QLocalePrivate::GroupSeparatorMode mode
1225 = d->m_numberOptions & RejectGroupSeparator
1226 ? QLocalePrivate::FailOnGroupSeparators
1227 : QLocalePrivate::ParseGroupSeparators;
1229 return d->stringToDouble(s, ok, mode);
1233 Returns a localized string representation of \a i.
1238 QString QLocale::toString(qlonglong i) const
1240 int flags = d->m_numberOptions & OmitGroupSeparator
1242 : QLocalePrivate::ThousandsGroup;
1244 return d->longLongToString(i, -1, 10, -1, flags);
1253 QString QLocale::toString(qulonglong i) const
1255 int flags = d->m_numberOptions & OmitGroupSeparator
1257 : QLocalePrivate::ThousandsGroup;
1259 return d->unsLongLongToString(i, -1, 10, -1, flags);
1263 Returns a localized string representation of the given \a date in the
1264 specified \a format.
1265 If \a format is an empty string, an empty string is returned.
1268 QString QLocale::toString(const QDate &date, const QString &format) const
1270 return d->dateTimeToString(format, &date, 0, this);
1274 Returns a localized string representation of the given \a date according
1275 to the specified \a format.
1278 QString QLocale::toString(const QDate &date, FormatType format) const
1280 if (!date.isValid())
1283 #ifndef QT_NO_SYSTEMLOCALE
1284 if (d->m_data == systemData()) {
1285 QVariant res = systemLocale()->query(format == LongFormat
1286 ? QSystemLocale::DateToStringLong : QSystemLocale::DateToStringShort,
1289 return res.toString();
1293 QString format_str = dateFormat(format);
1294 return toString(date, format_str);
1297 static bool timeFormatContainsAP(const QString &format)
1300 while (i < format.size()) {
1301 if (format.at(i).unicode() == '\'') {
1302 qt_readEscapedFormatString(format, &i);
1306 if (format.at(i).toLower().unicode() == 'a')
1314 static QString timeZone()
1316 #if defined(Q_OS_WINCE)
1317 TIME_ZONE_INFORMATION info;
1318 DWORD res = GetTimeZoneInformation(&info);
1319 if (res == TIME_ZONE_ID_UNKNOWN)
1321 return QString::fromWCharArray(info.StandardName);
1322 #elif defined(Q_OS_WIN)
1324 # if defined(_MSC_VER) && _MSC_VER >= 1400
1325 size_t returnSize = 0;
1326 char timeZoneName[512];
1327 if (_get_tzname(&returnSize, timeZoneName, 512, 1))
1329 return QString::fromLocal8Bit(timeZoneName);
1331 return QString::fromLocal8Bit(_tzname[1]);
1333 #elif defined(Q_OS_VXWORKS)
1337 return QString::fromLocal8Bit(tzname[1]);
1342 Returns a localized string representation of the given \a time according
1343 to the specified \a format.
1344 If \a format is an empty string, an empty string is returned.
1346 QString QLocale::toString(const QTime &time, const QString &format) const
1348 return d->dateTimeToString(format, 0, &time, this);
1354 Returns a localized string representation of the given \a dateTime according
1355 to the specified \a format.
1356 If \a format is an empty string, an empty string is returned.
1359 QString QLocale::toString(const QDateTime &dateTime, const QString &format) const
1361 const QDate dt = dateTime.date();
1362 const QTime tm = dateTime.time();
1363 return d->dateTimeToString(format, &dt, &tm, this);
1369 Returns a localized string representation of the given \a dateTime according
1370 to the specified \a format.
1373 QString QLocale::toString(const QDateTime &dateTime, FormatType format) const
1375 if (!dateTime.isValid())
1378 #ifndef QT_NO_SYSTEMLOCALE
1379 if (d->m_data == systemData()) {
1380 QVariant res = systemLocale()->query(format == LongFormat
1381 ? QSystemLocale::DateTimeToStringLong
1382 : QSystemLocale::DateTimeToStringShort,
1385 return res.toString();
1389 const QString format_str = dateTimeFormat(format);
1390 return toString(dateTime, format_str);
1395 Returns a localized string representation of the given \a time in the
1396 specified \a format.
1399 QString QLocale::toString(const QTime &time, FormatType format) const
1401 if (!time.isValid())
1404 #ifndef QT_NO_SYSTEMLOCALE
1405 if (d->m_data == systemData()) {
1406 QVariant res = systemLocale()->query(format == LongFormat
1407 ? QSystemLocale::TimeToStringLong : QSystemLocale::TimeToStringShort,
1410 return res.toString();
1414 QString format_str = timeFormat(format);
1415 return toString(time, format_str);
1421 Returns the date format used for the current locale.
1423 If \a format is LongFormat the format will be a long version.
1424 Otherwise it uses a shorter version.
1426 \sa QDate::toString(), QDate::fromString()
1429 QString QLocale::dateFormat(FormatType format) const
1431 #ifndef QT_NO_SYSTEMLOCALE
1432 if (d->m_data == systemData()) {
1433 QVariant res = systemLocale()->query(format == LongFormat
1434 ? QSystemLocale::DateFormatLong : QSystemLocale::DateFormatShort,
1437 return res.toString();
1444 idx = d->m_data->m_long_date_format_idx;
1445 size = d->m_data->m_long_date_format_size;
1448 idx = d->m_data->m_short_date_format_idx;
1449 size = d->m_data->m_short_date_format_size;
1452 return getLocaleData(date_format_data + idx, size);
1458 Returns the time format used for the current locale.
1460 If \a format is LongFormat the format will be a long version.
1461 Otherwise it uses a shorter version.
1463 \sa QTime::toString(), QTime::fromString()
1466 QString QLocale::timeFormat(FormatType format) const
1468 #ifndef QT_NO_SYSTEMLOCALE
1469 if (d->m_data == systemData()) {
1470 QVariant res = systemLocale()->query(format == LongFormat
1471 ? QSystemLocale::TimeFormatLong : QSystemLocale::TimeFormatShort,
1474 return res.toString();
1481 idx = d->m_data->m_long_time_format_idx;
1482 size = d->m_data->m_long_time_format_size;
1485 idx = d->m_data->m_short_time_format_idx;
1486 size = d->m_data->m_short_time_format_size;
1489 return getLocaleData(time_format_data + idx, size);
1495 Returns the date time format used for the current locale.
1497 If \a format is ShortFormat the format will be a short version.
1498 Otherwise it uses a longer version.
1500 \sa QDateTime::toString(), QDateTime::fromString()
1503 QString QLocale::dateTimeFormat(FormatType format) const
1505 #ifndef QT_NO_SYSTEMLOCALE
1506 if (d->m_data == systemData()) {
1507 QVariant res = systemLocale()->query(format == LongFormat
1508 ? QSystemLocale::DateTimeFormatLong
1509 : QSystemLocale::DateTimeFormatShort,
1511 if (!res.isNull()) {
1512 return res.toString();
1516 return dateFormat(format) + QLatin1Char(' ') + timeFormat(format);
1522 Parses the time string given in \a string and returns the
1523 time. The format of the time string is chosen according to the
1524 \a format parameter (see timeFormat()).
1526 If the time could not be parsed, returns an invalid time.
1528 \sa timeFormat(), toDate(), toDateTime(), QTime::fromString()
1530 #ifndef QT_NO_DATESTRING
1531 QTime QLocale::toTime(const QString &string, FormatType format) const
1533 return toTime(string, timeFormat(format));
1540 Parses the date string given in \a string and returns the
1541 date. The format of the date string is chosen according to the
1542 \a format parameter (see dateFormat()).
1544 If the date could not be parsed, returns an invalid date.
1546 \sa dateFormat(), toTime(), toDateTime(), QDate::fromString()
1548 #ifndef QT_NO_DATESTRING
1549 QDate QLocale::toDate(const QString &string, FormatType format) const
1551 return toDate(string, dateFormat(format));
1558 Parses the date/time string given in \a string and returns the
1559 time. The format of the date/time string is chosen according to the
1560 \a format parameter (see dateTimeFormat()).
1562 If the string could not be parsed, returns an invalid QDateTime.
1564 \sa dateTimeFormat(), toTime(), toDate(), QDateTime::fromString()
1567 #ifndef QT_NO_DATESTRING
1568 QDateTime QLocale::toDateTime(const QString &string, FormatType format) const
1570 return toDateTime(string, dateTimeFormat(format));
1577 Parses the time string given in \a string and returns the
1578 time. See QTime::fromString() for information on what is a valid
1581 If the time could not be parsed, returns an invalid time.
1583 \sa timeFormat(), toDate(), toDateTime(), QTime::fromString()
1585 #ifndef QT_NO_DATESTRING
1586 QTime QLocale::toTime(const QString &string, const QString &format) const
1589 #ifndef QT_BOOTSTRAPPED
1590 QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString);
1591 dt.defaultLocale = *this;
1592 if (dt.parseFormat(format))
1593 dt.fromString(string, 0, &time);
1605 Parses the date string given in \a string and returns the
1606 date. See QDate::fromString() for information on the expressions
1607 that can be used with this function.
1609 This function searches month names and the names of the days of
1610 the week in the current locale.
1612 If the date could not be parsed, returns an invalid date.
1614 \sa dateFormat(), toTime(), toDateTime(), QDate::fromString()
1616 #ifndef QT_NO_DATESTRING
1617 QDate QLocale::toDate(const QString &string, const QString &format) const
1620 #ifndef QT_BOOTSTRAPPED
1621 QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString);
1622 dt.defaultLocale = *this;
1623 if (dt.parseFormat(format))
1624 dt.fromString(string, &date, 0);
1636 Parses the date/time string given in \a string and returns the
1637 time. See QDateTime::fromString() for information on the expressions
1638 that can be used with this function.
1640 \note The month and day names used must be given in the user's local
1643 If the string could not be parsed, returns an invalid QDateTime.
1645 \sa dateTimeFormat(), toTime(), toDate(), QDateTime::fromString()
1647 #ifndef QT_NO_DATESTRING
1648 QDateTime QLocale::toDateTime(const QString &string, const QString &format) const
1650 #ifndef QT_BOOTSTRAPPED
1654 QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString);
1655 dt.defaultLocale = *this;
1656 if (dt.parseFormat(format) && dt.fromString(string, &date, &time))
1657 return QDateTime(date, time);
1662 return QDateTime(QDate(), QTime(-1, -1, -1));
1670 Returns the decimal point character of this locale.
1672 QChar QLocale::decimalPoint() const
1674 return d->decimal();
1680 Returns the group separator character of this locale.
1682 QChar QLocale::groupSeparator() const
1690 Returns the percent character of this locale.
1692 QChar QLocale::percent() const
1694 return d->percent();
1700 Returns the zero digit character of this locale.
1702 QChar QLocale::zeroDigit() const
1710 Returns the negative sign character of this locale.
1712 QChar QLocale::negativeSign() const
1720 Returns the positive sign character of this locale.
1722 QChar QLocale::positiveSign() const
1730 Returns the exponential character of this locale.
1732 QChar QLocale::exponential() const
1734 return d->exponential();
1737 static bool qIsUpper(char c)
1739 return c >= 'A' && c <= 'Z';
1742 static char qToLower(char c)
1744 if (c >= 'A' && c <= 'Z')
1745 return c - 'A' + 'a';
1753 \a f and \a prec have the same meaning as in QString::number(double, char, int).
1758 QString QLocale::toString(double i, char f, int prec) const
1760 QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
1764 flags = QLocalePrivate::CapitalEorX;
1769 form = QLocalePrivate::DFDecimal;
1772 form = QLocalePrivate::DFExponent;
1775 form = QLocalePrivate::DFSignificantDigits;
1781 if (!(d->m_numberOptions & OmitGroupSeparator))
1782 flags |= QLocalePrivate::ThousandsGroup;
1783 return d->doubleToString(i, prec, form, -1, flags);
1787 \fn QLocale QLocale::c()
1789 Returns a QLocale object initialized to the "C" locale.
1795 Returns a QLocale object initialized to the system locale.
1797 On Windows and Mac, this locale will use the decimal/grouping characters and date/time
1798 formats specified in the system configuration panel.
1803 QLocale QLocale::system()
1806 result.d->m_index = localeDataIndex(systemData());
1807 result.d->m_data = dataPointerHelper(result.d->m_index);
1815 Returns a list of valid locale objects that match the given \a language, \a
1816 script and \a country.
1818 Getting a list of all locales:
1819 QList<QLocale> allLocales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry);
1821 QList<QLocale> QLocale::matchingLocales(QLocale::Language language,
1822 QLocale::Script script,
1823 QLocale::Country country)
1825 if (uint(language) > QLocale::LastLanguage || uint(script) > QLocale::LastScript ||
1826 uint(country) > QLocale::LastCountry)
1827 return QList<QLocale>();
1829 QList<QLocale> result;
1830 const QLocaleData *data = locale_data;
1831 if (language == QLocale::AnyLanguage && script == QLocale::AnyScript && country == QLocale::AnyCountry)
1832 result.reserve(locale_data_size);
1833 if (language != QLocale::C)
1834 data += locale_index[language];
1835 while ( (data != locale_data + locale_data_size)
1836 && (language == QLocale::AnyLanguage || data->m_language_id == uint(language))) {
1837 QLocale locale(QLocale::C);
1838 locale.d->m_index = localeDataIndex(data);
1839 locale.d->m_data = dataPointerHelper(locale.d->m_index);
1840 result.append(locale);
1850 Returns the list of countries that have entires for \a language in Qt's locale
1851 database. If the result is an empty list, then \a language is not represented in
1852 Qt's locale database.
1854 \sa matchingLocales()
1856 QList<QLocale::Country> QLocale::countriesForLanguage(Language language)
1858 QList<Country> result;
1860 unsigned language_id = language;
1861 uint idx = locale_index[language_id];
1863 if (language == C) {
1864 result << AnyCountry;
1868 const QLocaleData *data = locale_data + idx;
1870 while (data->m_language_id == language_id) {
1871 result << static_cast<Country>(data->m_country_id);
1881 Returns the localized name of \a month, in the format specified
1884 \sa dayName(), standaloneMonthName()
1886 QString QLocale::monthName(int month, FormatType type) const
1888 if (month < 1 || month > 12)
1891 #ifndef QT_NO_SYSTEMLOCALE
1892 if (d->m_data == systemData()) {
1893 QVariant res = systemLocale()->query(type == LongFormat
1894 ? QSystemLocale::MonthNameLong : QSystemLocale::MonthNameShort,
1897 return res.toString();
1903 case QLocale::LongFormat:
1904 idx = d->m_data->m_long_month_names_idx;
1905 size = d->m_data->m_long_month_names_size;
1907 case QLocale::ShortFormat:
1908 idx = d->m_data->m_short_month_names_idx;
1909 size = d->m_data->m_short_month_names_size;
1911 case QLocale::NarrowFormat:
1912 idx = d->m_data->m_narrow_month_names_idx;
1913 size = d->m_data->m_narrow_month_names_size;
1918 return getLocaleListData(months_data + idx, size, month - 1);
1924 Returns the localized name of \a month that is used as a
1925 standalone text, in the format specified by \a type.
1927 If the locale information doesn't specify the standalone month
1928 name then return value is the same as in monthName().
1930 \sa monthName(), standaloneDayName()
1932 QString QLocale::standaloneMonthName(int month, FormatType type) const
1934 if (month < 1 || month > 12)
1937 #ifndef QT_NO_SYSTEMLOCALE
1938 if (d->m_data == systemData()) {
1939 QVariant res = systemLocale()->query(type == LongFormat
1940 ? QSystemLocale::MonthNameLong : QSystemLocale::MonthNameShort,
1943 return res.toString();
1949 case QLocale::LongFormat:
1950 idx = d->m_data->m_standalone_long_month_names_idx;
1951 size = d->m_data->m_standalone_long_month_names_size;
1953 case QLocale::ShortFormat:
1954 idx = d->m_data->m_standalone_short_month_names_idx;
1955 size = d->m_data->m_standalone_short_month_names_size;
1957 case QLocale::NarrowFormat:
1958 idx = d->m_data->m_standalone_narrow_month_names_idx;
1959 size = d->m_data->m_standalone_narrow_month_names_size;
1964 QString name = getLocaleListData(months_data + idx, size, month - 1);
1966 return monthName(month, type);
1973 Returns the localized name of the \a day (where 1 represents
1974 Monday, 2 represents Tuesday and so on), in the format specified
1977 \sa monthName(), standaloneDayName()
1979 QString QLocale::dayName(int day, FormatType type) const
1981 if (day < 1 || day > 7)
1984 #ifndef QT_NO_SYSTEMLOCALE
1985 if (d->m_data == systemData()) {
1986 QVariant res = systemLocale()->query(type == LongFormat
1987 ? QSystemLocale::DayNameLong : QSystemLocale::DayNameShort,
1990 return res.toString();
1998 case QLocale::LongFormat:
1999 idx = d->m_data->m_long_day_names_idx;
2000 size = d->m_data->m_long_day_names_size;
2002 case QLocale::ShortFormat:
2003 idx = d->m_data->m_short_day_names_idx;
2004 size = d->m_data->m_short_day_names_size;
2006 case QLocale::NarrowFormat:
2007 idx = d->m_data->m_narrow_day_names_idx;
2008 size = d->m_data->m_narrow_day_names_size;
2013 return getLocaleListData(days_data + idx, size, day);
2019 Returns the localized name of the \a day (where 1 represents
2020 Monday, 2 represents Tuesday and so on) that is used as a
2021 standalone text, in the format specified by \a type.
2023 If the locale information does not specify the standalone day
2024 name then return value is the same as in dayName().
2026 \sa dayName(), standaloneMonthName()
2028 QString QLocale::standaloneDayName(int day, FormatType type) const
2030 if (day < 1 || day > 7)
2033 #ifndef QT_NO_SYSTEMLOCALE
2034 if (d->m_data == systemData()) {
2035 QVariant res = systemLocale()->query(type == LongFormat
2036 ? QSystemLocale::DayNameLong : QSystemLocale::DayNameShort,
2039 return res.toString();
2047 case QLocale::LongFormat:
2048 idx = d->m_data->m_standalone_long_day_names_idx;
2049 size = d->m_data->m_standalone_long_day_names_size;
2051 case QLocale::ShortFormat:
2052 idx = d->m_data->m_standalone_short_day_names_idx;
2053 size = d->m_data->m_standalone_short_day_names_size;
2055 case QLocale::NarrowFormat:
2056 idx = d->m_data->m_standalone_narrow_day_names_idx;
2057 size = d->m_data->m_standalone_narrow_day_names_size;
2062 QString name = getLocaleListData(days_data + idx, size, day);
2064 return dayName(day == 0 ? 7 : day, type);
2071 Returns the first day of the week according to the current locale.
2073 Qt::DayOfWeek QLocale::firstDayOfWeek() const
2075 #ifndef QT_NO_SYSTEMLOCALE
2076 if (d->m_data == systemData()) {
2077 QVariant res = systemLocale()->query(QSystemLocale::FirstDayOfWeek, QVariant());
2079 return static_cast<Qt::DayOfWeek>(res.toUInt());
2082 return static_cast<Qt::DayOfWeek>(d->m_data->m_first_day_of_week);
2085 QLocale::MeasurementSystem QLocalePrivate::measurementSystem() const
2087 for (int i = 0; i < ImperialMeasurementSystemsCount; ++i) {
2088 if (ImperialMeasurementSystems[i].languageId == m_data->m_language_id
2089 && ImperialMeasurementSystems[i].countryId == m_data->m_country_id) {
2090 return ImperialMeasurementSystems[i].system;
2093 return QLocale::MetricSystem;
2099 Returns a list of days that are considered weekdays according to the current locale.
2101 QList<Qt::DayOfWeek> QLocale::weekdays() const
2103 #ifndef QT_NO_SYSTEMLOCALE
2104 if (d->m_data == systemData()) {
2105 QVariant res = systemLocale()->query(QSystemLocale::Weekdays, QVariant());
2107 return static_cast<QList<Qt::DayOfWeek> >(res.value<QList<Qt::DayOfWeek> >());
2110 QList<Qt::DayOfWeek> weekdays;
2111 quint16 weekendStart = d->m_data->m_weekend_start;
2112 quint16 weekendEnd = d->m_data->m_weekend_end;
2113 for (int day = Qt::Monday; day <= Qt::Sunday; day++) {
2114 if ((weekendEnd >= weekendStart && (day < weekendStart || day > weekendEnd)) ||
2115 (weekendEnd < weekendStart && (day > weekendEnd && day < weekendStart)))
2116 weekdays << static_cast<Qt::DayOfWeek>(day);
2124 Returns the measurement system for the locale.
2126 QLocale::MeasurementSystem QLocale::measurementSystem() const
2128 #ifndef QT_NO_SYSTEMLOCALE
2129 if (d->m_data == systemData()) {
2130 QVariant res = systemLocale()->query(QSystemLocale::MeasurementSystem, QVariant());
2132 return MeasurementSystem(res.toInt());
2136 return d->measurementSystem();
2142 Returns the text direction of the language.
2144 Qt::LayoutDirection QLocale::textDirection() const
2146 Language lang = language();
2147 if (lang == QLocale::Arabic ||
2148 lang == QLocale::Hebrew ||
2149 lang == QLocale::Persian ||
2150 lang == QLocale::Pashto ||
2151 lang == QLocale::Urdu ||
2152 lang == QLocale::Syriac ||
2153 lang == QLocale::Divehi)
2154 return Qt::RightToLeft;
2156 return Qt::LeftToRight;
2162 Returns an uppercase copy of \a str.
2164 QString QLocale::toUpper(const QString &str) const
2169 if (qt_u_strToUpper(str, &result, *this))
2171 // else fall through and use Qt's toUpper
2174 return str.toUpper();
2180 Returns a lowercase copy of \a str.
2182 QString QLocale::toLower(const QString &str) const
2187 if (qt_u_strToLower(str, &result, *this))
2189 // else fall through and use Qt's toUpper
2192 return str.toLower();
2199 Returns the localized name of the "AM" suffix for times specified using
2200 the conventions of the 12-hour clock.
2204 QString QLocale::amText() const
2206 #ifndef QT_NO_SYSTEMLOCALE
2207 if (d->m_data == systemData()) {
2208 QVariant res = systemLocale()->query(QSystemLocale::AMText, QVariant());
2210 return res.toString();
2213 return getLocaleData(am_data + d->m_data->m_am_idx, d->m_data->m_am_size);
2219 Returns the localized name of the "PM" suffix for times specified using
2220 the conventions of the 12-hour clock.
2224 QString QLocale::pmText() const
2226 #ifndef QT_NO_SYSTEMLOCALE
2227 if (d->m_data == systemData()) {
2228 QVariant res = systemLocale()->query(QSystemLocale::PMText, QVariant());
2230 return res.toString();
2233 return getLocaleData(pm_data + d->m_data->m_pm_idx, d->m_data->m_pm_size);
2237 QString QLocalePrivate::dateTimeToString(const QString &format, const QDate *date, const QTime *time,
2238 const QLocale *q) const
2240 Q_ASSERT(date || time);
2241 if ((date && !date->isValid()) || (time && !time->isValid()))
2243 const bool format_am_pm = time && timeFormatContainsAP(format);
2245 enum { AM, PM } am_pm = AM;
2246 int hour12 = time ? time->hour() : -1;
2251 } else if (hour12 < 12) {
2253 } else if (hour12 == 12) {
2264 while (i < format.size()) {
2265 if (format.at(i).unicode() == '\'') {
2266 result.append(qt_readEscapedFormatString(format, &i));
2270 const QChar c = format.at(i);
2271 int repeat = qt_repeatCount(format, i);
2274 switch (c.unicode()) {
2279 else if (repeat >= 2)
2284 result.append(longLongToString(date->year()));
2287 result.append(longLongToString(date->year() % 100, -1, 10, 2,
2288 QLocalePrivate::ZeroPadded));
2299 repeat = qMin(repeat, 4);
2302 result.append(longLongToString(date->month()));
2305 result.append(longLongToString(date->month(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2308 result.append(q->monthName(date->month(), QLocale::ShortFormat));
2311 result.append(q->monthName(date->month(), QLocale::LongFormat));
2318 repeat = qMin(repeat, 4);
2321 result.append(longLongToString(date->day()));
2324 result.append(longLongToString(date->day(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2327 result.append(q->dayName(date->dayOfWeek(), QLocale::ShortFormat));
2330 result.append(q->dayName(date->dayOfWeek(), QLocale::LongFormat));
2339 if (!used && time) {
2340 switch (c.unicode()) {
2343 repeat = qMin(repeat, 2);
2344 const int hour = format_am_pm ? hour12 : time->hour();
2348 result.append(longLongToString(hour));
2351 result.append(longLongToString(hour, -1, 10, 2, QLocalePrivate::ZeroPadded));
2358 repeat = qMin(repeat, 2);
2361 result.append(longLongToString(time->hour()));
2364 result.append(longLongToString(time->hour(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2371 repeat = qMin(repeat, 2);
2374 result.append(longLongToString(time->minute()));
2377 result.append(longLongToString(time->minute(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2384 repeat = qMin(repeat, 2);
2387 result.append(longLongToString(time->second()));
2390 result.append(longLongToString(time->second(), -1, 10, 2, QLocalePrivate::ZeroPadded));
2397 if (i + 1 < format.length() && format.at(i + 1).unicode() == 'p') {
2402 result.append(am_pm == AM ? q->amText().toLower() : q->pmText().toLower());
2407 if (i + 1 < format.length() && format.at(i + 1).unicode() == 'P') {
2412 result.append(am_pm == AM ? q->amText().toUpper() : q->pmText().toUpper());
2424 result.append(longLongToString(time->msec()));
2427 result.append(longLongToString(time->msec(), -1, 10, 3, QLocalePrivate::ZeroPadded));
2435 result.append(timeZone());
2442 result.append(QString(repeat, c));
2450 QString QLocalePrivate::doubleToString(double d,
2454 unsigned flags) const
2456 return QLocalePrivate::doubleToString(zero(), plus(), minus(), exponential(),
2458 d, precision, form, width, flags);
2461 QString QLocalePrivate::doubleToString(const QChar _zero, const QChar plus, const QChar minus,
2462 const QChar exponential, const QChar group, const QChar decimal,
2469 if (precision == -1)
2474 bool negative = false;
2475 bool special_number = false; // nan, +/-inf
2478 // Detect special numbers (nan, +/-inf)
2480 num_str = QString::fromLatin1("inf");
2481 special_number = true;
2483 } else if (qt_is_nan(d)) {
2484 num_str = QString::fromLatin1("nan");
2485 special_number = true;
2488 // Handle normal numbers
2489 if (!special_number) {
2493 #ifdef QT_QLOCALE_USES_FCVT
2495 if (form == DFDecimal) {
2496 digits = QLatin1String(fcvt(d, precision, &decpt, &sign));
2499 if (form == DFExponent)
2501 else if (form == DFSignificantDigits && pr == 0)
2503 digits = QLatin1String(ecvt(d, pr, &decpt, &sign));
2505 // Chop trailing zeros
2506 if (digits.length() > 0) {
2507 int last_nonzero_idx = digits.length() - 1;
2508 while (last_nonzero_idx > 0
2509 && digits.unicode()[last_nonzero_idx] == QLatin1Char('0'))
2511 digits.truncate(last_nonzero_idx + 1);
2518 if (form == DFDecimal)
2523 /* This next bit is a bit quirky. In DFExponent form, the precision
2524 is the number of digits after decpt. So that would suggest using
2525 mode=3 for qdtoa. But qdtoa behaves strangely when mode=3 and
2526 precision=0. So we get around this by using mode=2 and reasoning
2527 that we want precision+1 significant digits, since the decimal
2528 point in this mode is always after the first digit. */
2530 if (form == DFExponent)
2536 digits = QLatin1String(qdtoa(d, mode, pr, &decpt, &sign, &rve, &buff));
2544 #endif // QT_QLOCALE_USES_FCVT
2546 if (_zero.unicode() != '0') {
2547 ushort z = _zero.unicode() - '0';
2548 for (int i = 0; i < digits.length(); ++i)
2549 reinterpret_cast<ushort *>(digits.data())[i] += z;
2552 bool always_show_decpt = (flags & Alternate || flags & ForcePoint);
2555 num_str = exponentForm(_zero, decimal, exponential, group, plus, minus,
2556 digits, decpt, precision, PMDecimalDigits,
2561 num_str = decimalForm(_zero, decimal, group,
2562 digits, decpt, precision, PMDecimalDigits,
2563 always_show_decpt, flags & ThousandsGroup);
2566 case DFSignificantDigits: {
2567 PrecisionMode mode = (flags & Alternate) ?
2568 PMSignificantDigits : PMChopTrailingZeros;
2570 if (decpt != digits.length() && (decpt <= -4 || decpt > precision))
2571 num_str = exponentForm(_zero, decimal, exponential, group, plus, minus,
2572 digits, decpt, precision, mode,
2575 num_str = decimalForm(_zero, decimal, group,
2576 digits, decpt, precision, mode,
2577 always_show_decpt, flags & ThousandsGroup);
2582 negative = sign != 0 && !isZero(d);
2585 // pad with zeros. LeftAdjusted overrides this flag). Also, we don't
2586 // pad special numbers
2587 if (flags & QLocalePrivate::ZeroPadded
2588 && !(flags & QLocalePrivate::LeftAdjusted)
2589 && !special_number) {
2590 int num_pad_chars = width - num_str.length();
2591 // leave space for the sign
2593 || flags & QLocalePrivate::AlwaysShowSign
2594 || flags & QLocalePrivate::BlankBeforePositive)
2597 for (int i = 0; i < num_pad_chars; ++i)
2598 num_str.prepend(_zero);
2603 num_str.prepend(minus);
2604 else if (flags & QLocalePrivate::AlwaysShowSign)
2605 num_str.prepend(plus);
2606 else if (flags & QLocalePrivate::BlankBeforePositive)
2607 num_str.prepend(QLatin1Char(' '));
2609 if (flags & QLocalePrivate::CapitalEorX)
2610 num_str = num_str.toUpper();
2615 QString QLocalePrivate::longLongToString(qlonglong l, int precision,
2616 int base, int width,
2617 unsigned flags) const
2619 return QLocalePrivate::longLongToString(zero(), group(), plus(), minus(),
2620 l, precision, base, width, flags);
2623 QString QLocalePrivate::longLongToString(const QChar zero, const QChar group,
2624 const QChar plus, const QChar minus,
2625 qlonglong l, int precision,
2626 int base, int width,
2629 bool precision_not_specified = false;
2630 if (precision == -1) {
2631 precision_not_specified = true;
2635 bool negative = l < 0;
2637 // these are not supported by sprintf for octal and hex
2638 flags &= ~AlwaysShowSign;
2639 flags &= ~BlankBeforePositive;
2640 negative = false; // neither are negative numbers
2645 num_str = qlltoa(l, base, zero);
2647 num_str = qulltoa(l, base, zero);
2649 uint cnt_thousand_sep = 0;
2650 if (flags & ThousandsGroup && base == 10) {
2651 for (int i = num_str.length() - 3; i > 0; i -= 3) {
2652 num_str.insert(i, group);
2657 for (int i = num_str.length()/* - cnt_thousand_sep*/; i < precision; ++i)
2658 num_str.prepend(base == 10 ? zero : QChar::fromLatin1('0'));
2660 if ((flags & Alternate || flags & ShowBase)
2662 && (num_str.isEmpty() || num_str[0].unicode() != QLatin1Char('0')))
2663 num_str.prepend(QLatin1Char('0'));
2665 // LeftAdjusted overrides this flag ZeroPadded. sprintf only padds
2666 // when precision is not specified in the format string
2667 bool zero_padded = flags & ZeroPadded
2668 && !(flags & LeftAdjusted)
2669 && precision_not_specified;
2672 int num_pad_chars = width - num_str.length();
2674 // leave space for the sign
2676 || flags & AlwaysShowSign
2677 || flags & BlankBeforePositive)
2680 // leave space for optional '0x' in hex form
2681 if (base == 16 && (flags & Alternate || flags & ShowBase))
2683 // leave space for optional '0b' in binary form
2684 else if (base == 2 && (flags & Alternate || flags & ShowBase))
2687 for (int i = 0; i < num_pad_chars; ++i)
2688 num_str.prepend(base == 10 ? zero : QChar::fromLatin1('0'));
2691 if (flags & CapitalEorX)
2692 num_str = num_str.toUpper();
2694 if (base == 16 && (flags & Alternate || flags & ShowBase))
2695 num_str.prepend(QLatin1String(flags & UppercaseBase ? "0X" : "0x"));
2696 if (base == 2 && (flags & Alternate || flags & ShowBase))
2697 num_str.prepend(QLatin1String(flags & UppercaseBase ? "0B" : "0b"));
2701 num_str.prepend(minus);
2702 else if (flags & AlwaysShowSign)
2703 num_str.prepend(plus);
2704 else if (flags & BlankBeforePositive)
2705 num_str.prepend(QLatin1Char(' '));
2710 QString QLocalePrivate::unsLongLongToString(qulonglong l, int precision,
2711 int base, int width,
2712 unsigned flags) const
2714 return QLocalePrivate::unsLongLongToString(zero(), group(), plus(),
2715 l, precision, base, width, flags);
2718 QString QLocalePrivate::unsLongLongToString(const QChar zero, const QChar group,
2720 qulonglong l, int precision,
2721 int base, int width,
2724 bool precision_not_specified = false;
2725 if (precision == -1) {
2726 precision_not_specified = true;
2730 QString num_str = qulltoa(l, base, zero);
2732 uint cnt_thousand_sep = 0;
2733 if (flags & ThousandsGroup && base == 10) {
2734 for (int i = num_str.length() - 3; i > 0; i -=3) {
2735 num_str.insert(i, group);
2740 for (int i = num_str.length()/* - cnt_thousand_sep*/; i < precision; ++i)
2741 num_str.prepend(base == 10 ? zero : QChar::fromLatin1('0'));
2743 if ((flags & Alternate || flags & ShowBase)
2745 && (num_str.isEmpty() || num_str[0].unicode() != QLatin1Char('0')))
2746 num_str.prepend(QLatin1Char('0'));
2748 // LeftAdjusted overrides this flag ZeroPadded. sprintf only padds
2749 // when precision is not specified in the format string
2750 bool zero_padded = flags & ZeroPadded
2751 && !(flags & LeftAdjusted)
2752 && precision_not_specified;
2755 int num_pad_chars = width - num_str.length();
2757 // leave space for optional '0x' in hex form
2758 if (base == 16 && flags & Alternate)
2760 // leave space for optional '0b' in binary form
2761 else if (base == 2 && flags & Alternate)
2764 for (int i = 0; i < num_pad_chars; ++i)
2765 num_str.prepend(base == 10 ? zero : QChar::fromLatin1('0'));
2768 if (flags & CapitalEorX)
2769 num_str = num_str.toUpper();
2771 if (base == 16 && (flags & Alternate || flags & ShowBase))
2772 num_str.prepend(QLatin1String(flags & UppercaseBase ? "0X" : "0x"));
2773 else if (base == 2 && (flags & Alternate || flags & ShowBase))
2774 num_str.prepend(QLatin1String(flags & UppercaseBase ? "0B" : "0b"));
2777 if (flags & AlwaysShowSign)
2778 num_str.prepend(plus);
2779 else if (flags & BlankBeforePositive)
2780 num_str.prepend(QLatin1Char(' '));
2786 Converts a number in locale to its representation in the C locale.
2787 Only has to guarantee that a string that is a correct representation of
2788 a number will be converted. If junk is passed in, junk will be passed
2789 out and the error will be detected during the actual conversion to a
2790 number. We can't detect junk here, since we don't even know the base
2793 bool QLocalePrivate::numberToCLocale(const QString &num,
2794 GroupSeparatorMode group_sep_mode,
2795 CharBuff *result) const
2797 const QChar *uc = num.unicode();
2798 int l = num.length();
2802 while (idx < l && uc[idx].isSpace())
2808 const QChar in = uc[idx];
2810 char out = digitToCLocale(in);
2814 else if (in == percent())
2816 // for handling base-x numbers
2817 else if (in.unicode() >= 'A' && in.unicode() <= 'Z')
2818 out = in.toLower().toLatin1();
2819 else if (in.unicode() >= 'a' && in.unicode() <= 'z')
2820 out = in.toLatin1();
2825 result->append(out);
2830 // Check trailing whitespace
2831 for (; idx < l; ++idx) {
2832 if (!uc[idx].isSpace())
2836 result->append('\0');
2839 if (group_sep_mode == ParseGroupSeparators
2840 && !removeGroupSeparators(result))
2847 bool QLocalePrivate::validateChars(const QString &str, NumberMode numMode, QByteArray *buff,
2848 int decDigits) const
2851 buff->reserve(str.length());
2853 const bool scientific = numMode == DoubleScientificMode;
2854 bool lastWasE = false;
2855 bool lastWasDigit = false;
2857 int decPointCnt = 0;
2859 int decDigitCnt = 0;
2861 for (int i = 0; i < str.length(); ++i) {
2862 char c = digitToCLocale(str.at(i));
2864 if (c >= '0' && c <= '9') {
2865 if (numMode != IntegerMode) {
2866 // If a double has too many digits after decpt, it shall be Invalid.
2867 if (dec && decDigits != -1 && decDigits < ++decDigitCnt)
2870 lastWasDigit = true;
2874 if (numMode == IntegerMode) {
2875 // If an integer has a decimal point, it shall be Invalid.
2878 // If a double has more than one decimal point, it shall be Invalid.
2879 if (++decPointCnt > 1)
2882 // If a double with no decimal digits has a decimal point, it shall be
2886 #endif // On second thoughts, it shall be Valid.
2895 // If a scientific has a sign that's not at the beginning or after
2896 // an 'e', it shall be Invalid.
2897 if (i != 0 && !lastWasE)
2900 // If a non-scientific has a sign that's not at the beginning,
2901 // it shall be Invalid.
2908 //it can only be placed after a digit which is before the decimal point
2909 if (!lastWasDigit || decPointCnt > 0)
2915 // If a scientific has more than one 'e', it shall be Invalid.
2920 // If a non-scientific has an 'e', it shall be Invalid.
2926 // If it's not a valid digit, it shall be Invalid.
2929 lastWasDigit = false;
2932 lastWasE = c == 'e';
2940 double QLocalePrivate::stringToDouble(const QString &number, bool *ok,
2941 GroupSeparatorMode group_sep_mode) const
2944 if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
2945 group_sep_mode, &buff)) {
2950 return bytearrayToDouble(buff.constData(), ok);
2953 qlonglong QLocalePrivate::stringToLongLong(const QString &number, int base,
2954 bool *ok, GroupSeparatorMode group_sep_mode) const
2957 if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
2958 group_sep_mode, &buff)) {
2964 return bytearrayToLongLong(buff.constData(), base, ok);
2967 qulonglong QLocalePrivate::stringToUnsLongLong(const QString &number, int base,
2968 bool *ok, GroupSeparatorMode group_sep_mode) const
2971 if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
2972 group_sep_mode, &buff)) {
2978 return bytearrayToUnsLongLong(buff.constData(), base, ok);
2982 double QLocalePrivate::bytearrayToDouble(const char *num, bool *ok, bool *overflow)
2995 if (qstrcmp(num, "nan") == 0)
2998 if (qstrcmp(num, "+inf") == 0 || qstrcmp(num, "inf") == 0)
3001 if (qstrcmp(num, "-inf") == 0)
3006 double d = qstrtod(num, &endptr, &_ok);
3009 // the only way strtod can fail with *endptr != '\0' on a non-empty
3010 // input string is overflow
3014 *overflow = *endptr != '\0';
3018 if (*endptr != '\0') {
3019 // we stopped at a non-digit character after converting some digits
3034 qlonglong QLocalePrivate::bytearrayToLongLong(const char *num, int base, bool *ok, bool *overflow)
3047 qlonglong l = qstrtoll(num, &endptr, base, &_ok);
3052 if (overflow != 0) {
3053 // the only way qstrtoll can fail with *endptr != '\0' on a non-empty
3054 // input string is overflow
3055 *overflow = *endptr != '\0';
3060 if (*endptr != '\0') {
3061 // we stopped at a non-digit character after converting some digits
3076 qulonglong QLocalePrivate::bytearrayToUnsLongLong(const char *num, int base, bool *ok)
3080 qulonglong l = qstrtoull(num, &endptr, base, &_ok);
3082 if (!_ok || *endptr != '\0') {
3096 \enum QLocale::CurrencySymbolFormat
3098 Specifies the format of the currency symbol.
3100 \value CurrencyIsoCode a ISO-4217 code of the currency.
3101 \value CurrencySymbol a currency symbol.
3102 \value CurrencyDisplayName a user readable name of the currency.
3107 Returns a currency symbol according to the \a format.
3109 QString QLocale::currencySymbol(QLocale::CurrencySymbolFormat format) const
3111 #ifndef QT_NO_SYSTEMLOCALE
3112 if (d->m_data == systemData()) {
3113 QVariant res = systemLocale()->query(QSystemLocale::CurrencySymbol, format);
3115 return res.toString();
3120 case CurrencySymbol:
3121 idx = d->m_data->m_currency_symbol_idx;
3122 size = d->m_data->m_currency_symbol_size;
3123 return getLocaleData(currency_symbol_data + idx, size);
3124 case CurrencyDisplayName:
3125 idx = d->m_data->m_currency_display_name_idx;
3126 size = d->m_data->m_currency_display_name_size;
3127 return getLocaleListData(currency_display_name_data + idx, size, 0);
3128 case CurrencyIsoCode: {
3130 const QLocaleData *data = this->d->m_data;
3131 for (; len < 3; ++len)
3132 if (!data->m_currency_iso_code[len])
3134 return len ? QString::fromLatin1(data->m_currency_iso_code, len) : QString();
3143 Returns a localized string representation of \a value as a currency.
3144 If the \a symbol is provided it is used instead of the default currency symbol.
3146 \sa currencySymbol()
3148 QString QLocale::toCurrencyString(qlonglong value, const QString &symbol) const
3150 #ifndef QT_NO_SYSTEMLOCALE
3151 if (d->m_data == systemData()) {
3152 QSystemLocale::CurrencyToStringArgument arg(value, symbol);
3153 QVariant res = systemLocale()->query(QSystemLocale::CurrencyToString, QVariant::fromValue(arg));
3155 return res.toString();
3158 const QLocalePrivate *d = this->d;
3159 quint8 idx = d->m_data->m_currency_format_idx;
3160 quint8 size = d->m_data->m_currency_format_size;
3161 if (d->m_data->m_currency_negative_format_size && value < 0) {
3162 idx = d->m_data->m_currency_negative_format_idx;
3163 size = d->m_data->m_currency_negative_format_size;
3166 QString str = d->longLongToString(value);
3167 QString sym = symbol.isNull() ? currencySymbol() : symbol;
3169 sym = currencySymbol(QLocale::CurrencyIsoCode);
3170 QString format = getLocaleData(currency_format_data + idx, size);
3171 return format.arg(str, sym);
3178 QString QLocale::toCurrencyString(qulonglong value, const QString &symbol) const
3180 #ifndef QT_NO_SYSTEMLOCALE
3181 if (d->m_data == systemData()) {
3182 QSystemLocale::CurrencyToStringArgument arg(value, symbol);
3183 QVariant res = systemLocale()->query(QSystemLocale::CurrencyToString, QVariant::fromValue(arg));
3185 return res.toString();
3188 const QLocaleData *data = this->d->m_data;
3189 quint8 idx = data->m_currency_format_idx;
3190 quint8 size = data->m_currency_format_size;
3191 QString str = d->unsLongLongToString(value);
3192 QString sym = symbol.isNull() ? currencySymbol() : symbol;
3194 sym = currencySymbol(QLocale::CurrencyIsoCode);
3195 QString format = getLocaleData(currency_format_data + idx, size);
3196 return format.arg(str, sym);
3203 QString QLocale::toCurrencyString(double value, const QString &symbol) const
3205 #ifndef QT_NO_SYSTEMLOCALE
3206 if (d->m_data == systemData()) {
3207 QSystemLocale::CurrencyToStringArgument arg(value, symbol);
3208 QVariant res = systemLocale()->query(QSystemLocale::CurrencyToString, QVariant::fromValue(arg));
3210 return res.toString();
3213 const QLocaleData *data = this->d->m_data;
3214 quint8 idx = data->m_currency_format_idx;
3215 quint8 size = data->m_currency_format_size;
3216 if (data->m_currency_negative_format_size && value < 0) {
3217 idx = data->m_currency_negative_format_idx;
3218 size = data->m_currency_negative_format_size;
3221 QString str = d->doubleToString(value, d->m_data->m_currency_digits,
3222 QLocalePrivate::DFDecimal);
3223 QString sym = symbol.isNull() ? currencySymbol() : symbol;
3225 sym = currencySymbol(QLocale::CurrencyIsoCode);
3226 QString format = getLocaleData(currency_format_data + idx, size);
3227 return format.arg(str, sym);
3233 Returns an ordered list of locale names for translation purposes in
3236 The return value represents locale names that the user expects to see the
3239 Most like you do not need to use this function directly, but just pass the
3240 QLocale object to the QTranslator::load() function.
3242 The first item in the list is the most preferred one.
3244 \sa QTranslator, bcp47Name()
3246 QStringList QLocale::uiLanguages() const
3248 #ifndef QT_NO_SYSTEMLOCALE
3249 if (d->m_data == systemData()) {
3250 QVariant res = systemLocale()->query(QSystemLocale::UILanguages, QVariant());
3251 if (!res.isNull()) {
3252 QStringList result = res.toStringList();
3253 if (!result.isEmpty())
3258 return QStringList(bcp47Name());
3264 Returns a native name of the language for the locale. For example
3265 "Schwiizertüütsch" for Swiss-German locale.
3267 \sa nativeCountryName(), languageToString()
3269 QString QLocale::nativeLanguageName() const
3271 #ifndef QT_NO_SYSTEMLOCALE
3272 if (d->m_data == systemData()) {
3273 QVariant res = systemLocale()->query(QSystemLocale::NativeLanguageName, QVariant());
3275 return res.toString();
3278 return getLocaleData(endonyms_data + d->m_data->m_language_endonym_idx, d->m_data->m_language_endonym_size);
3284 Returns a native name of the country for the locale. For example
3285 "España" for Spanish/Spain locale.
3287 \sa nativeLanguageName(), countryToString()
3289 QString QLocale::nativeCountryName() const
3291 #ifndef QT_NO_SYSTEMLOCALE
3292 if (d->m_data == systemData()) {
3293 QVariant res = systemLocale()->query(QSystemLocale::NativeCountryName, QVariant());
3295 return res.toString();
3298 return getLocaleData(endonyms_data + d->m_data->m_country_endonym_idx, d->m_data->m_country_endonym_size);
3301 #ifndef QT_NO_DEBUG_STREAM
3302 QDebug operator<<(QDebug dbg, const QLocale &l)
3304 dbg.nospace() << "QLocale(" << qPrintable(QLocale::languageToString(l.language()))
3305 << ", " << qPrintable(QLocale::scriptToString(l.script()))
3306 << ", " << qPrintable(QLocale::countryToString(l.country())) << ')';