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