author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qplatformdefs.h" |
|
43 |
||
44 |
#include "qtranslator.h" |
|
45 |
||
46 |
#ifndef QT_NO_TRANSLATION |
|
47 |
||
48 |
#include "qfileinfo.h" |
|
49 |
#include "qstring.h" |
|
50 |
#include "qcoreapplication.h" |
|
51 |
#include "qcoreapplication_p.h" |
|
52 |
#include "qdatastream.h" |
|
53 |
#include "qfile.h" |
|
54 |
#include "qmap.h" |
|
55 |
#include "qalgorithms.h" |
|
56 |
#include "qhash.h" |
|
57 |
#include "qtranslator_p.h" |
|
58 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
59 |
#if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN) |
0 | 60 |
#define QT_USE_MMAP |
61 |
#include "private/qcore_unix_p.h" |
|
62 |
#endif |
|
63 |
||
64 |
// most of the headers below are already included in qplatformdefs.h |
|
65 |
// also this lacks Large File support but that's probably irrelevant |
|
66 |
#if defined(QT_USE_MMAP) |
|
67 |
// for mmap |
|
68 |
#include <sys/mman.h> |
|
69 |
#include <errno.h> |
|
70 |
#endif |
|
71 |
||
72 |
#include <stdlib.h> |
|
73 |
||
74 |
#include "qobject_p.h" |
|
75 |
||
76 |
QT_BEGIN_NAMESPACE |
|
77 |
||
78 |
enum Tag { Tag_End = 1, Tag_SourceText16, Tag_Translation, Tag_Context16, Tag_Obsolete1, |
|
79 |
Tag_SourceText, Tag_Context, Tag_Comment, Tag_Obsolete2 }; |
|
80 |
/* |
|
81 |
$ mcookie |
|
82 |
3cb86418caef9c95cd211cbf60a1bddd |
|
83 |
$ |
|
84 |
*/ |
|
85 |
||
86 |
// magic number for the file |
|
87 |
static const int MagicLength = 16; |
|
88 |
static const uchar magic[MagicLength] = { |
|
89 |
0x3c, 0xb8, 0x64, 0x18, 0xca, 0xef, 0x9c, 0x95, |
|
90 |
0xcd, 0x21, 0x1c, 0xbf, 0x60, 0xa1, 0xbd, 0xdd |
|
91 |
}; |
|
92 |
||
93 |
static bool match(const uchar* found, const char* target, uint len) |
|
94 |
{ |
|
95 |
// catch the case if \a found has a zero-terminating symbol and \a len includes it. |
|
96 |
// (normalize it to be without the zero-terminating symbol) |
|
97 |
if (len > 0 && found[len-1] == '\0') |
|
98 |
--len; |
|
99 |
return (memcmp(found, target, len) == 0 && target[len] == '\0'); |
|
100 |
} |
|
101 |
||
102 |
static uint elfHash(const char *name) |
|
103 |
{ |
|
104 |
const uchar *k; |
|
105 |
uint h = 0; |
|
106 |
uint g; |
|
107 |
||
108 |
if (name) { |
|
109 |
k = (const uchar *) name; |
|
110 |
while (*k) { |
|
111 |
h = (h << 4) + *k++; |
|
112 |
if ((g = (h & 0xf0000000)) != 0) |
|
113 |
h ^= g >> 24; |
|
114 |
h &= ~g; |
|
115 |
} |
|
116 |
} |
|
117 |
if (!h) |
|
118 |
h = 1; |
|
119 |
return h; |
|
120 |
} |
|
121 |
||
122 |
static int numerusHelper(int n, const uchar *rules, int rulesSize) |
|
123 |
{ |
|
124 |
#define CHECK_RANGE \ |
|
125 |
do { \ |
|
126 |
if (i >= rulesSize) \ |
|
127 |
return -1; \ |
|
128 |
} while (0) |
|
129 |
||
130 |
int result = 0; |
|
131 |
int i = 0; |
|
132 |
||
133 |
if (rulesSize == 0) |
|
134 |
return 0; |
|
135 |
||
136 |
for (;;) { |
|
137 |
bool orExprTruthValue = false; |
|
138 |
||
139 |
for (;;) { |
|
140 |
bool andExprTruthValue = true; |
|
141 |
||
142 |
for (;;) { |
|
143 |
bool truthValue = true; |
|
144 |
||
145 |
CHECK_RANGE; |
|
146 |
int opcode = rules[i++]; |
|
147 |
||
148 |
int leftOperand = n; |
|
149 |
if (opcode & Q_MOD_10) { |
|
150 |
leftOperand %= 10; |
|
151 |
} else if (opcode & Q_MOD_100) { |
|
152 |
leftOperand %= 100; |
|
153 |
} else if (opcode & Q_LEAD_1000) { |
|
154 |
while (leftOperand >= 1000) |
|
155 |
leftOperand /= 1000; |
|
156 |
} |
|
157 |
||
158 |
int op = opcode & Q_OP_MASK; |
|
159 |
||
160 |
CHECK_RANGE; |
|
161 |
int rightOperand = rules[i++]; |
|
162 |
||
163 |
switch (op) { |
|
164 |
default: |
|
165 |
return -1; |
|
166 |
case Q_EQ: |
|
167 |
truthValue = (leftOperand == rightOperand); |
|
168 |
break; |
|
169 |
case Q_LT: |
|
170 |
truthValue = (leftOperand < rightOperand); |
|
171 |
break; |
|
172 |
case Q_LEQ: |
|
173 |
truthValue = (leftOperand <= rightOperand); |
|
174 |
break; |
|
175 |
case Q_BETWEEN: |
|
176 |
int bottom = rightOperand; |
|
177 |
CHECK_RANGE; |
|
178 |
int top = rules[i++]; |
|
179 |
truthValue = (leftOperand >= bottom && leftOperand <= top); |
|
180 |
} |
|
181 |
||
182 |
if (opcode & Q_NOT) |
|
183 |
truthValue = !truthValue; |
|
184 |
||
185 |
andExprTruthValue = andExprTruthValue && truthValue; |
|
186 |
||
187 |
if (i == rulesSize || rules[i] != Q_AND) |
|
188 |
break; |
|
189 |
++i; |
|
190 |
} |
|
191 |
||
192 |
orExprTruthValue = orExprTruthValue || andExprTruthValue; |
|
193 |
||
194 |
if (i == rulesSize || rules[i] != Q_OR) |
|
195 |
break; |
|
196 |
++i; |
|
197 |
} |
|
198 |
||
199 |
if (orExprTruthValue) |
|
200 |
return result; |
|
201 |
||
202 |
++result; |
|
203 |
||
204 |
if (i == rulesSize) |
|
205 |
return result; |
|
206 |
||
207 |
if (rules[i++] != Q_NEWRULE) |
|
208 |
break; |
|
209 |
} |
|
210 |
return -1; |
|
211 |
} |
|
212 |
||
213 |
class QTranslatorPrivate : public QObjectPrivate |
|
214 |
{ |
|
215 |
Q_DECLARE_PUBLIC(QTranslator) |
|
216 |
public: |
|
217 |
enum { Contexts = 0x2f, Hashes = 0x42, Messages = 0x69, NumerusRules = 0x88 }; |
|
218 |
||
219 |
QTranslatorPrivate() |
|
220 |
: used_mmap(0), unmapPointer(0), unmapLength(0), |
|
221 |
messageArray(0), offsetArray(0), contextArray(0), numerusRulesArray(0), |
|
222 |
messageLength(0), offsetLength(0), contextLength(0), numerusRulesLength(0) {} |
|
223 |
||
224 |
// for mmap'ed files, this is what needs to be unmapped. |
|
225 |
bool used_mmap : 1; |
|
226 |
char *unmapPointer; |
|
227 |
unsigned int unmapLength; |
|
228 |
||
229 |
// for squeezed but non-file data, this is what needs to be deleted |
|
230 |
const uchar *messageArray; |
|
231 |
const uchar *offsetArray; |
|
232 |
const uchar *contextArray; |
|
233 |
const uchar *numerusRulesArray; |
|
234 |
uint messageLength; |
|
235 |
uint offsetLength; |
|
236 |
uint contextLength; |
|
237 |
uint numerusRulesLength; |
|
238 |
||
239 |
bool do_load(const uchar *data, int len); |
|
240 |
QString do_translate(const char *context, const char *sourceText, const char *comment, |
|
241 |
int n) const; |
|
242 |
void clear(); |
|
243 |
}; |
|
244 |
||
245 |
/*! |
|
246 |
\class QTranslator |
|
247 |
||
248 |
\brief The QTranslator class provides internationalization support for text |
|
249 |
output. |
|
250 |
||
251 |
\ingroup i18n |
|
252 |
||
253 |
An object of this class contains a set of translations from a |
|
254 |
source language to a target language. QTranslator provides |
|
255 |
functions to look up translations in a translation file. |
|
256 |
Translation files are created using \l{Qt Linguist}. |
|
257 |
||
258 |
The most common use of QTranslator is to: load a translation |
|
259 |
file, install it using QApplication::installTranslator(), and use |
|
260 |
it via QObject::tr(). Here's the \c main() function from the |
|
261 |
\l{linguist/hellotr}{Hello tr()} example: |
|
262 |
||
263 |
\snippet examples/linguist/hellotr/main.cpp 2 |
|
264 |
||
265 |
Note that the translator must be created \e before the |
|
266 |
application's widgets. |
|
267 |
||
268 |
Most applications will never need to do anything else with this |
|
269 |
class. The other functions provided by this class are useful for |
|
270 |
applications that work on translator files. |
|
271 |
||
272 |
\section1 Looking up Translations |
|
273 |
||
274 |
It is possible to look up a translation using translate() (as tr() |
|
275 |
and QApplication::translate() do). The translate() function takes |
|
276 |
up to three parameters: |
|
277 |
||
278 |
\list |
|
279 |
\o The \e context - usually the class name for the tr() caller. |
|
280 |
\o The \e {source text} - usually the argument to tr(). |
|
281 |
\o The \e disambiguation - an optional string that helps disambiguate |
|
282 |
different uses of the same text in the same context. |
|
283 |
\endlist |
|
284 |
||
285 |
For example, the "Cancel" in a dialog might have "Anuluj" when the |
|
286 |
program runs in Polish (in this case the source text would be |
|
287 |
"Cancel"). The context would (normally) be the dialog's class |
|
288 |
name; there would normally be no comment, and the translated text |
|
289 |
would be "Anuluj". |
|
290 |
||
291 |
But it's not always so simple. The Spanish version of a printer |
|
292 |
dialog with settings for two-sided printing and binding would |
|
293 |
probably require both "Activado" and "Activada" as translations |
|
294 |
for "Enabled". In this case the source text would be "Enabled" in |
|
295 |
both cases, and the context would be the dialog's class name, but |
|
296 |
the two items would have disambiguations such as "two-sided printing" |
|
297 |
for one and "binding" for the other. The disambiguation enables the |
|
298 |
translator to choose the appropriate gender for the Spanish version, |
|
299 |
and enables Qt to distinguish between translations. |
|
300 |
||
301 |
\section1 Using Multiple Translations |
|
302 |
||
303 |
Multiple translation files can be installed in an application. |
|
304 |
Translations are searched for in the reverse order in which they were |
|
305 |
installed, so the most recently installed translation file is searched |
|
306 |
for translations first and the earliest translation file is searched |
|
307 |
last. The search stops as soon as a translation containing a matching |
|
308 |
string is found. |
|
309 |
||
310 |
This mechanism makes it possible for a specific translation to be |
|
311 |
"selected" or given priority over the others; simply uninstall the |
|
312 |
translator from the application by passing it to the |
|
313 |
QApplication::removeTranslator() function and reinstall it with |
|
314 |
QApplication::installTranslator(). It will then be the first |
|
315 |
translation to be searched for matching strings. |
|
316 |
||
317 |
\sa QApplication::installTranslator(), QApplication::removeTranslator(), |
|
318 |
QObject::tr(), QApplication::translate(), {I18N Example}, |
|
319 |
{Hello tr() Example}, {Arrow Pad Example}, {Troll Print Example} |
|
320 |
*/ |
|
321 |
||
322 |
/*! |
|
323 |
Constructs an empty message file object with parent \a parent that |
|
324 |
is not connected to any file. |
|
325 |
*/ |
|
326 |
||
327 |
QTranslator::QTranslator(QObject * parent) |
|
328 |
: QObject(*new QTranslatorPrivate, parent) |
|
329 |
{ |
|
330 |
} |
|
331 |
||
332 |
#ifdef QT3_SUPPORT |
|
333 |
/*! |
|
334 |
\overload QTranslator() |
|
335 |
\obsolete |
|
336 |
*/ |
|
337 |
QTranslator::QTranslator(QObject * parent, const char * name) |
|
338 |
: QObject(*new QTranslatorPrivate, parent) |
|
339 |
{ |
|
340 |
setObjectName(QString::fromAscii(name)); |
|
341 |
} |
|
342 |
#endif |
|
343 |
||
344 |
/*! |
|
345 |
Destroys the object and frees any allocated resources. |
|
346 |
*/ |
|
347 |
||
348 |
QTranslator::~QTranslator() |
|
349 |
{ |
|
350 |
if (QCoreApplication::instance()) |
|
351 |
QCoreApplication::removeTranslator(this); |
|
352 |
Q_D(QTranslator); |
|
353 |
d->clear(); |
|
354 |
} |
|
355 |
||
356 |
/*! |
|
357 |
Loads \a filename + \a suffix (".qm" if the \a suffix is |
|
358 |
not specified), which may be an absolute file name or relative |
|
359 |
to \a directory. Returns true if the translation is successfully |
|
360 |
loaded; otherwise returns false. |
|
361 |
||
362 |
The previous contents of this translator object are discarded. |
|
363 |
||
364 |
If the file name does not exist, other file names are tried |
|
365 |
in the following order: |
|
366 |
||
367 |
\list 1 |
|
368 |
\o File name without \a suffix appended. |
|
369 |
\o File name with text after a character in \a search_delimiters |
|
370 |
stripped ("_." is the default for \a search_delimiters if it is |
|
371 |
an empty string) and \a suffix. |
|
372 |
\o File name stripped without \a suffix appended. |
|
373 |
\o File name stripped further, etc. |
|
374 |
\endlist |
|
375 |
||
376 |
For example, an application running in the fr_CA locale |
|
377 |
(French-speaking Canada) might call load("foo.fr_ca", |
|
378 |
"/opt/foolib"). load() would then try to open the first existing |
|
379 |
readable file from this list: |
|
380 |
||
381 |
\list 1 |
|
382 |
\o \c /opt/foolib/foo.fr_ca.qm |
|
383 |
\o \c /opt/foolib/foo.fr_ca |
|
384 |
\o \c /opt/foolib/foo.fr.qm |
|
385 |
\o \c /opt/foolib/foo.fr |
|
386 |
\o \c /opt/foolib/foo.qm |
|
387 |
\o \c /opt/foolib/foo |
|
388 |
\endlist |
|
389 |
*/ |
|
390 |
||
391 |
bool QTranslator::load(const QString & filename, const QString & directory, |
|
392 |
const QString & search_delimiters, |
|
393 |
const QString & suffix) |
|
394 |
{ |
|
395 |
Q_D(QTranslator); |
|
396 |
d->clear(); |
|
397 |
||
398 |
QString prefix; |
|
399 |
if (QFileInfo(filename).isRelative()) { |
|
400 |
prefix = directory; |
|
401 |
if (prefix.length() && !prefix.endsWith(QLatin1Char('/'))) |
|
402 |
prefix += QLatin1Char('/'); |
|
403 |
} |
|
404 |
||
405 |
QString fname = filename; |
|
406 |
QString realname; |
|
407 |
QString delims; |
|
408 |
delims = search_delimiters.isNull() ? QString::fromLatin1("_.") : search_delimiters; |
|
409 |
||
410 |
for (;;) { |
|
411 |
QFileInfo fi; |
|
412 |
||
413 |
realname = prefix + fname + (suffix.isNull() ? QString::fromLatin1(".qm") : suffix); |
|
414 |
fi.setFile(realname); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
415 |
if (fi.isReadable() && fi.isFile()) |
0 | 416 |
break; |
417 |
||
418 |
realname = prefix + fname; |
|
419 |
fi.setFile(realname); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
420 |
if (fi.isReadable() && fi.isFile()) |
0 | 421 |
break; |
422 |
||
423 |
int rightmost = 0; |
|
424 |
for (int i = 0; i < (int)delims.length(); i++) { |
|
425 |
int k = fname.lastIndexOf(delims[i]); |
|
426 |
if (k > rightmost) |
|
427 |
rightmost = k; |
|
428 |
} |
|
429 |
||
430 |
// no truncations? fail |
|
431 |
if (rightmost == 0) |
|
432 |
return false; |
|
433 |
||
434 |
fname.truncate(rightmost); |
|
435 |
} |
|
436 |
||
437 |
// realname is now the fully qualified name of a readable file. |
|
438 |
||
439 |
bool ok = false; |
|
440 |
||
441 |
#ifdef QT_USE_MMAP |
|
442 |
||
443 |
#ifndef MAP_FILE |
|
444 |
#define MAP_FILE 0 |
|
445 |
#endif |
|
446 |
#ifndef MAP_FAILED |
|
447 |
#define MAP_FAILED -1 |
|
448 |
#endif |
|
449 |
||
450 |
int fd = -1; |
|
451 |
if (!realname.startsWith(QLatin1Char(':'))) |
|
452 |
fd = QT_OPEN(QFile::encodeName(realname), O_RDONLY, |
|
453 |
#if defined(Q_OS_WIN) |
|
454 |
_S_IREAD | _S_IWRITE |
|
455 |
#else |
|
456 |
0666 |
|
457 |
#endif |
|
458 |
); |
|
459 |
if (fd >= 0) { |
|
460 |
QT_STATBUF st; |
|
461 |
if (!QT_FSTAT(fd, &st)) { |
|
462 |
char *ptr; |
|
463 |
ptr = reinterpret_cast<char *>( |
|
464 |
mmap(0, st.st_size, // any address, whole file |
|
465 |
PROT_READ, // read-only memory |
|
466 |
MAP_FILE | MAP_PRIVATE, // swap-backed map from file |
|
467 |
fd, 0)); // from offset 0 of fd |
|
468 |
if (ptr && ptr != reinterpret_cast<char *>(MAP_FAILED)) { |
|
469 |
d->used_mmap = true; |
|
470 |
d->unmapPointer = ptr; |
|
471 |
d->unmapLength = st.st_size; |
|
472 |
ok = true; |
|
473 |
} |
|
474 |
} |
|
475 |
::close(fd); |
|
476 |
} |
|
477 |
#endif // QT_USE_MMAP |
|
478 |
||
479 |
if (!ok) { |
|
480 |
QFile file(realname); |
|
481 |
d->unmapLength = file.size(); |
|
482 |
if (!d->unmapLength) |
|
483 |
return false; |
|
484 |
d->unmapPointer = new char[d->unmapLength]; |
|
485 |
||
486 |
if (file.open(QIODevice::ReadOnly)) |
|
487 |
ok = (d->unmapLength == (uint)file.read(d->unmapPointer, d->unmapLength)); |
|
488 |
||
489 |
if (!ok) { |
|
490 |
delete [] d->unmapPointer; |
|
491 |
d->unmapPointer = 0; |
|
492 |
d->unmapLength = 0; |
|
493 |
return false; |
|
494 |
} |
|
495 |
} |
|
496 |
||
497 |
return d->do_load(reinterpret_cast<const uchar *>(d->unmapPointer), d->unmapLength); |
|
498 |
} |
|
499 |
||
500 |
/*! |
|
501 |
\overload load() |
|
502 |
\fn bool QTranslator::load(const uchar *data, int len) |
|
503 |
||
504 |
Loads the QM file data \a data of length \a len into the |
|
505 |
translator. |
|
506 |
||
507 |
The data is not copied. The caller must be able to guarantee that \a data |
|
508 |
will not be deleted or modified. |
|
509 |
*/ |
|
510 |
bool QTranslator::load(const uchar *data, int len) |
|
511 |
{ |
|
512 |
Q_D(QTranslator); |
|
513 |
d->clear(); |
|
514 |
return d->do_load(data, len); |
|
515 |
} |
|
516 |
||
517 |
static quint8 read8(const uchar *data) |
|
518 |
{ |
|
519 |
return *data; |
|
520 |
} |
|
521 |
||
522 |
static quint16 read16(const uchar *data) |
|
523 |
{ |
|
524 |
return (data[0] << 8) | (data[1]); |
|
525 |
} |
|
526 |
||
527 |
static quint32 read32(const uchar *data) |
|
528 |
{ |
|
529 |
return (data[0] << 24) |
|
530 |
| (data[1] << 16) |
|
531 |
| (data[2] << 8) |
|
532 |
| (data[3]); |
|
533 |
} |
|
534 |
||
535 |
bool QTranslatorPrivate::do_load(const uchar *data, int len) |
|
536 |
{ |
|
537 |
if (!data || len < MagicLength || memcmp(data, magic, MagicLength)) |
|
538 |
return false; |
|
539 |
||
540 |
bool ok = true; |
|
541 |
const uchar *end = data + len; |
|
542 |
||
543 |
data += MagicLength; |
|
544 |
||
545 |
while (data < end - 4) { |
|
546 |
quint8 tag = read8(data++); |
|
547 |
quint32 blockLen = read32(data); |
|
548 |
data += 4; |
|
549 |
if (!tag || !blockLen) |
|
550 |
break; |
|
551 |
if (data + blockLen > end) { |
|
552 |
ok = false; |
|
553 |
break; |
|
554 |
} |
|
555 |
||
556 |
if (tag == QTranslatorPrivate::Contexts) { |
|
557 |
contextArray = data; |
|
558 |
contextLength = blockLen; |
|
559 |
} else if (tag == QTranslatorPrivate::Hashes) { |
|
560 |
offsetArray = data; |
|
561 |
offsetLength = blockLen; |
|
562 |
} else if (tag == QTranslatorPrivate::Messages) { |
|
563 |
messageArray = data; |
|
564 |
messageLength = blockLen; |
|
565 |
} else if (tag == QTranslatorPrivate::NumerusRules) { |
|
566 |
numerusRulesArray = data; |
|
567 |
numerusRulesLength = blockLen; |
|
568 |
} |
|
569 |
||
570 |
data += blockLen; |
|
571 |
} |
|
572 |
||
573 |
return ok; |
|
574 |
} |
|
575 |
||
576 |
static QString getMessage(const uchar *m, const uchar *end, const char *context, |
|
577 |
const char *sourceText, const char *comment, int numerus) |
|
578 |
{ |
|
579 |
const uchar *tn = 0; |
|
580 |
uint tn_length = 0; |
|
581 |
int currentNumerus = -1; |
|
582 |
||
583 |
for (;;) { |
|
584 |
uchar tag = 0; |
|
585 |
if (m < end) |
|
586 |
tag = read8(m++); |
|
587 |
switch((Tag)tag) { |
|
588 |
case Tag_End: |
|
589 |
goto end; |
|
590 |
case Tag_Translation: { |
|
591 |
int len = read32(m); |
|
592 |
if (len % 1) |
|
593 |
return QString(); |
|
594 |
m += 4; |
|
595 |
if (++currentNumerus == numerus) { |
|
596 |
tn_length = len; |
|
597 |
tn = m; |
|
598 |
} |
|
599 |
m += len; |
|
600 |
break; |
|
601 |
} |
|
602 |
case Tag_Obsolete1: |
|
603 |
m += 4; |
|
604 |
break; |
|
605 |
case Tag_SourceText: { |
|
606 |
quint32 len = read32(m); |
|
607 |
m += 4; |
|
608 |
if (!match(m, sourceText, len)) |
|
609 |
return QString(); |
|
610 |
m += len; |
|
611 |
} |
|
612 |
break; |
|
613 |
case Tag_Context: { |
|
614 |
quint32 len = read32(m); |
|
615 |
m += 4; |
|
616 |
if (!match(m, context, len)) |
|
617 |
return QString(); |
|
618 |
m += len; |
|
619 |
} |
|
620 |
break; |
|
621 |
case Tag_Comment: { |
|
622 |
quint32 len = read32(m); |
|
623 |
m += 4; |
|
624 |
if (*m && !match(m, comment, len)) |
|
625 |
return QString(); |
|
626 |
m += len; |
|
627 |
} |
|
628 |
break; |
|
629 |
default: |
|
630 |
return QString(); |
|
631 |
} |
|
632 |
} |
|
633 |
end: |
|
634 |
if (!tn) |
|
635 |
return QString(); |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
636 |
QString str = QString((const QChar *)tn, tn_length/2); |
0 | 637 |
if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) { |
638 |
for (int i = 0; i < str.length(); ++i) |
|
639 |
str[i] = QChar((str.at(i).unicode() >> 8) + ((str.at(i).unicode() << 8) & 0xff00)); |
|
640 |
} |
|
641 |
return str; |
|
642 |
} |
|
643 |
||
644 |
QString QTranslatorPrivate::do_translate(const char *context, const char *sourceText, |
|
645 |
const char *comment, int n) const |
|
646 |
{ |
|
647 |
if (context == 0) |
|
648 |
context = ""; |
|
649 |
if (sourceText == 0) |
|
650 |
sourceText = ""; |
|
651 |
if (comment == 0) |
|
652 |
comment = ""; |
|
653 |
||
654 |
if (!offsetLength) |
|
655 |
return QString(); |
|
656 |
||
657 |
/* |
|
658 |
Check if the context belongs to this QTranslator. If many |
|
659 |
translators are installed, this step is necessary. |
|
660 |
*/ |
|
661 |
if (contextLength) { |
|
662 |
quint16 hTableSize = read16(contextArray); |
|
663 |
uint g = elfHash(context) % hTableSize; |
|
664 |
const uchar *c = contextArray + 2 + (g << 1); |
|
665 |
quint16 off = read16(c); |
|
666 |
c += 2; |
|
667 |
if (off == 0) |
|
668 |
return QString(); |
|
669 |
c = contextArray + (2 + (hTableSize << 1) + (off << 1)); |
|
670 |
||
671 |
for (;;) { |
|
672 |
quint8 len = read8(c++); |
|
673 |
if (len == 0) |
|
674 |
return QString(); |
|
675 |
if (match(c, context, len)) |
|
676 |
break; |
|
677 |
c += len; |
|
678 |
} |
|
679 |
} |
|
680 |
||
681 |
size_t numItems = offsetLength / (2 * sizeof(quint32)); |
|
682 |
if (!numItems) |
|
683 |
return QString(); |
|
684 |
||
685 |
int numerus = 0; |
|
686 |
if (n >= 0) |
|
687 |
numerus = numerusHelper(n, numerusRulesArray, numerusRulesLength); |
|
688 |
||
689 |
for (;;) { |
|
690 |
quint32 h = elfHash(QByteArray(sourceText) + comment); |
|
691 |
||
692 |
const uchar *start = offsetArray; |
|
693 |
const uchar *end = start + ((numItems-1) << 3); |
|
694 |
while (start <= end) { |
|
695 |
const uchar *middle = start + (((end - start) >> 4) << 3); |
|
696 |
uint hash = read32(middle); |
|
697 |
if (h == hash) { |
|
698 |
start = middle; |
|
699 |
break; |
|
700 |
} else if (hash < h) { |
|
701 |
start = middle + 8; |
|
702 |
} else { |
|
703 |
end = middle - 8; |
|
704 |
} |
|
705 |
} |
|
706 |
||
707 |
if (start <= end) { |
|
708 |
// go back on equal key |
|
709 |
while (start != offsetArray && read32(start) == read32(start-8)) |
|
710 |
start -= 8; |
|
711 |
||
712 |
while (start < offsetArray + offsetLength) { |
|
713 |
quint32 rh = read32(start); |
|
714 |
start += 4; |
|
715 |
if (rh != h) |
|
716 |
break; |
|
717 |
quint32 ro = read32(start); |
|
718 |
start += 4; |
|
719 |
QString tn = getMessage(messageArray + ro, messageArray + messageLength, context, |
|
720 |
sourceText, comment, numerus); |
|
721 |
if (!tn.isNull()) |
|
722 |
return tn; |
|
723 |
} |
|
724 |
} |
|
725 |
if (!comment[0]) |
|
726 |
break; |
|
727 |
comment = ""; |
|
728 |
} |
|
729 |
return QString(); |
|
730 |
} |
|
731 |
||
732 |
/*! |
|
733 |
Empties this translator of all contents. |
|
734 |
||
735 |
This function works with stripped translator files. |
|
736 |
*/ |
|
737 |
||
738 |
void QTranslatorPrivate::clear() |
|
739 |
{ |
|
740 |
Q_Q(QTranslator); |
|
741 |
if (unmapPointer && unmapLength) { |
|
742 |
#if defined(QT_USE_MMAP) |
|
743 |
if (used_mmap) |
|
744 |
munmap(unmapPointer, unmapLength); |
|
745 |
else |
|
746 |
#endif |
|
747 |
delete [] unmapPointer; |
|
748 |
} |
|
749 |
||
750 |
unmapPointer = 0; |
|
751 |
unmapLength = 0; |
|
752 |
messageArray = 0; |
|
753 |
contextArray = 0; |
|
754 |
offsetArray = 0; |
|
755 |
numerusRulesArray = 0; |
|
756 |
messageLength = 0; |
|
757 |
contextLength = 0; |
|
758 |
offsetLength = 0; |
|
759 |
numerusRulesLength = 0; |
|
760 |
||
761 |
if (QCoreApplicationPrivate::isTranslatorInstalled(q)) |
|
762 |
QCoreApplication::postEvent(QCoreApplication::instance(), |
|
763 |
new QEvent(QEvent::LanguageChange)); |
|
764 |
} |
|
765 |
||
766 |
/*! |
|
767 |
Returns the translation for the key (\a context, \a sourceText, |
|
768 |
\a disambiguation). If none is found, also tries (\a context, \a |
|
769 |
sourceText, ""). If that still fails, returns an empty string. |
|
770 |
||
771 |
If you need to programatically insert translations in to a |
|
772 |
QTranslator, this function can be reimplemented. |
|
773 |
||
774 |
\sa load() |
|
775 |
*/ |
|
776 |
QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation) const |
|
777 |
{ |
|
778 |
Q_D(const QTranslator); |
|
779 |
return d->do_translate(context, sourceText, disambiguation, -1); |
|
780 |
} |
|
781 |
||
782 |
||
783 |
/*! |
|
784 |
\overload translate() |
|
785 |
||
786 |
Returns the translation for the key (\a context, \a sourceText, |
|
787 |
\a disambiguation). If none is found, also tries (\a context, \a |
|
788 |
sourceText, ""). If that still fails, returns an empty string. |
|
789 |
||
790 |
If \a n is not -1, it is used to choose an appropriate form for |
|
791 |
the translation (e.g. "%n file found" vs. "%n files found"). |
|
792 |
||
793 |
\sa load() |
|
794 |
*/ |
|
795 |
QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation, |
|
796 |
int n) const |
|
797 |
{ |
|
798 |
Q_D(const QTranslator); |
|
799 |
// this step is necessary because the 3-parameter translate() overload is virtual |
|
800 |
if (n == -1) |
|
801 |
return translate(context, sourceText, disambiguation); |
|
802 |
return d->do_translate(context, sourceText, disambiguation, n); |
|
803 |
} |
|
804 |
||
805 |
/*! |
|
806 |
Returns true if this translator is empty, otherwise returns false. |
|
807 |
This function works with stripped and unstripped translation files. |
|
808 |
*/ |
|
809 |
bool QTranslator::isEmpty() const |
|
810 |
{ |
|
811 |
Q_D(const QTranslator); |
|
812 |
return !d->unmapPointer && !d->unmapLength && !d->messageArray && |
|
813 |
!d->offsetArray && !d->contextArray; |
|
814 |
} |
|
815 |
||
816 |
/*! |
|
817 |
\fn QString QTranslator::find(const char *context, const char *sourceText, const char * comment = 0) const |
|
818 |
||
819 |
Use translate(\a context, \a sourceText, \a comment) instead. |
|
820 |
*/ |
|
821 |
||
822 |
QT_END_NAMESPACE |
|
823 |
||
824 |
#endif // QT_NO_TRANSLATION |