Qt 4.8
qlocale_unix.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qlocale_p.h"
43 
44 #include "qstringbuilder.h"
45 #include "qdatetime.h"
46 #include "qstringlist.h"
47 #include "qvariant.h"
48 
50 
52 {
53 #if defined(Q_OS_QNX)
54  static char buff[257];
55 
56  memset(buff, 0, sizeof buff);
57 
58  if (confstr(_CS_LOCALE, buff, 257) > 0)
59  return buff;
60  else
61  return qgetenv("LC_ALL");
62 #else
63  return qgetenv("LC_ALL");
64 #endif
65 }
66 
67 #ifndef QT_NO_SYSTEMLOCALE
69 {
71  : lc_numeric(QLocale::C)
72  ,lc_time(QLocale::C)
73  ,lc_monetary(QLocale::C)
74  ,lc_messages(QLocale::C)
75  {
76  updateLocale();
77  }
78 
79  void updateLocale()
80  {
82  QByteArray numeric = all.isEmpty() ? qgetenv("LC_NUMERIC") : all;
83  QByteArray time = all.isEmpty() ? qgetenv("LC_TIME") : all;
84  QByteArray monetary = all.isEmpty() ? qgetenv("LC_MONETARY") : all;
85  lc_messages_var = all.isEmpty() ? qgetenv("LC_MESSAGES") : all;
86  lc_measurement_var = all.isEmpty() ? qgetenv("LC_MEASUREMENT") : all;
87 
88  QByteArray lang = qgetenv("LANG");
89  if (lang.isEmpty())
90  lang = QByteArray("C");
91  if (numeric.isEmpty())
92  numeric = lang;
93  if (time.isEmpty())
94  time = lang;
95  if (monetary.isEmpty())
96  monetary = lang;
98  lc_messages_var = lang;
100  lc_measurement_var = lang;
105  }
106 
113 };
114 Q_GLOBAL_STATIC(QSystemLocaleData, qSystemLocaleData)
115 #endif
116 
117 #ifndef QT_NO_SYSTEMLOCALE
119 {
120 
121  QByteArray lang = getSystemLocale();
122 
123  if (lang.isEmpty())
124  lang = qgetenv("LC_MESSAGES");
125  if (lang.isEmpty())
126  lang = qgetenv("LANG");
127  return QLocale(QLatin1String(lang));
128 }
129 
130 QVariant QSystemLocale::query(QueryType type, QVariant in) const
131 {
132  QSystemLocaleData *d = qSystemLocaleData();
133  const QLocale &lc_numeric = d->lc_numeric;
134  const QLocale &lc_time = d->lc_time;
135  const QLocale &lc_monetary = d->lc_monetary;
136  const QLocale &lc_messages = d->lc_messages;
137 
138  switch (type) {
139  case DecimalPoint:
140  return lc_numeric.decimalPoint();
141  case GroupSeparator:
142  return lc_numeric.groupSeparator();
143  case ZeroDigit:
144  return lc_numeric.zeroDigit();
145  case NegativeSign:
146  return lc_numeric.negativeSign();
147  case DateFormatLong:
148  return lc_time.dateFormat(QLocale::LongFormat);
149  case DateFormatShort:
150  return lc_time.dateFormat(QLocale::ShortFormat);
151  case TimeFormatLong:
152  return lc_time.timeFormat(QLocale::LongFormat);
153  case TimeFormatShort:
154  return lc_time.timeFormat(QLocale::ShortFormat);
155  case DayNameLong:
156  return lc_time.dayName(in.toInt(), QLocale::LongFormat);
157  case DayNameShort:
158  return lc_time.dayName(in.toInt(), QLocale::ShortFormat);
159  case MonthNameLong:
160  return lc_time.monthName(in.toInt(), QLocale::LongFormat);
161  case MonthNameShort:
162  return lc_time.monthName(in.toInt(), QLocale::ShortFormat);
163  case StandaloneMonthNameLong:
164  return lc_time.standaloneMonthName(in.toInt(), QLocale::LongFormat);
165  case StandaloneMonthNameShort:
166  return lc_time.standaloneMonthName(in.toInt(), QLocale::ShortFormat);
167  case DateToStringLong:
168  return lc_time.toString(in.toDate(), QLocale::LongFormat);
169  case DateToStringShort:
170  return lc_time.toString(in.toDate(), QLocale::ShortFormat);
171  case TimeToStringLong:
172  return lc_time.toString(in.toTime(), QLocale::LongFormat);
173  case TimeToStringShort:
174  return lc_time.toString(in.toTime(), QLocale::ShortFormat);
175  case DateTimeFormatLong:
176  return lc_time.dateTimeFormat(QLocale::LongFormat);
177  case DateTimeFormatShort:
178  return lc_time.dateTimeFormat(QLocale::ShortFormat);
179  case DateTimeToStringLong:
180  return lc_time.toString(in.toDateTime(), QLocale::LongFormat);
181  case DateTimeToStringShort:
182  return lc_time.toString(in.toDateTime(), QLocale::ShortFormat);
183  case PositiveSign:
184  return lc_numeric.positiveSign();
185  case AMText:
186  return lc_time.amText();
187  case PMText:
188  return lc_time.pmText();
189  case FirstDayOfWeek:
190  return lc_time.firstDayOfWeek();
191  case CurrencySymbol:
192  return lc_monetary.currencySymbol(QLocale::CurrencySymbolFormat(in.toUInt()));
193  case CurrencyToString: {
194  switch (in.type()) {
195  case QVariant::Int:
196  return lc_monetary.toCurrencyString(in.toInt());
197  case QVariant::UInt:
198  return lc_monetary.toCurrencyString(in.toUInt());
199  case QVariant::Double:
200  return lc_monetary.toCurrencyString(in.toDouble());
201  case QVariant::LongLong:
202  return lc_monetary.toCurrencyString(in.toLongLong());
203  case QVariant::ULongLong:
204  return lc_monetary.toCurrencyString(in.toULongLong());
205  default:
206  break;
207  }
208  return QString();
209  }
210  case MeasurementSystem: {
212  if (meas_locale.compare(QLatin1String("Metric"), Qt::CaseInsensitive) == 0)
213  return QLocale::MetricSystem;
214  if (meas_locale.compare(QLatin1String("Other"), Qt::CaseInsensitive) == 0)
215  return QLocale::MetricSystem;
216  return QVariant((int)QLocale(meas_locale).measurementSystem());
217  }
218  case UILanguages: {
219  static QString languages = QString::fromLatin1(qgetenv("LANGUAGE"));
220  if (!languages.isEmpty()) {
221  QStringList lst = languages.split(QLatin1Char(':'));
222  for (int i = 0; i < lst.size();) {
223  const QString &name = lst.at(i);
224  QString lang, script, cntry;
225  if (name.isEmpty() || !qt_splitLocaleName(name, lang, script, cntry))
226  lst.removeAt(i);
227  else
228  ++i;
229  }
230  return lst;
231  }
232  if (!d->lc_messages_var.isEmpty()) {
233  QString lang, script, cntry;
235  lang, script, cntry)) {
236  if (!cntry.length() && lang.length())
237  return QStringList(lang);
238  return QStringList(lang % QLatin1Char('-') % cntry);
239  }
240  }
241  return QVariant();
242  }
243  case StringToStandardQuotation:
244  return lc_messages.quoteString(in.value<QStringRef>());
245  case StringToAlternateQuotation:
246  return lc_messages.quoteString(in.value<QStringRef>(), QLocale::AlternateQuotation);
247  case ListToSeparatedString:
248  return lc_messages.createSeparatedList(in.value<QStringList>());
249  case LocaleChanged:
250  d->updateLocale();
251  break;
252  default:
253  break;
254  }
255  return QVariant();
256 }
257 #endif // QT_NO_SYSTEMLOCALE
258 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
double d
Definition: qnumeric_p.h:62
QChar positiveSign() const
Returns the positive sign character of this locale.
Definition: qlocale.cpp:1830
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString timeFormat(FormatType format=LongFormat) const
Returns the time format used for the current locale.
Definition: qlocale.cpp:1535
QString createSeparatedList(const QStringList &strl) const
Returns a string that represents a join of a given list of strings with a separator defined by the lo...
Definition: qlocale.cpp:855
QString monthName(int, FormatType format=LongFormat) const
Returns the localized name of month, in the format specified by type.
Definition: qlocale.cpp:2007
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QDateTime toDateTime() const
Returns the variant as a QDateTime if the variant has type() DateTime , Date , or String ; otherwise ...
Definition: qvariant.cpp:2349
QChar zeroDigit() const
Returns the zero digit character of this locale.
Definition: qlocale.cpp:1804
QByteArray lc_measurement_var
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool qt_splitLocaleName(const QString &name, QString &lang, QString &script, QString &cntry)
Definition: qlocale.cpp:302
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QByteArray getSystemLocale()
QString toString(qlonglong i) const
Returns a localized string representation of i.
Definition: qlocale.cpp:1295
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qlonglong toLongLong(bool *ok=0) const
Returns the variant as a long long int if the variant has type() LongLong , Bool , ByteArray , Char , Double , Int , String , UInt , or ULongLong ; otherwise returns 0.
Definition: qvariant.cpp:2659
QChar decimalPoint() const
Returns the decimal point character of this locale.
Definition: qlocale.cpp:1765
QString dateTimeFormat(FormatType format=LongFormat) const
Returns the date time format used for the current locale.
Definition: qlocale.cpp:1575
QString quoteString(const QString &str, QuotationStyle style=StandardQuotation) const
Returns str quoted according to the current locale using the given quotation style.
Definition: qlocale.cpp:816
QChar groupSeparator() const
Returns the group separator character of this locale.
Definition: qlocale.cpp:1778
QString pmText() const
Returns the localized name of the "PM" suffix for times specified using the conventions of the 12-hou...
Definition: qlocale.cpp:2376
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QString toCurrencyString(qlonglong, const QString &symbol=QString()) const
Returns a localized string representation of value as a currency.
Definition: qlocale.cpp:3309
qulonglong toULongLong(bool *ok=0) const
Returns the variant as as an unsigned long long int if the variant has type() ULongLong ...
Definition: qvariant.cpp:2675
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QChar negativeSign() const
Returns the negative sign character of this locale.
Definition: qlocale.cpp:1817
CurrencySymbolFormat
Specifies the format of the currency symbol.
Definition: qlocale.h:666
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
The QStringRef class provides a thin wrapper around QString substrings.
Definition: qstring.h:1099
QString amText() const
Returns the localized name of the "AM" suffix for times specified using the conventions of the 12-hou...
Definition: qlocale.cpp:2353
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QDate toDate() const
Returns the variant as a QDate if the variant has type() Date , DateTime , or String ; otherwise retu...
Definition: qvariant.cpp:2311
int compare(const QString &s) const
Definition: qstring.cpp:5037
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
uint toUInt(bool *ok=0) const
Returns the variant as an unsigned int if the variant has type() UInt , Bool , ByteArray ...
Definition: qvariant.cpp:2644
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
QString standaloneMonthName(int, FormatType format=LongFormat) const
Returns the localized name of month that is used as a standalone text, in the format specified by typ...
Definition: qlocale.cpp:2056
virtual QVariant query(QueryType type, QVariant in) const
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
double toDouble(bool *ok=0) const
Returns the variant as a double if the variant has type() Double , QMetaType::Float ...
Definition: qvariant.cpp:2710
virtual QLocale fallbackLocale() const
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QString dayName(int, FormatType format=LongFormat) const
Returns the localized name of the day (where 1 represents Monday, 2 represents Tuesday and so on)...
Definition: qlocale.cpp:2106
QString currencySymbol(CurrencySymbolFormat=CurrencySymbol) const
Returns a currency symbol according to the format.
Definition: qlocale.cpp:3267
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
Qt::DayOfWeek firstDayOfWeek() const
Returns the first day of the week according to the current locale.
Definition: qlocale.cpp:2206
QTime toTime() const
Returns the variant as a QTime if the variant has type() Time , DateTime , or String ; otherwise retu...
Definition: qvariant.cpp:2330
QByteArray lc_messages_var
T value() const
Returns the stored value converted to the template type T.
Definition: qvariant.h:332
QString dateFormat(FormatType format=LongFormat) const
Returns the date format used for the current locale.
Definition: qlocale.cpp:1495
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void removeAt(int i)
Removes the item at index position i.
Definition: qlist.h:480