From a4e14afdbb898371c121b01a88014fc79a7a281b Mon Sep 17 00:00:00 2001 From: David Sansome Date: Thu, 8 Apr 2010 14:29:08 +0000 Subject: [PATCH] Load translations from an empty context too. --- src/main.cpp | 3 ++- src/potranslator.h | 35 +++++++++++++++++++++++++++++++++++ tests/translations_test.cpp | 7 ++++--- 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/potranslator.h diff --git a/src/main.cpp b/src/main.cpp index 467417563..e69adf61c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,7 @@ #include "player.h" #include "song.h" #include "equalizer.h" +#include "potranslator.h" #include #include @@ -57,7 +58,7 @@ void LoadTranslation(const QString& prefix, const QString& path) { return; #endif - QTranslator* t = new QTranslator; + QTranslator* t = new PoTranslator; if (t->load(prefix + "_" + QLocale::system().name(), path)) QCoreApplication::installTranslator(t); else diff --git a/src/potranslator.h b/src/potranslator.h new file mode 100644 index 000000000..5e753c5e4 --- /dev/null +++ b/src/potranslator.h @@ -0,0 +1,35 @@ +/* This file is part of Clementine. + + Clementine is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Clementine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Clementine. If not, see . +*/ + +#ifndef POTRANSLATOR_H +#define POTRANSLATOR_H + +#include + +// We convert from .po files to .qm files, which loses context information. +// This translator tries loading strings with an empty context if it can't +// find any others. + +class PoTranslator : public QTranslator { + public: + QString translate(const char* context, const char* source_text, const char* disambiguation = 0) const { + QString ret = QTranslator::translate(context, source_text, disambiguation); + if (!ret.isEmpty()) return ret; + return QTranslator::translate(NULL, source_text, disambiguation); + } +}; + +#endif // POTRANSLATOR_H diff --git a/tests/translations_test.cpp b/tests/translations_test.cpp index 33875431e..4c577b70f 100644 --- a/tests/translations_test.cpp +++ b/tests/translations_test.cpp @@ -17,18 +17,19 @@ #include #include -#include +#include "potranslator.h" #include "test_utils.h" TEST(Translations, Basic) { ASSERT_TRUE(QFile::exists(":/translations")); - ASSERT_TRUE(QFile::exists(":/translations/clementine_empty.qm")); ASSERT_TRUE(QFile::exists(":/translations/clementine_es.qm")); - QTranslator t; + PoTranslator t; t.load("clementine_es.qm", ":/translations"); EXPECT_EQ(QString::fromUtf8("Colección"), t.translate("MainWindow", "Library")); + EXPECT_EQ(QString::fromUtf8("Colección"), + t.translate("", "Library")); }