Load translations from an empty context too.

This commit is contained in:
David Sansome 2010-04-08 14:29:08 +00:00
parent 64bf9220cb
commit a4e14afdbb
3 changed files with 41 additions and 4 deletions

View File

@ -26,6 +26,7 @@
#include "player.h"
#include "song.h"
#include "equalizer.h"
#include "potranslator.h"
#include <QtSingleApplication>
#include <QtDebug>
@ -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

35
src/potranslator.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef POTRANSLATOR_H
#define POTRANSLATOR_H
#include <QTranslator>
// 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

View File

@ -17,18 +17,19 @@
#include <gtest/gtest.h>
#include <QFile>
#include <QTranslator>
#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"));
}