Translator.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <algorithm>
00027 # include <QTranslator>
00028 # include <QStringList>
00029 # include <QDir>
00030 # include <QApplication>
00031 #endif
00032
00033 #include "Translator.h"
00034
00035
00036
00037 using namespace Gui;
00038
00099
00100
00101 Translator* Translator::_pcSingleton = 0;
00102
00103 namespace Gui {
00104 class TranslatorP
00105 {
00106 public:
00107 std::string activatedLanguage;
00108 std::map<std::string, std::string> mapLanguageTopLevelDomain;
00109 std::list<QTranslator*> translators;
00110 QStringList paths;
00111 };
00112 }
00113
00114 Translator* Translator::instance(void)
00115 {
00116 if (!_pcSingleton)
00117 _pcSingleton = new Translator;
00118 return _pcSingleton;
00119 }
00120
00121 void Translator::destruct (void)
00122 {
00123 if (_pcSingleton)
00124 delete _pcSingleton;
00125 _pcSingleton=0;
00126 }
00127
00128 Translator::Translator()
00129 {
00130
00131 d = new TranslatorP;
00132 d->mapLanguageTopLevelDomain[QT_TR_NOOP("English" )] = "en";
00133 d->mapLanguageTopLevelDomain[QT_TR_NOOP("German" )] = "de";
00134 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Spanish" )] = "es";
00135 d->mapLanguageTopLevelDomain[QT_TR_NOOP("French" )] = "fr";
00136 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Italian" )] = "it";
00137 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Japanese" )] = "jp";
00138 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Chinese" )] = "zh";
00139 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Korean" )] = "kr";
00140 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Russian" )] = "ru";
00141 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Swedish" )] = "se";
00142 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Afrikaans" )] = "af";
00143 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Norwegian" )] = "no";
00144 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Portuguese")] = "pt";
00145 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Dutch" )] = "nl";
00146 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Ukrainian" )] = "uk";
00147 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Finnish" )] = "fi";
00148 d->mapLanguageTopLevelDomain[QT_TR_NOOP("Croatian" )] = "hr";
00149 d->activatedLanguage = "English";
00150
00151 d->paths = directories();
00152 }
00153
00154 Translator::~Translator()
00155 {
00156 removeTranslators();
00157 delete d;
00158 }
00159
00160 TStringList Translator::supportedLanguages() const
00161 {
00162
00163 TStringList languages;
00164 QDir dir(QLatin1String(":/translations"));
00165 for (std::map<std::string,std::string>::const_iterator it = d->mapLanguageTopLevelDomain.begin();
00166 it != d->mapLanguageTopLevelDomain.end(); ++it) {
00167 QString filter = QString::fromAscii("*_%1.qm").arg(QLatin1String(it->second.c_str()));
00168 QStringList fileNames = dir.entryList(QStringList(filter), QDir::Files, QDir::Name);
00169 if (!fileNames.isEmpty())
00170 languages.push_back(it->first);
00171 }
00172
00173 return languages;
00174 }
00175
00176 void Translator::activateLanguage (const char* lang)
00177 {
00178 removeTranslators();
00179 d->activatedLanguage = lang;
00180 TStringList languages = supportedLanguages();
00181 if (std::find(languages.begin(), languages.end(), lang) != languages.end()) {
00182 refresh();
00183 }
00184 }
00185
00186 std::string Translator::activeLanguage() const
00187 {
00188 return d->activatedLanguage;
00189 }
00190
00191 QStringList Translator::directories() const
00192 {
00193
00194 QStringList list;
00195
00196
00197
00198
00199
00200 list.push_back(QLatin1String(":/translations"));
00201 return list;
00202 }
00203
00204 void Translator::addPath(const QString& path)
00205 {
00206 d->paths.push_back(path);
00207 }
00208
00209 void Translator::installQMFiles(const QDir& dir, const char* locale)
00210 {
00211 QString filter = QString::fromAscii("*_%1.qm").arg(QLatin1String(locale));
00212 QStringList fileNames = dir.entryList(QStringList(filter), QDir::Files, QDir::Name);
00213 for (QStringList::Iterator it = fileNames.begin(); it != fileNames.end(); ++it){
00214 bool ok=false;
00215 for (std::list<QTranslator*>::const_iterator tt = d->translators.begin();
00216 tt != d->translators.end(); ++tt) {
00217 if ((*tt)->objectName() == *it) {
00218 ok = true;
00219 break;
00220 }
00221 }
00222
00223
00224 if (!ok) {
00225 QTranslator* translator = new QTranslator;
00226 translator->setObjectName(*it);
00227 if (translator->load(dir.filePath(*it))) {
00228 qApp->installTranslator(translator);
00229 d->translators.push_back(translator);
00230 }
00231 else {
00232 delete translator;
00233 }
00234 }
00235 }
00236 }
00237
00243 void Translator::refresh()
00244 {
00245 std::map<std::string, std::string>::iterator tld = d->mapLanguageTopLevelDomain.find(d->activatedLanguage);
00246 if (tld == d->mapLanguageTopLevelDomain.end())
00247 return;
00248 for (QStringList::iterator it = d->paths.begin(); it != d->paths.end(); ++it) {
00249 QDir dir(*it);
00250 installQMFiles(dir, tld->second.c_str());
00251 }
00252 }
00253
00257 void Translator::removeTranslators()
00258 {
00259 for (std::list<QTranslator*>::iterator it = d->translators.begin(); it != d->translators.end(); ++it) {
00260 qApp->removeTranslator(*it);
00261 delete *it;
00262 }
00263
00264 d->translators.clear();
00265 }
00266
00267 #include "moc_Translator.cpp"