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 keys.reserve(d.m_translators.size());
107 for (TransMap::Iterator it = d.m_translators.begin();
108 it != d.m_translators.end();
109 ++it)
110 keys.append(it.key());
111
112 for (const auto& key : std::as_const(keys))
113 load_real(key);
114 }
115}
116
117QMap<QString, QString> MythTranslation::getLanguages(void)
118{
119 QMap<QString, QString> langs;
120
121 QDir translationDir(GetTranslationsDir());
122 translationDir.setNameFilters(QStringList("mythfrontend_*.qm"));
123 translationDir.setFilter(QDir::Files);
124 QFileInfoList translationFiles = translationDir.entryInfoList();
125 QFileInfoList::const_iterator it;
126 for (it = translationFiles.constBegin(); it != translationFiles.constEnd();
127 ++it)
128 {
129 // We write the names incorrectly as all lowercase, so fix this before
130 // sending to QLocale
131 QString languageCode = (*it).baseName().section('_', 1, 1);
132 QString countryCode = (*it).baseName().section('_', 2, 2);
133 if (!countryCode.isEmpty())
134 languageCode = QString("%1_%2")
135 .arg(languageCode, countryCode.toUpper());
136
137 MythLocale locale(languageCode);
138 QString language = locale.GetNativeLanguage();
139 if (language.isEmpty())
140 language = locale.GetLanguage(); // Fall back to English
141
142 if (!countryCode.isEmpty())
143 {
144 QString country = locale.GetNativeCountry();
145 if (country.isEmpty())
146 country = locale.GetCountry(); // Fall back to English
147
148 language.append(QString(" (%1)").arg(country));
149 }
150
151 langs[languageCode] = language;
152 }
153
154 return langs;
155}
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:285
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
QMap< QString, QTranslator * > TransMap