MythTV
master
mythtv
libs
libmythbase
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
15
using
TransMap
= QMap<QString, QTranslator*>;
16
17
class
MythTranslationPrivate
18
{
19
public
:
20
MythTranslationPrivate
() =
default
;
21
22
void
Init
(
void
)
23
{
24
if
(!
m_loaded
)
25
{
26
m_loaded
=
true
;
27
m_language
=
gCoreContext
->
GetSetting
(
"Language"
);
28
}
29
};
30
31
bool
m_loaded
{
false
};
32
QString
m_language
;
33
TransMap
m_translators
;
34
};
35
36
MythTranslationPrivate
MythTranslation::d
;
37
38
void
MythTranslation::load
(
const
QString &module_name)
39
{
40
d
.
Init
();
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
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
QCoreApplication::removeTranslator(*it);
83
delete
*it;
84
d
.
m_translators
.erase(it);
85
}
86
}
87
88
bool
MythTranslation::LanguageChanged
(
void
)
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
98
void
MythTranslation::reload
()
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
(
const
auto
& key : std::as_const(keys))
115
load
(key);
116
}
117
}
118
119
QMap<QString, QString>
MythTranslation::getLanguages
(
void
)
120
{
121
QMap<QString, QString> langs;
122
123
QDir translationDir(
GetTranslationsDir
());
124
translationDir.setNameFilters(QStringList(
"mythfrontend_*.qm"
));
125
translationDir.setFilter(QDir::Files);
126
QFileInfoList translationFiles = translationDir.entryInfoList();
127
QFileInfoList::const_iterator it;
128
for
(it = translationFiles.constBegin(); it != translationFiles.constEnd();
129
++it)
130
{
131
// We write the names incorrectly as all lowercase, so fix this before
132
// sending to QLocale
133
QString languageCode = (*it).baseName().section(
'_'
, 1, 1);
134
QString countryCode = (*it).baseName().section(
'_'
, 2, 2);
135
if
(!countryCode.isEmpty())
136
languageCode = QString(
"%1_%2"
)
137
.arg(languageCode, countryCode.toUpper());
138
139
MythLocale
locale(languageCode);
140
QString language = locale.
GetNativeLanguage
();
141
if
(language.isEmpty())
142
language = locale.
GetLanguage
();
// Fall back to English
143
144
if
(!countryCode.isEmpty())
145
{
146
QString country = locale.
GetNativeCountry
();
147
if
(country.isEmpty())
148
country = locale.
GetCountry
();
// Fall back to English
149
150
language.append(QString(
" (%1)"
).arg(country));
151
}
152
153
langs[languageCode] = language;
154
}
155
156
return
langs;
157
}
MythTranslation::reload
static void reload()
Reload all active translators based on the current language setting.
Definition:
mythtranslation.cpp:98
MythLocale
Definition:
mythlocale.h:12
MythTranslationPrivate::Init
void Init(void)
Definition:
mythtranslation.cpp:22
MythLocale::GetLanguage
QString GetLanguage() const
ISO639 2-letter.
Definition:
mythlocale.cpp:83
MythCoreContext::OverrideSettingForSession
void OverrideSettingForSession(const QString &key, const QString &value)
Definition:
mythcorecontext.cpp:1347
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition:
mythlogging.h:39
MythLocale::GetNativeLanguage
QString GetNativeLanguage() const
Name of language in English.
Definition:
mythlocale.cpp:88
mythdirs.h
MythTranslation::LanguageChanged
static bool LanguageChanged(void)
Definition:
mythtranslation.cpp:88
MythLocale::GetNativeCountry
QString GetNativeCountry() const
Name of country in English.
Definition:
mythlocale.cpp:71
MythTranslation::getLanguages
static QMap< QString, QString > getLanguages(void)
Definition:
mythtranslation.cpp:119
mythlogging.h
MythTranslation::d
static class MythTranslationPrivate d
Definition:
mythtranslation.h:33
MythTranslationPrivate::m_loaded
bool m_loaded
Definition:
mythtranslation.cpp:31
mythtranslation.h
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition:
mythcorecontext.cpp:57
mythcorecontext.h
MythTranslation::unload
static void unload(const QString &module_name)
Remove a QTranslator previously installed using load().
Definition:
mythtranslation.cpp:76
MythTranslationPrivate::m_language
QString m_language
Definition:
mythtranslation.cpp:32
MythLocale::GetCountry
QString GetCountry() const
ISO3166 2-letter.
Definition:
mythlocale.cpp:66
MythTranslationPrivate::m_translators
TransMap m_translators
Definition:
mythtranslation.cpp:33
GetTranslationsDir
QString GetTranslationsDir(void)
Definition:
mythdirs.cpp:266
MythTranslation::load
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
Definition:
mythtranslation.cpp:38
MythTranslationPrivate
Definition:
mythtranslation.cpp:17
MythTranslationPrivate::MythTranslationPrivate
MythTranslationPrivate()=default
TransMap
QMap< QString, QTranslator * > TransMap
Definition:
mythtranslation.cpp:15
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition:
mythcorecontext.cpp:904
Generated on Wed Feb 26 2025 03:17:54 for MythTV by
1.8.17