diff options
author | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2009-07-17 14:16:31 (GMT) |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2009-07-17 14:35:21 (GMT) |
commit | b4d801bf4d6e664ad729e3e95b212b83cc0c784f (patch) | |
tree | 086991093228f4296c576d258c3418ae86e23d5b /tools/linguist/shared | |
parent | 1d52548c21f9ca47e2ef02cd944b7640b0d4dd6b (diff) | |
download | Qt-b4d801bf4d6e664ad729e3e95b212b83cc0c784f.zip Qt-b4d801bf4d6e664ad729e3e95b212b83cc0c784f.tar.gz Qt-b4d801bf4d6e664ad729e3e95b212b83cc0c784f.tar.bz2 |
reorganize numerus form count normalization
replace implicit normalization of individual messages on file writeout
with global normalization which is called by the command line tools.
this should a) be faster and b) cover the most critical case: lrelease.
Diffstat (limited to 'tools/linguist/shared')
-rw-r--r-- | tools/linguist/shared/po.cpp | 4 | ||||
-rw-r--r-- | tools/linguist/shared/translator.cpp | 42 | ||||
-rw-r--r-- | tools/linguist/shared/translator.h | 4 | ||||
-rw-r--r-- | tools/linguist/shared/ts.cpp | 4 | ||||
-rw-r--r-- | tools/linguist/shared/xliff.cpp | 14 |
5 files changed, 39 insertions, 29 deletions
diff --git a/tools/linguist/shared/po.cpp b/tools/linguist/shared/po.cpp index cb943be..a197b25 100644 --- a/tools/linguist/shared/po.cpp +++ b/tools/linguist/shared/po.cpp @@ -547,7 +547,7 @@ bool loadPO(Translator &translator, QIODevice &dev, ConversionData &cd) return !error && cd.errors().isEmpty(); } -bool savePO(const Translator &translator, QIODevice &dev, ConversionData &cd) +bool savePO(const Translator &translator, QIODevice &dev, ConversionData &) { bool ok = true; QTextStream out(&dev); @@ -633,7 +633,7 @@ bool savePO(const Translator &translator, QIODevice &dev, ConversionData &cd) if (plural.isEmpty()) plural = msg.sourceText(); out << poEscapedString(prefix, QLatin1String("msgid_plural"), noWrap, plural); - QStringList translations = translator.normalizedTranslations(msg, cd, &ok); + const QStringList &translations = msg.translations(); for (int i = 0; i != translations.size(); ++i) { out << poEscapedString(prefix, QString::fromLatin1("msgstr[%1]").arg(i), noWrap, translations.at(i)); diff --git a/tools/linguist/shared/translator.cpp b/tools/linguist/shared/translator.cpp index b8d559f..e5b8932 100644 --- a/tools/linguist/shared/translator.cpp +++ b/tools/linguist/shared/translator.cpp @@ -497,16 +497,10 @@ QList<TranslatorMessage> Translator::translatedMessages() const return result; } -QStringList Translator::normalizedTranslations(const TranslatorMessage &msg, - QLocale::Language language, QLocale::Country country) +QStringList Translator::normalizedTranslations(const TranslatorMessage &msg, int numPlurals) { QStringList translations = msg.translations(); - int numTranslations = 1; - if (msg.isPlural() && language != QLocale::C) { - QStringList forms; - if (getNumerusInfo(language, country, 0, &forms)) - numTranslations = forms.count(); // includes singular - } + int numTranslations = msg.isPlural() ? numPlurals : 1; // make sure that the stringlist always have the size of the // language's current numerus, or 1 if its not plural @@ -520,21 +514,39 @@ QStringList Translator::normalizedTranslations(const TranslatorMessage &msg, return translations; } -QStringList Translator::normalizedTranslations(const TranslatorMessage &msg, - ConversionData &cd, bool *ok) const +void Translator::normalizeTranslations(ConversionData &cd) { + bool truncated = false; QLocale::Language l; QLocale::Country c; languageAndCountry(languageCode(), &l, &c); - QStringList translns = normalizedTranslations(msg, l, c); - if (msg.translations().size() > translns.size() && ok) { + int numPlurals = 1; + if (l != QLocale::C) { + QStringList forms; + if (getNumerusInfo(l, c, 0, &forms)) + numPlurals = forms.count(); // includes singular + } + for (int i = 0; i < m_messages.count(); ++i) { + const TranslatorMessage &msg = m_messages.at(i); + QStringList tlns = msg.translations(); + int ccnt = msg.isPlural() ? numPlurals : 1; + if (tlns.count() != ccnt) { + while (tlns.count() < ccnt) + tlns.append(QString()); + while (tlns.count() > ccnt) { + tlns.removeLast(); + truncated = true; + } + TranslatorMessage msg2(msg); + msg2.setTranslations(tlns); + m_messages[i] = msg2; + } + } + if (truncated) cd.appendError(QLatin1String( "Removed plural forms as the target language has less " "forms.\nIf this sounds wrong, possibly the target language is " "not set or recognized.\n")); - *ok = false; - } - return translns; } QString Translator::guessLanguageCodeFromFileName(const QString &filename) diff --git a/tools/linguist/shared/translator.h b/tools/linguist/shared/translator.h index 77b515f..01778d7 100644 --- a/tools/linguist/shared/translator.h +++ b/tools/linguist/shared/translator.h @@ -150,8 +150,8 @@ public: static QString guessLanguageCodeFromFileName(const QString &fileName); QList<TranslatorMessage> messages() const; QList<TranslatorMessage> translatedMessages() const; - static QStringList normalizedTranslations(const TranslatorMessage &m, - QLocale::Language lang, QLocale::Country country); + static QStringList normalizedTranslations(const TranslatorMessage &m, int numPlurals); + void normalizeTranslations(ConversionData &cd); QStringList normalizedTranslations(const TranslatorMessage &m, ConversionData &cd, bool *ok) const; int messageCount() const { return m_messages.size(); } diff --git a/tools/linguist/shared/ts.cpp b/tools/linguist/shared/ts.cpp index 6c95dbd..a0ce727 100644 --- a/tools/linguist/shared/ts.cpp +++ b/tools/linguist/shared/ts.cpp @@ -693,8 +693,8 @@ bool saveTS(const Translator &translator, QIODevice &dev, ConversionData &cd, in t << " type=\"obsolete\""; if (msg.isPlural()) { t << ">"; - QStringList translns = translator.normalizedTranslations(msg, cd, &result); - for (int j = 0; j < qMax(1, translns.count()); ++j) { + const QStringList &translns = msg.translations(); + for (int j = 0; j < translns.count(); ++j) { t << "\n <numerusform"; writeVariants(t, " ", translns[j]); t << "</numerusform>"; diff --git a/tools/linguist/shared/xliff.cpp b/tools/linguist/shared/xliff.cpp index 61e4b9f..969d5d4 100644 --- a/tools/linguist/shared/xliff.cpp +++ b/tools/linguist/shared/xliff.cpp @@ -243,13 +243,12 @@ static void writeComment(QTextStream &ts, const TranslatorMessage &msg, const QR } } -static void writeTransUnits(QTextStream &ts, const TranslatorMessage &msg, const QRegExp &drops, int indent, - const Translator &translator, ConversionData &cd, bool *ok) +static void writeTransUnits(QTextStream &ts, const TranslatorMessage &msg, const QRegExp &drops, int indent) { static int msgid; QString msgidstr = !msg.id().isEmpty() ? msg.id() : QString::fromAscii("_msg%1").arg(++msgid); - QStringList translns = translator.normalizedTranslations(msg, cd, ok); + QStringList translns = msg.translations(); QHash<QString, QString>::const_iterator it; QString pluralStr; QStringList sources(msg.sourceText()); @@ -347,8 +346,7 @@ static void writeTransUnits(QTextStream &ts, const TranslatorMessage &msg, const } } -static void writeMessage(QTextStream &ts, const TranslatorMessage &msg, const QRegExp &drops, int indent, - const Translator &translator, ConversionData &cd, bool *ok) +static void writeMessage(QTextStream &ts, const TranslatorMessage &msg, const QRegExp &drops, int indent) { if (msg.isPlural()) { writeIndent(ts, indent); @@ -362,12 +360,12 @@ static void writeMessage(QTextStream &ts, const TranslatorMessage &msg, const QR writeLineNumber(ts, msg, indent); writeComment(ts, msg, drops, indent); - writeTransUnits(ts, msg, drops, indent, translator, cd, ok); + writeTransUnits(ts, msg, drops, indent); --indent; writeIndent(ts, indent); ts << "</group>\n"; } else { - writeTransUnits(ts, msg, drops, indent, translator, cd, ok); + writeTransUnits(ts, msg, drops, indent); } } @@ -790,7 +788,7 @@ bool saveXLIFF(const Translator &translator, QIODevice &dev, ConversionData &cd) } foreach (const TranslatorMessage &msg, messageOrder[fn][ctx]) - writeMessage(ts, msg, drops, indent, translator, cd, &ok); + writeMessage(ts, msg, drops, indent); if (!ctx.isEmpty()) { --indent; |