00001
00002 #include "mythtranslation.h"
00003
00004
00005 #include <QStringList>
00006 #include <QFileInfo>
00007 #include <QApplication>
00008 #include <QDir>
00009
00010
00011 #include "mythdirs.h"
00012 #include "mythcorecontext.h"
00013
00014 typedef QMap<QString, QTranslator*> TransMap;
00015
00016 class MythTranslationPrivate
00017 {
00018 public:
00019 MythTranslationPrivate():
00020 m_loaded(false) { };
00021
00022 void Init(void)
00023 {
00024 if (!m_loaded)
00025 {
00026 m_loaded = true;
00027 m_language = gCoreContext->GetSetting("Language");
00028 }
00029 };
00030
00031 bool m_loaded;
00032 QString m_language;
00033 TransMap m_translators;
00034 };
00035
00036 MythTranslationPrivate MythTranslation::d;
00037
00038 void MythTranslation::load(const QString &module_name)
00039 {
00040 d.Init();
00041
00042
00043 unload(module_name);
00044
00045
00046 QString lang = d.m_language.toLower();
00047
00048 if (d.m_language.isEmpty())
00049 {
00050 lang = "en_us";
00051 }
00052
00053 if (lang == "en")
00054 {
00055 gCoreContext->SaveSetting("Language", "en_US");
00056 lang = "en_us";
00057 }
00058
00059 QTranslator *trans = new QTranslator(0);
00060 if (trans->load(GetTranslationsDir() + module_name
00061 + "_" + lang + ".qm", "."))
00062 {
00063 LOG(VB_GENERAL, LOG_INFO,
00064 QString("Loading %1 translation for module %2")
00065 .arg(lang).arg(module_name));
00066 qApp->installTranslator(trans);
00067 d.m_translators[module_name] = trans;
00068 }
00069 else
00070 {
00071 LOG(VB_GENERAL, LOG_ERR, QString("Error Loading %1 translation for "
00072 "module %2").arg(lang)
00073 .arg(module_name));
00074 }
00075 }
00076
00077 void MythTranslation::unload(const QString &module_name)
00078 {
00079 TransMap::Iterator it = d.m_translators.find(module_name);
00080 if (it != d.m_translators.end())
00081 {
00082
00083 qApp->removeTranslator(*it);
00084 delete *it;
00085 d.m_translators.erase(it);
00086 }
00087 }
00088
00089 bool MythTranslation::LanguageChanged(void)
00090 {
00091 QString currentLanguage = gCoreContext->GetSetting("Language");
00092 bool ret = false;
00093 if (!currentLanguage.isEmpty() && currentLanguage.compare(d.m_language))
00094 ret = true;
00095 d.m_language = currentLanguage;
00096 return ret;
00097 }
00098
00099 void MythTranslation::reload()
00100 {
00101
00102
00103
00104
00105
00106
00107 if (LanguageChanged())
00108 {
00109 QStringList keys;
00110 for (TransMap::Iterator it = d.m_translators.begin();
00111 it != d.m_translators.end();
00112 ++it)
00113 keys.append(it.key());
00114
00115 for (QStringList::Iterator it = keys.begin();
00116 it != keys.end();
00117 ++it)
00118 load(*it);
00119 }
00120 }
00121
00122 QMap<QString, QString> MythTranslation::getLanguages(void)
00123 {
00124 QMap<QString, QString> langs;
00125
00126 QDir translationDir(GetTranslationsDir());
00127 translationDir.setNameFilters(QStringList("mythfrontend_*.qm"));
00128 translationDir.setFilter(QDir::Files);
00129 QFileInfoList translationFiles = translationDir.entryInfoList();
00130 QFileInfoList::const_iterator it;
00131 for (it = translationFiles.constBegin(); it != translationFiles.constEnd();
00132 ++it)
00133 {
00134
00135
00136 QString languageCode = (*it).baseName().section('_', 1, 1);
00137 QString countryCode = (*it).baseName().section('_', 2, 2);
00138 if (!countryCode.isEmpty())
00139 languageCode = QString("%1_%2").arg(languageCode)
00140 .arg(countryCode.toUpper());
00141
00142 MythLocale locale(languageCode);
00143 QString language = locale.GetNativeLanguage();
00144 if (language.isEmpty())
00145 language = locale.GetLanguage();
00146
00147 if (!countryCode.isEmpty())
00148 {
00149 QString country = locale.GetNativeCountry();
00150 if (country.isEmpty())
00151 country = locale.GetCountry();
00152
00153 language.append(QString(" (%1)").arg(country));
00154 }
00155
00156 langs[languageCode] = language;
00157 }
00158
00159 return langs;
00160 }
00161