Skip to content

Commit 0ed3908

Browse files
Make translations load dynamicly
1 parent 3f5bed2 commit 0ed3908

File tree

15 files changed

+64
-119
lines changed

15 files changed

+64
-119
lines changed

src/Convert.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,5 @@ namespace Convert
2424
return retList;
2525
}
2626

27-
Languages LangEnumFromLocale(const QString& locale)
28-
{
29-
QString lowerLocale = locale.toLower();
30-
31-
for(auto elem = PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cbegin(); elem != PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cend(); ++elem)
32-
if (Windows::Locale::GetLanguageShortName(elem.key()) == lowerLocale)
33-
return elem.key();
34-
35-
return Languages::English;
36-
}
37-
38-
Languages ToLangEnum(const std::string& locale) { return LangEnumFromLocale(QString::fromStdString(locale)); }
39-
Languages ToLangEnum(const QString& locale) { return LangEnumFromLocale(locale); }
40-
Qt::Key ToQtKey(const QChar& ch) { return static_cast<Qt::Key>(QChar(ch).unicode()); }
41-
42-
QString ToQString(const Languages& locale)
43-
{
44-
for(auto elem = PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cbegin(); elem != PROGRAM_CONSTANTS->LANGUAGES_STRINGS.cend(); ++elem)
45-
if (elem.key() == locale)
46-
return elem.value().first;
47-
48-
return "en";
49-
}
27+
Qt::Key ToQtKey(const QChar& ch) { return static_cast<Qt::Key>(QChar(ch).unicode()); }
5028
}

src/Convert.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ namespace Convert
1010
QStringList ToQStringList(const std::list<std::string>& srcList);
1111
/// @brief Returns list `std::list` of `std::string` as element by converting `QStringList<QString>` value.
1212
inline std::list<std::string> ToStdStringList(const QStringList& srcList);
13-
/// @brief Return enum `Languages` value that equivalent to the text value.
14-
Languages ToLangEnum(const std::string& locale);
15-
/// @brief Return enum `Languages` value that equivalent to the text value.
16-
Languages ToLangEnum(const QString& locale);
1713
/// @brief Converts `QChar` to `Qt::Key` enum equivalent.
1814
Qt::Key ToQtKey(const QChar& ch);
19-
/// @brief Converts enum `Language` to `QString`.
20-
QString ToQString(const Languages& locale);
2115
}

src/Faction.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ Faction::Faction(const QJsonObject& factionAsObject)
1818
, displayNameDescription{factionAsObject[PROGRAM_CONSTANTS->DISPLAY_NAME_DESCRIPTION].toString()}
1919
, techTree{ParseJsonObject(factionAsObject)}
2020
{
21-
for (int currLng = 0; currLng < static_cast<int>(Languages::Count); currLng++)
21+
for (size_t lng : PROGRAM_CONSTANTS->Languages.keys())
2222
{
2323
QString _displayName = "";
2424
QString _displayNameDescription = "";
25-
Languages lng = static_cast<Languages>(currLng);
2625

27-
if (lng != Languages::English)
26+
if (lng != PROGRAM_CONSTANTS->DEFAULT_LANGUAGE_CODE)
2827
{
29-
auto translatedNames = factionAsObject[Windows::Locale::GetLanguageShortName(lng)].toObject();
28+
auto translatedNames = factionAsObject[PROGRAM_CONSTANTS->Languages.value(lng).first].toObject();
3029

3130
if (!translatedNames.isEmpty())
3231
{
@@ -57,23 +56,23 @@ const QString Faction::GetDisplayName() const { return displayName; }
5756
const QString Faction::GetDisplayNameDescription() const { return displayNameDescription; }
5857
const QMap<Faction::GameObject, GameObjectTypes>& Faction::GetTechTree() const { return techTree; }
5958

60-
const QString Faction::GetDisplayName(Languages lng) const
59+
const QString Faction::GetDisplayName(size_t lng) const
6160
{
6261
QString ret;
6362

64-
if (lng != Languages::English)
63+
if (lng != PROGRAM_CONSTANTS->DEFAULT_LANGUAGE_CODE)
6564
ret = localizedDisplay.value(lng).first;
6665

6766
if (ret == StringExt::EmptyString)
6867
ret = displayName;
6968

7069
return ret;
7170
}
72-
const QString Faction::GetDisplayNameDescription(Languages lng) const
71+
const QString Faction::GetDisplayNameDescription(size_t lng) const
7372
{
7473
QString ret;
7574

76-
if (lng != Languages::English)
75+
if (lng != PROGRAM_CONSTANTS->DEFAULT_LANGUAGE_CODE)
7776
ret = localizedDisplay.value(lng).second;
7877

7978
if (ret == StringExt::EmptyString)

src/Faction.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Faction
2727
QString displayName;
2828
QString displayNameDescription;
2929
QMap<GameObject, GameObjectTypes> techTree;
30-
QMap<Languages, QPair<QString, QString>> localizedDisplay;
30+
QMap<size_t, QPair<QString, QString>> localizedDisplay;
3131
public:
3232
inline static const int BASIC_FACTION_COUNT = 12;
3333

@@ -43,11 +43,11 @@ class Faction
4343
/// @brief Returns another one short faction name from field `DisplayName` of TechTree.json.
4444
const QString GetDisplayName() const;
4545
/// @brief Returns another one short faction name from field `DisplayName` of TechTree.json for the specific locale.
46-
const QString GetDisplayName(Languages lng) const;
46+
const QString GetDisplayName(size_t lng) const;
4747
/// @brief Returns long faction name from field `DisplayNameDescription` of TechTree.json.
4848
const QString GetDisplayNameDescription() const;
4949
/// @brief Returns long faction name from field `DisplayNameDescription` of TechTree.json for the specific locale.
50-
const QString GetDisplayNameDescription(Languages lng) const;
50+
const QString GetDisplayNameDescription(size_t lng) const;
5151
/// @brief Returns link to the techTree field.
5252
const QMap<GameObject, GameObjectTypes>& GetTechTree() const;
5353
/// @brief Returns link to the keyboard layout vector searching by object name.

src/GUI/EditorWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ EditorWindow::EditorWindow(QWidget* parent)
5151

5252
QBoxLayout* ltFactions = nullptr;
5353
int factonsCount = FACTIONS_MANAGER->Count();
54-
Languages language = PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
54+
size_t language = PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
5555

5656
if (factonsCount == Faction::BASIC_FACTION_COUNT)
5757
{

src/GUI/SettingsWindow.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ SettingsWindow::SettingsWindow(QWidget* parent) : QWidget(parent)
4848
lblLanguage->setText(tr("Language:"));
4949
lblLanguage->setObjectName(nameof(lblLanguage));
5050

51-
for (int i = 0; i < static_cast<int>(Languages::Count); ++i)
52-
cmbLanguage->addItem(Windows::Locale::GetLanguageFullName(static_cast<Languages>(i)));
51+
for (size_t i : PROGRAM_CONSTANTS->Languages.keys())
52+
cmbLanguage->addItem(PROGRAM_CONSTANTS->Languages.value(i).second);
53+
5354
cmbLanguage->setCurrentIndex(static_cast<int>(PROGRAM_CONSTANTS->pSettingsFile->GetLanguage()));
54-
cmbLanguage->setCurrentText(PROGRAM_CONSTANTS->LANGUAGES_STRINGS.value(PROGRAM_CONSTANTS->pSettingsFile->GetLanguage()).second);
55+
cmbLanguage->setCurrentText(PROGRAM_CONSTANTS->Languages.value(PROGRAM_CONSTANTS->pSettingsFile->GetLanguage()).second);
5556

5657
ltLanguage->setAlignment(Qt::AlignLeading);
5758
ltLanguage->addWidget(lblLanguage);
@@ -101,10 +102,10 @@ void SettingsWindow::BtnSave_Clicked()
101102

102103
PROGRAM_CONSTANTS->pSettingsFile->SetForceSystemLanguageOnStartUp(chkForceSystemLanguageOnStartUp->checkState());
103104

104-
bool isNewLanguageAssigned = static_cast<Languages>(cmbLanguage->currentIndex()) != PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
105+
bool isNewLanguageAssigned = cmbLanguage->currentIndex() != PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
105106

106107
if (isNewLanguageAssigned)
107-
PROGRAM_CONSTANTS->pSettingsFile->SetLanguage(static_cast<Languages>(cmbLanguage->currentIndex()));
108+
PROGRAM_CONSTANTS->pSettingsFile->SetLanguage(cmbLanguage->currentIndex());
108109

109110
PROGRAM_CONSTANTS->pSettingsFile->Save();
110111

src/GUI/Translations/ru.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,17 @@
187187
<context>
188188
<name>QObject</name>
189189
<message>
190-
<location filename="../../ProgramConstants.hpp" line="79"/>
190+
<location filename="../../ProgramConstants.hpp" line="78"/>
191191
<source>Error with CSF file</source>
192192
<translation>Ошибка с CSF файлом</translation>
193193
</message>
194194
<message>
195-
<location filename="../../ProgramConstants.hpp" line="80"/>
195+
<location filename="../../ProgramConstants.hpp" line="79"/>
196196
<source>Cannot process the empty file.</source>
197197
<translation>Невозможно обработать пустой файл.</translation>
198198
</message>
199199
<message>
200-
<location filename="../../ProgramConstants.hpp" line="81"/>
200+
<location filename="../../ProgramConstants.hpp" line="80"/>
201201
<source>Unable to find selected CSF file.</source>
202202
<translation>Невозможно найти указанный CSF файл.</translation>
203203
</message>
@@ -206,52 +206,52 @@
206206
<translation></translation>
207207
</message>
208208
<message>
209-
<location filename="../../ProgramConstants.hpp" line="82"/>
209+
<location filename="../../ProgramConstants.hpp" line="81"/>
210210
<source>Choosen CSF file doesn&apos;t have CONTROLBAR category. Make sure that you are load correct file.</source>
211211
<translation>У выбранного CSF файла отсутствует категория CONTROLBAR. Проверьте, что вы загружаете правильный файл.</translation>
212212
</message>
213213
<message>
214-
<location filename="../../ProgramConstants.hpp" line="83"/>
214+
<location filename="../../ProgramConstants.hpp" line="82"/>
215215
<source>Choosen CSF file doesn&apos;t have OBJECT category. Make sure that you are load correct file.</source>
216216
<translation>У выбранного CSF файла отсутствует категория OBJECT. Проверьте, что вы загружаете правильный файл.</translation>
217217
</message>
218218
<message>
219-
<location filename="../../ProgramConstants.hpp" line="84"/>
219+
<location filename="../../ProgramConstants.hpp" line="83"/>
220220
<source>Unable to find &quot;generals.csf&quot; file in &quot;%1&quot; folder.</source>
221221
<translation>Невозможно найти &quot;generals.csf&quot; в папке &quot;%1&quot;.</translation>
222222
</message>
223223
<message>
224-
<location filename="../../ProgramConstants.hpp" line="85"/>
224+
<location filename="../../ProgramConstants.hpp" line="84"/>
225225
<source>Unable to find CSF file inside BIG archive &quot;%1&quot;</source>
226226
<translation>Невозможно найти CSF файл внутри BIG архива &quot;%1&quot;</translation>
227227
</message>
228228
<message>
229-
<location filename="../../ProgramConstants.hpp" line="86"/>
229+
<location filename="../../ProgramConstants.hpp" line="85"/>
230230
<source>Game files search error</source>
231231
<translation>Ошибка поиска по игровым файлам</translation>
232232
</message>
233233
<message>
234-
<location filename="../../ProgramConstants.hpp" line="87"/>
234+
<location filename="../../ProgramConstants.hpp" line="86"/>
235235
<source>Unable to find &quot;EnglishZH.big&quot; archive in &quot;%1&quot; folder.</source>
236236
<translation>Невозможно найти &quot;EnglishZH.big&quot; в папке &quot;%1&quot;.</translation>
237237
</message>
238238
<message>
239-
<location filename="../../ProgramConstants.hpp" line="120"/>
239+
<location filename="../../ProgramConstants.hpp" line="113"/>
240240
<source>Buildings</source>
241241
<translation>Здания</translation>
242242
</message>
243243
<message>
244-
<location filename="../../ProgramConstants.hpp" line="121"/>
244+
<location filename="../../ProgramConstants.hpp" line="114"/>
245245
<source>Infantry</source>
246246
<translation>Пехота</translation>
247247
</message>
248248
<message>
249-
<location filename="../../ProgramConstants.hpp" line="122"/>
249+
<location filename="../../ProgramConstants.hpp" line="115"/>
250250
<source>Vehicles</source>
251251
<translation>Техника</translation>
252252
</message>
253253
<message>
254-
<location filename="../../ProgramConstants.hpp" line="123"/>
254+
<location filename="../../ProgramConstants.hpp" line="116"/>
255255
<source>Aircrafts</source>
256256
<translation>Авиация</translation>
257257
</message>
@@ -279,37 +279,37 @@
279279
<translation>Язык:</translation>
280280
</message>
281281
<message>
282-
<location filename="../SettingsWindow.cpp" line="65"/>
282+
<location filename="../SettingsWindow.cpp" line="66"/>
283283
<source>SAVE</source>
284284
<translation>СОХРАНИТЬ</translation>
285285
</message>
286286
<message>
287-
<location filename="../SettingsWindow.cpp" line="69"/>
287+
<location filename="../SettingsWindow.cpp" line="70"/>
288288
<source>RESET ALL</source>
289289
<translation>СБРОСИТЬ ВСЁ</translation>
290290
</message>
291291
<message>
292-
<location filename="../SettingsWindow.cpp" line="73"/>
292+
<location filename="../SettingsWindow.cpp" line="74"/>
293293
<source>BACK</source>
294294
<translation>НАЗАД</translation>
295295
</message>
296296
<message>
297-
<location filename="../SettingsWindow.cpp" line="117"/>
297+
<location filename="../SettingsWindow.cpp" line="118"/>
298298
<source>Setting Apply Confirmation</source>
299299
<translation>Подтверждение применения настроек</translation>
300300
</message>
301301
<message>
302-
<location filename="../SettingsWindow.cpp" line="118"/>
302+
<location filename="../SettingsWindow.cpp" line="119"/>
303303
<source>You have selected settings that require a complete restart of the editor. Do you want to apply new settings and restart editor?</source>
304304
<translation>Вы выбрали настройки, которые требуют полного перезапуска редактора. Вы желаете применить новые настройки и перезапустить редактор?</translation>
305305
</message>
306306
<message>
307-
<location filename="../SettingsWindow.cpp" line="120"/>
307+
<location filename="../SettingsWindow.cpp" line="121"/>
308308
<source>YES</source>
309309
<translation>ДА</translation>
310310
</message>
311311
<message>
312-
<location filename="../SettingsWindow.cpp" line="121"/>
312+
<location filename="../SettingsWindow.cpp" line="122"/>
313313
<source>CANCEL</source>
314314
<translation>ОТМЕНА</translation>
315315
</message>

src/GUI/WindowManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ void WindowManager::StartUpWindow_AcceptConfiguration()
106106

107107
void WindowManager::SetTranslator()
108108
{
109-
Languages lang;
109+
size_t language;
110110

111111
if (pAppTranslator != nullptr)
112112
qApp->removeTranslator(pAppTranslator);
113113

114-
lang = PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
114+
language = PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
115115

116-
QString lngShortName = Windows::Locale::GetLanguageShortName(lang);
116+
QString lngShortName = PROGRAM_CONSTANTS->Languages.value(language).first;
117117
LOGMSG("Set editor language to " + lngShortName.toUpper());
118118

119119
pAppTranslator = new QTranslator();

src/Main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ int main(int argc, const char** argv)
6565
if (!filesystem::exists(PROGRAM_CONSTANTS->TRANSLATIONS_FOLDER.toStdString().c_str()))
6666
return ShowErrorMessage(PROGRAM_CONSTANTS->TRANSLATIONS_NO_FOUND);
6767

68-
PROGRAM_CONSTANTS->InitializeFileSettings();
6968
PROGRAM_CONSTANTS->InitializeTranslations();
69+
PROGRAM_CONSTANTS->InitializeFileSettings();
7070

7171
// Show console, that by default is hiding by Logger class
7272
if (PROGRAM_CONSTANTS->pSettingsFile->IsConsoleEnabled())

src/ProgramConstants.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ void ProgramConstants::InitializeFileSettings() { pSettingsFile = std::make_uniq
1414

1515
void ProgramConstants::InitializeTranslations()
1616
{
17-
QDir transDir(PROGRAM_CONSTANTS->TRANSLATIONS_FOLDER);
17+
QDir transDir(TRANSLATIONS_FOLDER);
1818
int i = 0;
19-
_Languages.insert(0, {"en", "English"});
19+
Languages.insert(DEFAULT_LANGUAGE_CODE, {"en", "English"});
2020

2121
if (transDir.isEmpty())
2222
{
23-
LOGMSG(PROGRAM_CONSTANTS->TRANSLATIONS_FOLDER + " " + "is empty! Only English language available.");
23+
LOGMSG(TRANSLATIONS_FOLDER + " " + "is empty! Only English language available.");
2424
return;
2525
}
2626

2727
for (const auto& file : transDir.entryInfoList(QStringList( "*.qm" ), QDir::Filter::Files))
2828
{
2929
++i;
3030
LOGMSG("Filtered file: " + file.fileName());
31-
LOGMSG("Inserting: {" + file.completeBaseName() + ", " + Windows::Locale::LanguageName(file.completeBaseName()) + "}");
32-
_Languages.insert(i, {file.completeBaseName(), Windows::Locale::LanguageName(file.completeBaseName())});
31+
LOGMSG("Inserting: {" + file.completeBaseName().toLower() + ", " + Windows::Locale::LanguageName(file.completeBaseName().toLower()) + "}");
32+
Languages.insert(i, {file.completeBaseName().toLower(), Windows::Locale::LanguageName(file.completeBaseName().toLower())});
3333
}
3434
}

0 commit comments

Comments
 (0)