Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
numerus.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "translator.h"
5
6#include <QtCore/QByteArray>
7#include <QtCore/QDebug>
8#include <QtCore/QDir>
9#include <QtCore/QFile>
10#include <QtCore/QFileInfo>
11#include <QtCore/QMap>
12
13#include <private/qtranslator_p.h>
14
16
17static const uchar englishStyleRules[] =
18 { Q_EQ, 1 };
19static const uchar frenchStyleRules[] =
20 { Q_LEQ, 1 };
21static const uchar latvianRules[] =
22 { Q_MOD_10 | Q_EQ, 1, Q_AND, Q_MOD_100 | Q_NEQ, 11, Q_NEWRULE,
23 Q_NEQ, 0 };
24static const uchar icelandicRules[] =
25 { Q_MOD_10 | Q_EQ, 1, Q_AND, Q_MOD_100 | Q_NEQ, 11 };
26static const uchar irishStyleRules[] =
27 { Q_EQ, 1, Q_NEWRULE,
28 Q_EQ, 2 };
29static const uchar gaelicStyleRules[] =
30 { Q_EQ, 1, Q_OR, Q_EQ, 11, Q_NEWRULE,
31 Q_EQ, 2, Q_OR, Q_EQ, 12, Q_NEWRULE,
32 Q_BETWEEN, 3, 19 };
33static const uchar slovakStyleRules[] =
34 { Q_EQ, 1, Q_NEWRULE,
35 Q_BETWEEN, 2, 4 };
36static const uchar macedonianRules[] =
37 { Q_MOD_10 | Q_EQ, 1, Q_NEWRULE,
38 Q_MOD_10 | Q_EQ, 2 };
39static const uchar lithuanianRules[] =
40 { Q_MOD_10 | Q_EQ, 1, Q_AND, Q_MOD_100 | Q_NEQ, 11, Q_NEWRULE,
41 Q_MOD_10 | Q_NEQ, 0, Q_AND, Q_MOD_100 | Q_NOT_BETWEEN, 10, 19 };
42static const uchar russianStyleRules[] =
43 { Q_MOD_10 | Q_EQ, 1, Q_AND, Q_MOD_100 | Q_NEQ, 11, Q_NEWRULE,
44 Q_MOD_10 | Q_BETWEEN, 2, 4, Q_AND, Q_MOD_100 | Q_NOT_BETWEEN, 10, 19 };
45static const uchar polishRules[] =
46 { Q_EQ, 1, Q_NEWRULE,
47 Q_MOD_10 | Q_BETWEEN, 2, 4, Q_AND, Q_MOD_100 | Q_NOT_BETWEEN, 10, 19 };
48static const uchar romanianRules[] =
49 { Q_EQ, 1, Q_NEWRULE,
50 Q_EQ, 0, Q_OR, Q_MOD_100 | Q_BETWEEN, 1, 19 };
51static const uchar slovenianRules[] =
52 { Q_MOD_100 | Q_EQ, 1, Q_NEWRULE,
53 Q_MOD_100 | Q_EQ, 2, Q_NEWRULE,
54 Q_MOD_100 | Q_BETWEEN, 3, 4 };
55static const uchar malteseRules[] =
56 { Q_EQ, 1, Q_NEWRULE,
57 Q_EQ, 0, Q_OR, Q_MOD_100 | Q_BETWEEN, 1, 10, Q_NEWRULE,
58 Q_MOD_100 | Q_BETWEEN, 11, 19 };
59static const uchar welshRules[] =
60 { Q_EQ, 0, Q_NEWRULE,
61 Q_EQ, 1, Q_NEWRULE,
62 Q_BETWEEN, 2, 5, Q_NEWRULE,
63 Q_EQ, 6 };
64static const uchar arabicRules[] =
65 { Q_EQ, 0, Q_NEWRULE,
66 Q_EQ, 1, Q_NEWRULE,
67 Q_EQ, 2, Q_NEWRULE,
68 Q_MOD_100 | Q_BETWEEN, 3, 10, Q_NEWRULE,
69 Q_MOD_100 | Q_GEQ, 11 };
70static const uchar tagalogRules[] =
71 { Q_LEQ, 1, Q_NEWRULE,
72 Q_MOD_10 | Q_EQ, 4, Q_OR, Q_MOD_10 | Q_EQ, 6, Q_OR, Q_MOD_10 | Q_EQ, 9 };
73
74static const char * const japaneseStyleForms[] = { "Universal Form", 0 };
75static const char * const englishStyleForms[] = { "Singular", "Plural", 0 };
76static const char * const frenchStyleForms[] = { "Singular", "Plural", 0 };
77static const char * const icelandicForms[] = { "Singular", "Plural", 0 };
78static const char * const latvianForms[] = { "Singular", "Plural", "Nullar", 0 };
79static const char * const irishStyleForms[] = { "Singular", "Dual", "Plural", 0 };
80// Gaelic uses the grammatical Singular for the Plural cardinality,
81// so using the Latin terms is expected to cause confusion.
82static const char * const gaelicStyleForms[] = { "1/11", "2/12", "Few", "Many", 0 };
83static const char * const slovakStyleForms[] = { "Singular", "Paucal", "Plural", 0 };
84static const char * const macedonianForms[] = { "Singular", "Dual", "Plural", 0 };
85static const char * const lithuanianForms[] = { "Singular", "Paucal", "Plural", 0 };
86static const char * const russianStyleForms[] = { "Singular", "Dual", "Plural", 0 };
87static const char * const polishForms[] = { "Singular", "Paucal", "Plural", 0 };
88static const char * const romanianForms[] = { "Singular", "Paucal", "Plural", 0 };
89static const char * const slovenianForms[] = { "Singular", "Dual", "Trial", "Plural", 0 };
90static const char * const malteseForms[] =
91 { "Singular", "Paucal", "Greater Paucal", "Plural", 0 };
92static const char * const welshForms[] =
93 { "Nullar", "Singular", "Dual", "Sexal", "Plural", 0 };
94static const char * const arabicForms[] =
95 { "Nullar", "Singular", "Dual", "Minority Plural", "Plural", "Plural (100-102, ...)", 0 };
96static const char * const tagalogForms[] =
97 { "Singular", "Plural (consonant-ended)", "Plural (vowel-ended)", 0 };
98
99#define EOL QLocale::C
100
102 QLocale::Bislama,
103 QLocale::Burmese,
104 QLocale::Chinese,
105 QLocale::Dzongkha,
106 QLocale::Fijian,
107 QLocale::Guarani,
108 QLocale::Hungarian,
109 QLocale::Indonesian,
110 QLocale::Japanese,
111 QLocale::Javanese,
112 QLocale::Korean,
113 QLocale::Malay,
114 QLocale::NauruLanguage,
115 QLocale::Oromo,
116 QLocale::Persian,
117 QLocale::Sundanese,
118 QLocale::Tatar,
119 QLocale::Thai,
120 QLocale::Tibetan,
121 QLocale::Turkish,
122 QLocale::Vietnamese,
123 QLocale::Yoruba,
124 QLocale::Zhuang,
125 EOL
126};
127
129 QLocale::Abkhazian,
130 QLocale::Afar,
131 QLocale::Afrikaans,
132 QLocale::Albanian,
133 QLocale::Amharic,
134 QLocale::Assamese,
135 QLocale::Aymara,
136 QLocale::Azerbaijani,
137 QLocale::Bashkir,
138 QLocale::Basque,
139 QLocale::Bengali,
140 QLocale::Bulgarian,
141 QLocale::Catalan,
142 QLocale::Cornish,
143 QLocale::Corsican,
144 QLocale::Danish,
145 QLocale::Dutch,
146 QLocale::English,
147 QLocale::Esperanto,
148 QLocale::Estonian,
149 QLocale::Faroese,
150 QLocale::Finnish,
151 QLocale::Friulian,
152 QLocale::WesternFrisian,
153 QLocale::Galician,
154 QLocale::Ganda,
155 QLocale::Georgian,
156 QLocale::German,
157 QLocale::Greek,
158 QLocale::Greenlandic,
159 QLocale::Gujarati,
160 QLocale::Hausa,
161 QLocale::Hebrew,
162 QLocale::Hindi,
163 QLocale::Interlingua,
164 QLocale::Interlingue,
165 QLocale::Italian,
166 QLocale::Kannada,
167 QLocale::Kashmiri,
168 QLocale::Kazakh,
169 QLocale::Khmer,
170 QLocale::Kinyarwanda,
171 QLocale::Kirghiz,
172 QLocale::Kurdish,
173 QLocale::Lao,
174 QLocale::Latin,
175 QLocale::Lingala,
176 QLocale::Luxembourgish,
177 QLocale::Malagasy,
178 QLocale::Malayalam,
179 QLocale::Marathi,
180 QLocale::Mongolian,
181 // Missing: Nahuatl,
182 QLocale::Nepali,
183 QLocale::NorthernSotho,
184 QLocale::NorwegianBokmal,
185 QLocale::NorwegianNynorsk,
186 QLocale::Occitan,
187 QLocale::Oriya,
188 QLocale::Pashto,
189 QLocale::Portuguese,
190 QLocale::Punjabi,
191 QLocale::Quechua,
192 QLocale::Romansh,
193 QLocale::Rundi,
194 QLocale::Shona,
195 QLocale::Sindhi,
196 QLocale::Sinhala,
197 QLocale::Somali,
198 QLocale::SouthernSotho,
199 QLocale::Spanish,
200 QLocale::Swahili,
201 QLocale::Swati,
202 QLocale::Swedish,
203 QLocale::Tajik,
204 QLocale::Tamil,
205 QLocale::Telugu,
206 QLocale::Tongan,
207 QLocale::Tsonga,
208 QLocale::Tswana,
209 QLocale::Turkmen,
210 QLocale::Uigur,
211 QLocale::Urdu,
212 QLocale::Uzbek,
213 QLocale::Volapuk,
214 QLocale::Wolof,
215 QLocale::Xhosa,
216 QLocale::Yiddish,
217 QLocale::Zulu,
218 EOL
219};
221 // keep synchronized with frenchStyleCountries
222 QLocale::Armenian,
223 QLocale::Breton,
224 QLocale::French,
225 QLocale::Portuguese,
226 QLocale::Filipino,
227 QLocale::Tigrinya,
228 QLocale::Walloon,
229 EOL
230};
231static const QLocale::Language latvianLanguage[] = { QLocale::Latvian, EOL };
232static const QLocale::Language icelandicLanguage[] = { QLocale::Icelandic, EOL };
234 QLocale::Divehi,
235 QLocale::Inuktitut,
236 QLocale::Inupiak,
237 QLocale::Irish,
238 QLocale::Manx,
239 QLocale::Maori,
240 QLocale::NorthernSami,
241 QLocale::Samoan,
242 QLocale::Sanskrit,
243 EOL
244};
245static const QLocale::Language gaelicStyleLanguages[] = { QLocale::Gaelic, EOL };
246static const QLocale::Language slovakStyleLanguages[] = { QLocale::Slovak, QLocale::Czech, EOL };
247static const QLocale::Language macedonianLanguage[] = { QLocale::Macedonian, EOL };
248static const QLocale::Language lithuanianLanguage[] = { QLocale::Lithuanian, EOL };
250 QLocale::Bosnian,
251 QLocale::Belarusian,
252 QLocale::Croatian,
253 QLocale::Russian,
254 QLocale::Serbian,
255 QLocale::Ukrainian,
256 EOL
257};
258static const QLocale::Language polishLanguage[] = { QLocale::Polish, EOL };
260 QLocale::Romanian,
261 EOL
262};
263static const QLocale::Language slovenianLanguage[] = { QLocale::Slovenian, EOL };
264static const QLocale::Language malteseLanguage[] = { QLocale::Maltese, EOL };
265static const QLocale::Language welshLanguage[] = { QLocale::Welsh, EOL };
266static const QLocale::Language arabicLanguage[] = { QLocale::Arabic, EOL };
267static const QLocale::Language tagalogLanguage[] = { QLocale::Filipino, EOL };
268
270 // keep synchronized with frenchStyleLanguages
271 QLocale::AnyTerritory,
272 QLocale::AnyTerritory,
273 QLocale::AnyTerritory,
274 QLocale::Brazil,
275 QLocale::AnyTerritory,
276 QLocale::AnyTerritory,
277 QLocale::AnyTerritory
278};
280 const uchar *rules;
282 const char * const *forms;
285 const char * const gettextRules;
286};
287
289 { 0, 0, japaneseStyleForms, japaneseStyleLanguages, 0, "nplurals=1; plural=0;" },
290 { englishStyleRules, sizeof(englishStyleRules), englishStyleForms, englishStyleLanguages, 0,
291 "nplurals=2; plural=(n != 1);" },
292 { frenchStyleRules, sizeof(frenchStyleRules), frenchStyleForms, frenchStyleLanguages,
293 frenchStyleCountries, "nplurals=2; plural=(n > 1);" },
294 { latvianRules, sizeof(latvianRules), latvianForms, latvianLanguage, 0,
295 "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2);" },
296 { icelandicRules, sizeof(icelandicRules), icelandicForms, icelandicLanguage, 0,
297 "nplurals=2; plural=(n%10==1 && n%100!=11 ? 0 : 1);" },
298 { irishStyleRules, sizeof(irishStyleRules), irishStyleForms, irishStyleLanguages, 0,
299 "nplurals=3; plural=(n==1 ? 0 : n==2 ? 1 : 2);" },
300 { gaelicStyleRules, sizeof(gaelicStyleRules), gaelicStyleForms, gaelicStyleLanguages, 0,
301 "nplurals=4; plural=(n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3;" },
302 { slovakStyleRules, sizeof(slovakStyleRules), slovakStyleForms, slovakStyleLanguages, 0,
303 "nplurals=3; plural=((n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2);" },
304 { macedonianRules, sizeof(macedonianRules), macedonianForms, macedonianLanguage, 0,
305 "nplurals=3; plural=(n%100==1 ? 0 : n%100==2 ? 1 : 2);" },
306 { lithuanianRules, sizeof(lithuanianRules), lithuanianForms, lithuanianLanguage, 0,
307 "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);" },
308 { russianStyleRules, sizeof(russianStyleRules), russianStyleForms, russianStyleLanguages, 0,
309 "nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);" },
310 { polishRules, sizeof(polishRules), polishForms, polishLanguage, 0,
311 "nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);" },
312 { romanianRules, sizeof(romanianRules), romanianForms, romanianLanguages, 0,
313 "nplurals=3; plural=(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);" },
314 { slovenianRules, sizeof(slovenianRules), slovenianForms, slovenianLanguage, 0,
315 "nplurals=4; plural=(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3);" },
316 { malteseRules, sizeof(malteseRules), malteseForms, malteseLanguage, 0,
317 "nplurals=4; plural=(n==1 ? 0 : (n==0 || (n%100>=1 && n%100<=10)) ? 1 : (n%100>=11 && n%100<=19) ? 2 : 3);" },
318 { welshRules, sizeof(welshRules), welshForms, welshLanguage, 0,
319 "nplurals=5; plural=(n==0 ? 0 : n==1 ? 1 : (n>=2 && n<=5) ? 2 : n==6 ? 3 : 4);" },
320 { arabicRules, sizeof(arabicRules), arabicForms, arabicLanguage, 0,
321 "nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : (n%100>=3 && n%100<=10) ? 3 : n%100>=11 ? 4 : 5);" },
322 { tagalogRules, sizeof(tagalogRules), tagalogForms, tagalogLanguage, 0,
323 "nplurals=3; plural=(n==1 ? 0 : (n%10==4 || n%10==6 || n%10== 9) ? 1 : 2);" },
324};
325
326static const int NumerusTableSize = sizeof(numerusTable) / sizeof(numerusTable[0]);
327
328bool getNumerusInfo(QLocale::Language language, QLocale::Territory country,
329 QByteArray *rules, QStringList *forms, const char **gettextRules)
330{
331 while (true) {
332 for (int i = 0; i < NumerusTableSize; ++i) {
333 const NumerusTableEntry &entry = numerusTable[i];
334 for (int j = 0; entry.languages[j] != EOL; ++j) {
335 if (entry.languages[j] == language
336 && ((!entry.countries && country == QLocale::AnyTerritory)
337 || (entry.countries && entry.countries[j] == country))) {
338 if (rules) {
339 *rules = QByteArray::fromRawData(reinterpret_cast<const char *>(entry.rules),
340 entry.rulesSize);
341 }
342 if (gettextRules)
343 *gettextRules = entry.gettextRules;
344 if (forms) {
345 forms->clear();
346 for (int k = 0; entry.forms[k]; ++k)
347 forms->append(QLatin1String(entry.forms[k]));
348 }
349 return true;
350 }
351 }
352 }
353
354 if (country == QLocale::AnyTerritory)
355 break;
356 country = QLocale::AnyTerritory;
357 }
358 return false;
359}
360
362{
363 QStringList langs;
364
365 for (int i = 0; i < NumerusTableSize; ++i) {
366 const NumerusTableEntry &entry = numerusTable[i];
367 for (int j = 0; entry.languages[j] != EOL; ++j) {
368 QLocale loc(entry.languages[j], entry.countries ? entry.countries[j]
369 : QLocale::AnyTerritory);
370 QString lang = QLocale::languageToString(entry.languages[j]);
371 if (loc.language() == QLocale::C)
372 lang += QLatin1String(" (!!!)");
373 else if (entry.countries && entry.countries[j] != QLocale::AnyTerritory)
374 lang += QLatin1String(" (%1)").arg(QLocale::territoryToString(loc.territory()));
375 else
376 lang += QLatin1String(" [%1]").arg(QLocale::territoryToString(loc.territory()));
377 langs << QString::fromLatin1("%1 %2 %3\n").arg(lang, -40).arg(loc.name(), -8)
378 .arg(QString::fromLatin1(entry.gettextRules));
379 }
380 }
381 langs.sort();
382 return langs.join(QString());
383}
384
385QT_END_NAMESPACE
Combined button and popup list for selecting options.
static const QLocale::Language englishStyleLanguages[]
Definition numerus.cpp:128
static const char *const russianStyleForms[]
Definition numerus.cpp:86
static const QLocale::Language romanianLanguages[]
Definition numerus.cpp:259
static const uchar welshRules[]
Definition numerus.cpp:59
static const uchar slovenianRules[]
Definition numerus.cpp:51
static QT_BEGIN_NAMESPACE const uchar englishStyleRules[]
Definition numerus.cpp:17
static const QLocale::Language japaneseStyleLanguages[]
Definition numerus.cpp:101
static const QLocale::Language icelandicLanguage[]
Definition numerus.cpp:232
static const QLocale::Language welshLanguage[]
Definition numerus.cpp:265
static const char *const romanianForms[]
Definition numerus.cpp:88
static const QLocale::Language tagalogLanguage[]
Definition numerus.cpp:267
static const char *const lithuanianForms[]
Definition numerus.cpp:85
static const uchar polishRules[]
Definition numerus.cpp:45
static const QLocale::Language arabicLanguage[]
Definition numerus.cpp:266
static const QLocale::Language polishLanguage[]
Definition numerus.cpp:258
static const QLocale::Territory frenchStyleCountries[]
Definition numerus.cpp:269
#define EOL
Definition numerus.cpp:99
static const QLocale::Language gaelicStyleLanguages[]
Definition numerus.cpp:245
static const QLocale::Language frenchStyleLanguages[]
Definition numerus.cpp:220
static const char *const japaneseStyleForms[]
Definition numerus.cpp:74
static const QLocale::Language macedonianLanguage[]
Definition numerus.cpp:247
static const uchar slovakStyleRules[]
Definition numerus.cpp:33
static const QLocale::Language malteseLanguage[]
Definition numerus.cpp:264
static const QLocale::Language slovenianLanguage[]
Definition numerus.cpp:263
static const char *const slovenianForms[]
Definition numerus.cpp:89
static const uchar tagalogRules[]
Definition numerus.cpp:70
static const uchar romanianRules[]
Definition numerus.cpp:48
static const uchar gaelicStyleRules[]
Definition numerus.cpp:29
static const char *const macedonianForms[]
Definition numerus.cpp:84
static const char *const icelandicForms[]
Definition numerus.cpp:77
static const uchar icelandicRules[]
Definition numerus.cpp:24
static const char *const welshForms[]
Definition numerus.cpp:92
static const char *const tagalogForms[]
Definition numerus.cpp:96
static const int NumerusTableSize
Definition numerus.cpp:326
static const NumerusTableEntry numerusTable[]
Definition numerus.cpp:288
static const char *const irishStyleForms[]
Definition numerus.cpp:79
static const QLocale::Language irishStyleLanguages[]
Definition numerus.cpp:233
static const char *const englishStyleForms[]
Definition numerus.cpp:75
static const char *const polishForms[]
Definition numerus.cpp:87
static const QLocale::Language lithuanianLanguage[]
Definition numerus.cpp:248
static const char *const frenchStyleForms[]
Definition numerus.cpp:76
static const uchar arabicRules[]
Definition numerus.cpp:64
static const uchar macedonianRules[]
Definition numerus.cpp:36
static const uchar irishStyleRules[]
Definition numerus.cpp:26
static const uchar frenchStyleRules[]
Definition numerus.cpp:19
static const uchar lithuanianRules[]
Definition numerus.cpp:39
static const char *const latvianForms[]
Definition numerus.cpp:78
static const uchar latvianRules[]
Definition numerus.cpp:21
static const char *const gaelicStyleForms[]
Definition numerus.cpp:82
static const char *const arabicForms[]
Definition numerus.cpp:94
static const QLocale::Language latvianLanguage[]
Definition numerus.cpp:231
static const QLocale::Language russianStyleLanguages[]
Definition numerus.cpp:249
static const char *const malteseForms[]
Definition numerus.cpp:90
static const QLocale::Language slovakStyleLanguages[]
Definition numerus.cpp:246
static const uchar malteseRules[]
Definition numerus.cpp:55
static const uchar russianStyleRules[]
Definition numerus.cpp:42
static const char *const slovakStyleForms[]
Definition numerus.cpp:83
const uchar * rules
Definition numerus.cpp:280
const char *const * forms
Definition numerus.cpp:282
const char *const gettextRules
Definition numerus.cpp:285
const QLocale::Language * languages
Definition numerus.cpp:283
const QLocale::Territory * countries
Definition numerus.cpp:284
QString getNumerusInfoString()
Definition numerus.cpp:361
bool getNumerusInfo(QLocale::Language language, QLocale::Territory territory, QByteArray *rules, QStringList *forms, const char **gettextRules)
Definition numerus.cpp:328