2
* translationmanager.cpp
3
* Copyright (C) 2006 Remko Troncon, Justin Karneges
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include <QTranslator>
22
#include <QCoreApplication>
26
#include "translationmanager.h"
27
#include "applicationinfo.h"
30
TranslationManager::TranslationManager()
33
currentLanguage_ = "en";
34
QString currentLanguageName = QT_TR_NOOP("language_name");
36
// The application translator
37
translator_ = new QTranslator(0);
40
qt_translator_ = new QTranslator(0);
43
connect(QCoreApplication::instance(),SIGNAL(aboutToQuit()),SLOT(deleteLater()));
46
TranslationManager::~TranslationManager()
48
QCoreApplication::instance()->removeTranslator(translator_);
52
QCoreApplication::instance()->removeTranslator(qt_translator_);
53
delete qt_translator_;
57
TranslationManager* TranslationManager::instance()
60
instance_ = new TranslationManager();
65
const QString& TranslationManager::currentLanguage() const
67
return currentLanguage_;
70
QString TranslationManager::currentXMLLanguage() const
72
QString xmllang = currentLanguage_;
73
xmllang.replace('_',"-");
74
int at_index = xmllang.find('@');
76
xmllang = xmllang.left(at_index);
80
void TranslationManager::loadTranslation(const QString& language)
82
// The default translation
83
if(language == "en") {
84
currentLanguage_ = language;
85
//currentLanguageName_ = "English";
86
QCoreApplication::instance()->removeTranslator(translator_);
87
QCoreApplication::instance()->removeTranslator(qt_translator_);
91
// Try loading the translation file
92
QStringList dirs = translationDirs();
93
foreach(QString dir, dirs) {
94
if(!QFile::exists(dir))
96
if (translator_->load("psi_" + language, dir)) {
97
// try to load qt library translation
98
qt_translator_->load("qt_" + language, dir);
99
if (currentLanguage_ == "en") {
100
QCoreApplication::instance()->installTranslator(translator_);
101
QCoreApplication::instance()->installTranslator(qt_translator_);
103
currentLanguage_ = language;
109
VarList TranslationManager::availableTranslations()
113
// We always support english
114
langs.set("en", "English");
117
QStringList dirs = TranslationManager::translationDirs();
118
for(QStringList::Iterator it = dirs.begin(); it != dirs.end(); ++it) {
119
if(!QFile::exists(*it))
122
QStringList entries = d.entryList();
123
for(QStringList::Iterator it2 = entries.begin(); it2 != entries.end(); ++it2) {
124
if(*it2 == "." || *it2 == "..")
128
// verify that it is a language file
129
if(str.left(4) != "psi_")
131
int n = str.find('.', 4);
134
if(str.mid(n) != ".qm")
136
QString lang = str.mid(4, n-4);
138
//printf("found [%s], lang=[%s]\n", str.latin1(), lang.latin1());
140
// get the language_name
141
QString name = QString("[") + str + "]";
143
if(!t.load(str, *it))
146
//Is translate equivalent to the old findMessage? I hope so
148
QString s = t.translate("@default", "language_name");
152
langs.set(lang, name);
159
QStringList TranslationManager::translationDirs() const
163
dirs += "." + subdir;
164
dirs += ApplicationInfo::homeDir() + subdir;
165
dirs += ApplicationInfo::resourcesDir() + subdir;
170
TranslationManager* TranslationManager::instance_ = NULL;