diff options
author | Lucie Gerard <lucie.gerard@qt.io> | 2019-04-01 14:23:14 +0200 |
---|---|---|
committer | Lucie Gerard <lucie.gerard@qt.io> | 2019-04-02 07:35:15 +0000 |
commit | 9364c3fcc8c98f70aceaac5b72823e290504cb2a (patch) | |
tree | c30e55902353e45045817b9bc1b2ab56e1b0e7ad | |
parent | d9aebed5c8520911f290da0d66b6cb849ccb9657 (diff) | |
download | qttools-9364c3fcc8c98f70aceaac5b72823e290504cb2a.tar.gz |
lupdate: Do not touch a translated file without target language
[ChangeLog][lupdate] lupdate will now generate an error if it is asked
to update a .ts file with translations, but without a target language.
This is to ensure that plural translations are not destroyed.
Fixes: QTBUG-53775
Change-Id: I646c1f2ec42a32dbc1a4676ad784887613607b78
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
21 files changed, 114 insertions, 14 deletions
diff --git a/src/linguist/lupdate/main.cpp b/src/linguist/lupdate/main.cpp index 134eab336..ba1126aa7 100644 --- a/src/linguist/lupdate/main.cpp +++ b/src/linguist/lupdate/main.cpp @@ -310,6 +310,19 @@ static void updateTsFiles(const Translator &fetchedTor, const QStringList &tsFil printErr(LU::tr("lupdate warning: Specified source language '%1' disagrees with" " existing file's language '%2'. Ignoring.\n") .arg(sourceLanguage, tor.sourceLanguageCode())); + // If there is translation in the file, the language should be recognized + // (when the language is not recognized, plural translations are lost) + if (tor.translationsExist()) { + QLocale::Language l; + QLocale::Country c; + tor.languageAndCountry(tor.languageCode(), &l, &c); + QStringList forms; + if (!getNumerusInfo(l, c, 0, &forms, 0)) { + printErr(LU::tr("File %1 won't be updated: it contains translation but the" + " target language is not recognized\n").arg(fileName)); + continue; + } + } } else { if (!targetLanguage.isEmpty()) tor.setLanguageCode(targetLanguage); diff --git a/src/linguist/shared/translator.cpp b/src/linguist/shared/translator.cpp index af669e235..db28ea396 100644 --- a/src/linguist/shared/translator.cpp +++ b/src/linguist/shared/translator.cpp @@ -433,6 +433,17 @@ void Translator::stripUntranslatedMessages() m_indexOk = false; } +bool Translator::translationsExist() +{ + for (TMM::Iterator it = m_messages.begin(); it != m_messages.end(); ) { + if (it->isTranslated()) + return true; + else + ++it; + } + return false; +} + void Translator::stripEmptyContexts() { for (TMM::Iterator it = m_messages.begin(); it != m_messages.end();) diff --git a/src/linguist/shared/translator.h b/src/linguist/shared/translator.h index 8919a13df..5b5d12250 100644 --- a/src/linguist/shared/translator.h +++ b/src/linguist/shared/translator.h @@ -135,6 +135,7 @@ public: void dropTranslations(); void dropUiLines(); void makeFileNamesAbsolute(const QDir &originalPath); + bool translationsExist(); struct Duplicates { QSet<int> byId, byContents; }; Duplicates resolveDuplicates(); diff --git a/tests/auto/linguist/lupdate/testdata/good/heuristics/project.ts.before b/tests/auto/linguist/lupdate/testdata/good/heuristics/project.ts.before index ce8281033..e72debed9 100644 --- a/tests/auto/linguist/lupdate/testdata/good/heuristics/project.ts.before +++ b/tests/auto/linguist/lupdate/testdata/good/heuristics/project.ts.before @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.0"> +<TS version="2.0" language="de_DE"> <context> <name>A</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/heuristics/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/heuristics/project.ts.result index 155967a52..351bdff7d 100644 --- a/tests/auto/linguist/lupdate/testdata/good/heuristics/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/heuristics/project.ts.result @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.1"> +<TS version="2.1" language="de_DE"> <context> <name>A</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/merge_whitespace/project.ts.before b/tests/auto/linguist/lupdate/testdata/good/merge_whitespace/project.ts.before index 0f84fedb8..ab91cac9d 100644 --- a/tests/auto/linguist/lupdate/testdata/good/merge_whitespace/project.ts.before +++ b/tests/auto/linguist/lupdate/testdata/good/merge_whitespace/project.ts.before @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> -<!DOCTYPE TS><TS version="1.1"> +<!DOCTYPE TS><TS version="1.1" language="en_EN"> <context> <name>QObject</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/merge_whitespace/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/merge_whitespace/project.ts.result index 64ec91076..93bd54bb9 100644 --- a/tests/auto/linguist/lupdate/testdata/good/merge_whitespace/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/merge_whitespace/project.ts.result @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.1"> +<TS version="2.1" language="en_EN"> <context> <name>QObject</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/mergecpp_noobsolete/project.ts.before b/tests/auto/linguist/lupdate/testdata/good/mergecpp_noobsolete/project.ts.before index feab1694d..f9915e9ba 100644 --- a/tests/auto/linguist/lupdate/testdata/good/mergecpp_noobsolete/project.ts.before +++ b/tests/auto/linguist/lupdate/testdata/good/mergecpp_noobsolete/project.ts.before @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> -<!DOCTYPE TS><TS version="1.1"> +<!DOCTYPE TS><TS version="1.1" language="en_EN"> <context> <name></name> <message id="keep_id"> diff --git a/tests/auto/linguist/lupdate/testdata/good/mergecpp_noobsolete/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/mergecpp_noobsolete/project.ts.result index a0cdda96a..516072d5e 100644 --- a/tests/auto/linguist/lupdate/testdata/good/mergecpp_noobsolete/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/mergecpp_noobsolete/project.ts.result @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.1"> +<TS version="2.1" language="en_EN"> <context> <name></name> <message id="keep_id"> diff --git a/tests/auto/linguist/lupdate/testdata/good/mergecpp_obsolete/project.ts.before b/tests/auto/linguist/lupdate/testdata/good/mergecpp_obsolete/project.ts.before index 2af303be4..99443bbad 100644 --- a/tests/auto/linguist/lupdate/testdata/good/mergecpp_obsolete/project.ts.before +++ b/tests/auto/linguist/lupdate/testdata/good/mergecpp_obsolete/project.ts.before @@ -1,5 +1,5 @@ <?xml version="1.0"?> -<!DOCTYPE TS><TS version="1.1"> +<!DOCTYPE TS><TS version="1.1" language="en_EN"> <context> <name></name> <message id="keep_id"> diff --git a/tests/auto/linguist/lupdate/testdata/good/mergecpp_obsolete/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/mergecpp_obsolete/project.ts.result index 1fcfea4e5..b6d47c8dd 100644 --- a/tests/auto/linguist/lupdate/testdata/good/mergecpp_obsolete/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/mergecpp_obsolete/project.ts.result @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.1"> +<TS version="2.1" language="en_EN"> <context> <name></name> <message id="keep_id"> diff --git a/tests/auto/linguist/lupdate/testdata/good/mergeui/project.ts.before b/tests/auto/linguist/lupdate/testdata/good/mergeui/project.ts.before index b96cd4ee9..b9333c9dd 100644 --- a/tests/auto/linguist/lupdate/testdata/good/mergeui/project.ts.before +++ b/tests/auto/linguist/lupdate/testdata/good/mergeui/project.ts.before @@ -1,5 +1,5 @@ <?xml version="1.0"?> -<!DOCTYPE TS><TS version="1.1"> +<!DOCTYPE TS><TS version="1.1" language="no_NO"> <context> <name>FindDialog</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/mergeui/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/mergeui/project.ts.result index 00c70ed94..8d51b197d 100644 --- a/tests/auto/linguist/lupdate/testdata/good/mergeui/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/mergeui/project.ts.result @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.1"> +<TS version="2.1" language="no_NO"> <context> <name>FindDialog</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/mergeui_obsolete/project.ts.before b/tests/auto/linguist/lupdate/testdata/good/mergeui_obsolete/project.ts.before index f06c22ca8..d47409c84 100644 --- a/tests/auto/linguist/lupdate/testdata/good/mergeui_obsolete/project.ts.before +++ b/tests/auto/linguist/lupdate/testdata/good/mergeui_obsolete/project.ts.before @@ -1,5 +1,5 @@ <?xml version="1.0"?> -<!DOCTYPE TS><TS version="1.1"> +<!DOCTYPE TS><TS version="1.1" language="no_NO"> <context> <name>FindDialog</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/mergeui_obsolete/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/mergeui_obsolete/project.ts.result index 2636ae075..79a6e8c51 100644 --- a/tests/auto/linguist/lupdate/testdata/good/mergeui_obsolete/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/mergeui_obsolete/project.ts.result @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.1"> +<TS version="2.1" language="no_NO"> <context> <name>FindDialog</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/main.cpp b/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/main.cpp new file mode 100644 index 000000000..157979b70 --- /dev/null +++ b/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/main.cpp @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QApplication> +#include <QPushButton> +#include <QTranslator> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QTranslator translator; + app.installTranslator(&translator); + + QObject::tr("un mot","toto",1); + + return app.exec(); +} diff --git a/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.pro b/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.pro new file mode 100644 index 000000000..759bea068 --- /dev/null +++ b/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.pro @@ -0,0 +1,3 @@ +SOURCES = main.cpp + +TRANSLATIONS = project.ts diff --git a/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.ts.before b/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.ts.before new file mode 100644 index 000000000..7216e069c --- /dev/null +++ b/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.ts.before @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE TS> +<TS version="2.1"> +<context> + <name>QObject</name> + <message numerus="yes"> + <location filename="main.cpp" line="39"/> + <source>un mot</source> + <comment>toto</comment> + <translation type="unfinished"> + <numerusform>ein Wort</numerusform> + </translation> + </message> +</context> +</TS> diff --git a/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.ts.result new file mode 100644 index 000000000..7216e069c --- /dev/null +++ b/tests/auto/linguist/lupdate/testdata/good/notargetlanguage/project.ts.result @@ -0,0 +1,15 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE TS> +<TS version="2.1"> +<context> + <name>QObject</name> + <message numerus="yes"> + <location filename="main.cpp" line="39"/> + <source>un mot</source> + <comment>toto</comment> + <translation type="unfinished"> + <numerusform>ein Wort</numerusform> + </translation> + </message> +</context> +</TS> diff --git a/tests/auto/linguist/lupdate/testdata/good/textsimilarity/project.ts.before b/tests/auto/linguist/lupdate/testdata/good/textsimilarity/project.ts.before index f06c22ca8..d47409c84 100644 --- a/tests/auto/linguist/lupdate/testdata/good/textsimilarity/project.ts.before +++ b/tests/auto/linguist/lupdate/testdata/good/textsimilarity/project.ts.before @@ -1,5 +1,5 @@ <?xml version="1.0"?> -<!DOCTYPE TS><TS version="1.1"> +<!DOCTYPE TS><TS version="1.1" language="no_NO"> <context> <name>FindDialog</name> <message> diff --git a/tests/auto/linguist/lupdate/testdata/good/textsimilarity/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/textsimilarity/project.ts.result index 2636ae075..79a6e8c51 100644 --- a/tests/auto/linguist/lupdate/testdata/good/textsimilarity/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/textsimilarity/project.ts.result @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> -<TS version="2.1"> +<TS version="2.1" language="no_NO"> <context> <name>FindDialog</name> <message> |