MythTV master
mythtranslation.cpp
Go to the documentation of this file.
1
2#include "mythtranslation.h"
3
4// QT
5#include <QStringList>
6#include <QFileInfo>
7#include <QApplication>
8#include <QDir>
9
10// libmythbase
11#include "mythdirs.h"
12#include "mythcorecontext.h"
13#include "mythlogging.h"
14
15using TransMap = QMap<QString, QTranslator*>;
16
18{
19 public:
21
22 QString m_language;
24};
25
27
28void MythTranslation::load(const QString &module_name)
29{
30 // update m_language via LanguageChanged() and load the new translation
31 // if one was already loaded and the language has changed
32 reload();
33
34 if (!d.m_translators.contains(module_name))
35 {
36 load_real(module_name);
37 }
38}
39
40void MythTranslation::load_real(const QString &module_name)
41{
42 // unload any previous version
43 unload(module_name);
44
45 // install translator
46 QString lang = d.m_language.toLower();
47
48 if (d.m_language.isEmpty())
49 {
50 lang = "en_us";
51 }
52
53 if (lang == "en")
54 {
55 gCoreContext->OverrideSettingForSession("Language", "en_US");
56 lang = "en_us";
57 }
58
59 auto *trans = new QTranslator(nullptr);
60 if (trans->load(GetTranslationsDir() + module_name
61 + "_" + lang + ".qm", "."))
62 {
63 LOG(VB_GENERAL, LOG_INFO,
64 QString("Loading %1 translation for module %2")
65 .arg(lang, module_name));
66 QCoreApplication::installTranslator(trans);
67 d.m_translators[module_name] = trans;
68 }
69 else
70 {
71 LOG(VB_GENERAL, LOG_ERR, QString("Error Loading %1 translation for "
72 "module %2").arg(lang, module_name));
73 }
74}
75
76void MythTranslation::unload(const QString &module_name)
77{
78 TransMap::Iterator it = d.m_translators.find(module_name);
79 if (it != d.m_translators.end())
80 {
81 // found translator, remove it from qApp and our map
82 QCoreApplication::removeTranslator(*it);
83 delete *it;
84 d.m_translators.erase(it);
85 }
86}
87
89{
90 QString currentLanguage = gCoreContext->GetSetting("Language");
91 bool ret = false;
92 if (!currentLanguage.isEmpty() && currentLanguage.compare(d.m_language))
93 ret = true;
94 d.m_language = currentLanguage;
95 return ret;
96}
97
99{
100 // Update our translators if necessary.
101 // We need two loops, as the QMap wasn't happy with
102 // me changing its contents during my iteration.
103 if (LanguageChanged())
104 {
105 QStringList keys;
106 for (TransMap::Iterator it = d.m_translators.begin();
107 it != d.m_translators.end();
108 ++it)
109 keys.append(it.key());
110
111 for (const auto& key : std::as_const(keys))
112 load_real(key);
113 }
114}
115
116QMap<QString, QString> MythTranslation::getLanguages(void)
117{
118 QMap<QString, QString> langs;
119
120 QDir translationDir(GetTranslationsDir());
121 translationDir.setNameFilters(QStringList("mythfrontend_*.qm"));
122 translationDir.setFilter(QDir::Files);
123 QFileInfoList translationFiles = translationDir.entryInfoList();
124 QFileInfoList::const_iterator it;
125 for (it = translationFiles.constBegin(); it != translationFiles.constEnd();
126 ++it)
127 {
128 // We write the names incorrectly as all lowercase, so fix this before
129 // sending to QLocale
130 QString languageCode = (*it).baseName().section('_', 1, 1);
131 QString countryCode = (*it).baseName().section('_', 2, 2);
132 if (!countryCode.isEmpty())
133 languageCode = QString("%1_%2")
134 .arg(languageCode, countryCode.toUpper());
135
136 MythLocale locale(languageCode);
137 QString language = locale.GetNativeLanguage();
138 if (language.isEmpty())
139 language = locale.GetLanguage(); // Fall back to English
140
141 if (!countryCode.isEmpty())
142 {
143 QString country = locale.GetNativeCountry();
144 if (country.isEmpty())
145 country = locale.GetCountry(); // Fall back to English
146
147 language.append(QString(" (%1)").arg(country));
148 }
149
150 langs[languageCode] = language;
151 }
152
153 return langs;
154}
QString GetSetting(const QString &key, const QString &defaultval="")
void OverrideSettingForSession(const QString &key, const QString &value)
QString GetNativeLanguage() const
Name of language in English.
Definition: mythlocale.cpp:88
QString GetCountry() const
ISO3166 2-letter.
Definition: mythlocale.cpp:66
QString GetNativeCountry() const
Name of country in English.
Definition: mythlocale.cpp:71
QString GetLanguage() const
ISO639 2-letter.
Definition: mythlocale.cpp:83
MythTranslationPrivate()=default
static bool LanguageChanged(void)
static class MythTranslationPrivate d
static void load_real(const QString &module_name)
static void reload()
Reload all active translators based on the current language setting.
static void unload(const QString &module_name)
Remove a QTranslator previously installed using load().
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
static QMap< QString, QString > getLanguages(void)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString GetTranslationsDir(void)
Definition: mythdirs.cpp:266
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
QMap< QString, QTranslator * > TransMap