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).arg(module_name));
65  qApp->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)
72  .arg(module_name));
73  }
74 }
75 
76 void 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  qApp->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 // if (!d.m_loaded)
101 // return;
102 
103  // Update our translators if necessary.
104  // We need two loops, as the QMap wasn't happy with
105  // me changing its contents during my iteration.
106  if (LanguageChanged())
107  {
108  QStringList keys;
109  for (TransMap::Iterator it = d.m_translators.begin();
110  it != d.m_translators.end();
111  ++it)
112  keys.append(it.key());
113 
114  for (QStringList::Iterator it = keys.begin();
115  it != keys.end();
116  ++it)
117  load(*it);
118  }
119 }
120 
121 QMap<QString, QString> MythTranslation::getLanguages(void)
122 {
123  QMap<QString, QString> langs;
124 
125  QDir translationDir(GetTranslationsDir());
126  translationDir.setNameFilters(QStringList("mythfrontend_*.qm"));
127  translationDir.setFilter(QDir::Files);
128  QFileInfoList translationFiles = translationDir.entryInfoList();
129  QFileInfoList::const_iterator it;
130  for (it = translationFiles.constBegin(); it != translationFiles.constEnd();
131  ++it)
132  {
133  // We write the names incorrectly as all lowercase, so fix this before
134  // sending to QLocale
135  QString languageCode = (*it).baseName().section('_', 1, 1);
136  QString countryCode = (*it).baseName().section('_', 2, 2);
137  if (!countryCode.isEmpty())
138  languageCode = QString("%1_%2").arg(languageCode)
139  .arg(countryCode.toUpper());
140 
141  MythLocale locale(languageCode);
142  QString language = locale.GetNativeLanguage();
143  if (language.isEmpty())
144  language = locale.GetLanguage(); // Fall back to English
145 
146  if (!countryCode.isEmpty())
147  {
148  QString country = locale.GetNativeCountry();
149  if (country.isEmpty())
150  country = locale.GetCountry(); // Fall back to English
151 
152  language.append(QString(" (%1)").arg(country));
153  }
154 
155  langs[languageCode] = language;
156  }
157 
158  return langs;
159 }
QString GetLanguage() const
ISO639 2-letter.
Definition: mythlocale.cpp:82
static class MythTranslationPrivate d
QMap< QString, QTranslator * > TransMap
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythTranslationPrivate()=default
QString GetNativeLanguage() const
Name of language in English.
Definition: mythlocale.cpp:87
QString GetNativeCountry() const
Name of country in English.
Definition: mythlocale.cpp:70
static bool LanguageChanged(void)
void OverrideSettingForSession(const QString &key, const QString &value)
QString GetSetting(const QString &key, const QString &defaultval="")
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
static QMap< QString, QString > getLanguages(void)
static void reload()
Reload all active translators based on the current language setting.
QString GetTranslationsDir(void)
Definition: mythdirs.cpp:227
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QString GetCountry() const
ISO3166 2-letter.
Definition: mythlocale.cpp:65
static void unload(const QString &module_name)
Remove a QTranslator previously installed using load().