From b4d801bf4d6e664ad729e3e95b212b83cc0c784f Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Fri, 17 Jul 2009 16:16:31 +0200 Subject: 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. --- tools/linguist/lconvert/main.cpp | 5 ++++ tools/linguist/linguist/messagemodel.cpp | 2 +- tools/linguist/lrelease/main.cpp | 1 + tools/linguist/lupdate/main.cpp | 5 ++++ tools/linguist/shared/po.cpp | 4 +-- tools/linguist/shared/translator.cpp | 42 ++++++++++++++++++++------------ tools/linguist/shared/translator.h | 4 +-- tools/linguist/shared/ts.cpp | 4 +-- tools/linguist/shared/xliff.cpp | 14 +++++------ 9 files changed, 51 insertions(+), 30 deletions(-) diff --git a/tools/linguist/lconvert/main.cpp b/tools/linguist/lconvert/main.cpp index ddde578..553ce6e 100644 --- a/tools/linguist/lconvert/main.cpp +++ b/tools/linguist/lconvert/main.cpp @@ -238,6 +238,11 @@ int main(int argc, char *argv[]) if (dropTranslations) tr.dropTranslations(); + tr.normalizeTranslations(cd); + if (!cd.errors().isEmpty()) { + qWarning("%s", qPrintable(cd.error())); + cd.clearErrors(); + } if (!tr.save(outFileName, cd, outFormat)) { qWarning("%s", qPrintable(cd.error())); return 3; diff --git a/tools/linguist/linguist/messagemodel.cpp b/tools/linguist/linguist/messagemodel.cpp index 6bbf6f3..9995220 100644 --- a/tools/linguist/linguist/messagemodel.cpp +++ b/tools/linguist/linguist/messagemodel.cpp @@ -139,7 +139,7 @@ DataModel::DataModel(QObject *parent) QStringList DataModel::normalizedTranslations(const MessageItem &m) const { - return Translator::normalizedTranslations(m.message(), m_language, m_country); + return Translator::normalizedTranslations(m.message(), m_numerusForms.count()); } ContextItem *DataModel::contextItem(int context) const diff --git a/tools/linguist/lrelease/main.cpp b/tools/linguist/lrelease/main.cpp index 845dcb8..843414d 100644 --- a/tools/linguist/lrelease/main.cpp +++ b/tools/linguist/lrelease/main.cpp @@ -120,6 +120,7 @@ static bool releaseTranslator(Translator &tor, const QString &qmFileName, } ConversionData cd; + tor.normalizeTranslations(cd); cd.m_verbose = verbose; cd.m_ignoreUnfinished = ignoreUnfinished; cd.m_saveMode = mode; diff --git a/tools/linguist/lupdate/main.cpp b/tools/linguist/lupdate/main.cpp index 78e5b5f..cedc01e 100644 --- a/tools/linguist/lupdate/main.cpp +++ b/tools/linguist/lupdate/main.cpp @@ -199,6 +199,11 @@ static void updateTsFiles(const Translator &fetchedTor, const QStringList &tsFil out.stripObsoleteMessages(); out.stripEmptyContexts(); + out.normalizeTranslations(cd); + if (!cd.errors().isEmpty()) { + printOut(cd.error()); + cd.clearErrors(); + } if (!out.save(fileName, cd, QLatin1String("auto"))) { printOut(cd.error()); *fail = true; 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 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 messages() const; QList 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 "; 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::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 << "\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; -- cgit v0.12