Skip to content

Commit 4b57e1d

Browse files
Experimental implementation of faction translation
1 parent 8711226 commit 4b57e1d

File tree

6 files changed

+133
-52
lines changed

6 files changed

+133
-52
lines changed

src/Faction.cpp

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,85 @@
33
#include "Parsers/JSONFile.hpp"
44
#include "Logger.hpp"
55
#include "Faction.hpp"
6+
#include "Unsorted.hpp"
67

78
Faction::Faction(const QString& _shortName, const QString& _displayName, const QString& _displayNameDescription)
89
: shortName{_shortName}
910
, displayName{_displayName}
1011
, displayNameDescription{_displayNameDescription}
11-
{}
12+
{
13+
}
1214

1315
Faction::Faction(const QJsonObject& factionAsObject)
14-
: shortName{factionAsObject["ShortName"].toString()}
15-
, displayName{factionAsObject["DisplayName"].toString()}
16-
, displayNameDescription{factionAsObject["DisplayNameDescription"].toString()}
16+
: shortName{factionAsObject[PROGRAM_CONSTANTS->SHORT_NAME].toString()}
17+
, displayName{factionAsObject[PROGRAM_CONSTANTS->DISPLAY_NAME].toString()}
18+
, displayNameDescription{factionAsObject[PROGRAM_CONSTANTS->DISPLAY_NAME_DESCRIPTION].toString()}
1719
, techTree{ParseJsonObject(factionAsObject)}
18-
{}
20+
{
21+
for (int currLng = 0; currLng < static_cast<int>(Languages::Count); currLng++)
22+
{
23+
QString _displayName = "";
24+
QString _displayNameDescription = "";
25+
Languages lng = static_cast<Languages>(currLng);
26+
27+
if (lng != Languages::English)
28+
{
29+
auto translatedNames = factionAsObject[Unsorted::GetLanguageShortName(lng)].toObject();
30+
31+
if (!translatedNames.isEmpty())
32+
{
33+
if (!translatedNames[PROGRAM_CONSTANTS->DISPLAY_NAME].isNull()
34+
&& !translatedNames[PROGRAM_CONSTANTS->DISPLAY_NAME].isUndefined())
35+
{
36+
_displayName = translatedNames[PROGRAM_CONSTANTS->DISPLAY_NAME].toString();
37+
}
38+
39+
if (!translatedNames[PROGRAM_CONSTANTS->DISPLAY_NAME_DESCRIPTION].isNull()
40+
&& !translatedNames[PROGRAM_CONSTANTS->DISPLAY_NAME_DESCRIPTION].isUndefined())
41+
{
42+
_displayNameDescription = translatedNames[PROGRAM_CONSTANTS->DISPLAY_NAME_DESCRIPTION].toString();
43+
}
44+
}
45+
}
46+
else
47+
{
48+
continue;
49+
}
50+
51+
localizedDisplay.insert(lng, {_displayName, _displayNameDescription});
52+
}
53+
}
1954

20-
const QString& Faction::GetShortName() const { return shortName; }
21-
const QString& Faction::GetDisplayName() const { return displayName; }
22-
const QString& Faction::GetDisplayNameDescription() const { return displayNameDescription; }
55+
const QString Faction::GetShortName() const { return shortName; }
56+
const QString Faction::GetDisplayName() const { return displayName; }
57+
const QString Faction::GetDisplayNameDescription() const { return displayNameDescription; }
2358
const QMap<Faction::GameObject, GameObjectTypes>& Faction::GetTechTree() const { return techTree; }
2459

60+
const QString Faction::GetDisplayName(Languages lng) const
61+
{
62+
QString ret;
63+
64+
if (lng != Languages::English)
65+
ret = localizedDisplay.value(lng).first;
66+
67+
if (ret == StringExt::EmptyString)
68+
ret = displayName;
69+
70+
return ret;
71+
}
72+
const QString Faction::GetDisplayNameDescription(Languages lng) const
73+
{
74+
QString ret;
75+
76+
if (lng != Languages::English)
77+
ret = localizedDisplay.value(lng).second;
78+
79+
if (ret == StringExt::EmptyString)
80+
ret = displayNameDescription;
81+
82+
return ret;
83+
}
84+
2585
const QVector<QVector<Faction::Action>> Faction::GetKeyboardLayoutsByObjectName(const QString& objName) const
2686
{
2787
for(const Faction::GameObject& go : techTree.keys())

src/Faction.hpp

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

@@ -38,11 +39,15 @@ class Faction
3839
Faction(const QJsonObject& factionAsObject);
3940

4041
/// @brief Returns short faction name from field `ShortName` of TechTree.json.
41-
const QString& GetShortName() const;
42+
const QString GetShortName() const;
4243
/// @brief Returns another one short faction name from field `DisplayName` of TechTree.json.
43-
const QString& GetDisplayName() const;
44+
const QString GetDisplayName() const;
45+
/// @brief Returns another one short faction name from field `DisplayName` of TechTree.json for the specific locale.
46+
const QString GetDisplayName(Languages lng) const;
4447
/// @brief Returns long faction name from field `DisplayNameDescription` of TechTree.json.
45-
const QString& GetDisplayNameDescription() const;
48+
const QString GetDisplayNameDescription() const;
49+
/// @brief Returns long faction name from field `DisplayNameDescription` of TechTree.json for the specific locale.
50+
const QString GetDisplayNameDescription(Languages lng) const;
4651
/// @brief Returns link to the techTree field.
4752
const QMap<GameObject, GameObjectTypes>& GetTechTree() const;
4853
/// @brief Returns link to the keyboard layout vector searching by object name.

src/GUI/EditorWindow.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ EditorWindow::EditorWindow(QWidget* parent)
4949

5050
QBoxLayout* ltFactions = nullptr;
5151
int factonsCount = FACTIONS_MANAGER->Count();
52+
Languages language = PROGRAM_CONSTANTS->pSettingsFile->GetLanguage();
5253

5354
if (factonsCount == Faction::BASIC_FACTION_COUNT)
5455
{
@@ -67,11 +68,13 @@ EditorWindow::EditorWindow(QWidget* parent)
6768
{
6869
const Faction currFaction = FACTIONS_MANAGER->FindByIndex(sectionIndex + i);
6970

70-
QPushButton* factionButton = new QPushButton{currFaction.GetDisplayName()};
71+
QPushButton* factionButton = new QPushButton{currFaction.GetDisplayName(language)};
72+
factionButton->setToolTip(currFaction.GetDisplayNameDescription(language));
7173

7274
auto shortName = currFaction.GetShortName();
75+
7376
if (PROGRAM_CONSTANTS->USA_SHORT_NAMES.contains(shortName))
74-
factionButton->setProperty("faction", "USA");
77+
factionButton->setProperty("faction", "USA");
7578

7679
if (PROGRAM_CONSTANTS->PRC_SHORT_NAMES.contains(shortName))
7780
factionButton->setProperty("faction", "PRC");
@@ -389,7 +392,6 @@ QHBoxLayout* EditorWindow::CreateKeysOnKeyboard(const QString& str)
389392
return pKeys;
390393
}
391394

392-
393395
void EditorWindow::SetActionHotkey(const QString& fctShortName, const QString& goName, const QString& actName, const QString& hk)
394396
{
395397
Faction& fct = const_cast<Faction&>(FACTIONS_MANAGER->FindByShortName(fctShortName));

src/GUI/Translations/ru.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,99 +22,99 @@
2222
<context>
2323
<name>EditorWindow</name>
2424
<message>
25-
<location filename="../EditorWindow.cpp" line="172"/>
25+
<location filename="../EditorWindow.cpp" line="176"/>
2626
<source>File</source>
2727
<translation>Файл</translation>
2828
</message>
2929
<message>
30-
<location filename="../EditorWindow.cpp" line="173"/>
30+
<location filename="../EditorWindow.cpp" line="177"/>
3131
<source>Open</source>
3232
<translation>Открыть</translation>
3333
</message>
3434
<message>
35-
<location filename="../EditorWindow.cpp" line="174"/>
35+
<location filename="../EditorWindow.cpp" line="178"/>
3636
<source>Save</source>
3737
<translation>Сохранить</translation>
3838
</message>
3939
<message>
40-
<location filename="../EditorWindow.cpp" line="175"/>
40+
<location filename="../EditorWindow.cpp" line="179"/>
4141
<source>Save As...</source>
4242
<translation>Сохранить как...</translation>
4343
</message>
4444
<message>
45-
<location filename="../EditorWindow.cpp" line="176"/>
45+
<location filename="../EditorWindow.cpp" line="180"/>
4646
<source>Special</source>
4747
<translation>Дополнительно</translation>
4848
</message>
4949
<message>
50-
<location filename="../EditorWindow.cpp" line="192"/>
50+
<location filename="../EditorWindow.cpp" line="196"/>
5151
<source>View</source>
5252
<translation>Вид</translation>
5353
</message>
5454
<message>
55-
<location filename="../EditorWindow.cpp" line="193"/>
55+
<location filename="../EditorWindow.cpp" line="197"/>
5656
<source>Status Bar</source>
5757
<translation>Строка состояния</translation>
5858
</message>
5959
<message>
60-
<location filename="../EditorWindow.cpp" line="194"/>
60+
<location filename="../EditorWindow.cpp" line="198"/>
6161
<source>Enable</source>
6262
<translation>Включить</translation>
6363
</message>
6464
<message>
65-
<location filename="../EditorWindow.cpp" line="195"/>
65+
<location filename="../EditorWindow.cpp" line="199"/>
6666
<source>Disable</source>
6767
<translation>Отключить</translation>
6868
</message>
6969
<message>
70-
<location filename="../EditorWindow.cpp" line="199"/>
71-
<location filename="../EditorWindow.cpp" line="512"/>
70+
<location filename="../EditorWindow.cpp" line="203"/>
71+
<location filename="../EditorWindow.cpp" line="515"/>
7272
<source>Settings</source>
7373
<translation>Настройки</translation>
7474
</message>
7575
<message>
76-
<location filename="../EditorWindow.cpp" line="203"/>
77-
<location filename="../EditorWindow.cpp" line="477"/>
76+
<location filename="../EditorWindow.cpp" line="207"/>
77+
<location filename="../EditorWindow.cpp" line="480"/>
7878
<source>About</source>
7979
<translation>О программе</translation>
8080
</message>
8181
<message>
82-
<location filename="../EditorWindow.cpp" line="337"/>
82+
<location filename="../EditorWindow.cpp" line="341"/>
8383
<source>Layout %1</source>
8484
<translation>Раскладка %1</translation>
8585
</message>
8686
<message>
87-
<location filename="../EditorWindow.cpp" line="457"/>
87+
<location filename="../EditorWindow.cpp" line="460"/>
8888
<source>Authors: </source>
8989
<translation>Авторы: </translation>
9090
</message>
9191
<message>
92-
<location filename="../EditorWindow.cpp" line="458"/>
92+
<location filename="../EditorWindow.cpp" line="461"/>
9393
<source>Version: </source>
9494
<translation>Версия: </translation>
9595
</message>
9696
<message>
97-
<location filename="../EditorWindow.cpp" line="459"/>
97+
<location filename="../EditorWindow.cpp" line="462"/>
9898
<source>Program licensed with </source>
9999
<translation>Программа лицензирована под </translation>
100100
</message>
101101
<message>
102-
<location filename="../EditorWindow.cpp" line="460"/>
102+
<location filename="../EditorWindow.cpp" line="463"/>
103103
<source>GitHub repository:</source>
104104
<translation>Репозиторий на GitHub:</translation>
105105
</message>
106106
<message>
107-
<location filename="../EditorWindow.cpp" line="462"/>
107+
<location filename="../EditorWindow.cpp" line="465"/>
108108
<source>Support development:</source>
109109
<translation>Поддержать разработку:</translation>
110110
</message>
111111
<message>
112-
<location filename="../EditorWindow.cpp" line="552"/>
112+
<location filename="../EditorWindow.cpp" line="555"/>
113113
<source>Binary files</source>
114114
<translation>Двоичные файлы</translation>
115115
</message>
116116
<message>
117-
<location filename="../EditorWindow.cpp" line="553"/>
117+
<location filename="../EditorWindow.cpp" line="556"/>
118118
<source>Any files</source>
119119
<translation>Все файлы</translation>
120120
</message>
@@ -187,17 +187,17 @@
187187
<context>
188188
<name>QObject</name>
189189
<message>
190-
<location filename="../../ProgramConstants.hpp" line="74"/>
190+
<location filename="../../ProgramConstants.hpp" line="79"/>
191191
<source>Error with CSF file</source>
192192
<translation>Ошибка с CSF файлом</translation>
193193
</message>
194194
<message>
195-
<location filename="../../ProgramConstants.hpp" line="75"/>
195+
<location filename="../../ProgramConstants.hpp" line="80"/>
196196
<source>Cannot process the empty file.</source>
197197
<translation>Невозможно обработать пустой файл.</translation>
198198
</message>
199199
<message>
200-
<location filename="../../ProgramConstants.hpp" line="76"/>
200+
<location filename="../../ProgramConstants.hpp" line="81"/>
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="77"/>
209+
<location filename="../../ProgramConstants.hpp" line="82"/>
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="78"/>
214+
<location filename="../../ProgramConstants.hpp" line="83"/>
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="79"/>
219+
<location filename="../../ProgramConstants.hpp" line="84"/>
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="80"/>
224+
<location filename="../../ProgramConstants.hpp" line="85"/>
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="81"/>
229+
<location filename="../../ProgramConstants.hpp" line="86"/>
230230
<source>Game files search error</source>
231231
<translation>Ошибка поиска по игровым файлам</translation>
232232
</message>
233233
<message>
234-
<location filename="../../ProgramConstants.hpp" line="82"/>
234+
<location filename="../../ProgramConstants.hpp" line="87"/>
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="115"/>
239+
<location filename="../../ProgramConstants.hpp" line="120"/>
240240
<source>Buildings</source>
241241
<translation>Здания</translation>
242242
</message>
243243
<message>
244-
<location filename="../../ProgramConstants.hpp" line="116"/>
244+
<location filename="../../ProgramConstants.hpp" line="121"/>
245245
<source>Infantry</source>
246246
<translation>Пехота</translation>
247247
</message>
248248
<message>
249-
<location filename="../../ProgramConstants.hpp" line="117"/>
249+
<location filename="../../ProgramConstants.hpp" line="122"/>
250250
<source>Vehicles</source>
251251
<translation>Техника</translation>
252252
</message>
253253
<message>
254-
<location filename="../../ProgramConstants.hpp" line="118"/>
254+
<location filename="../../ProgramConstants.hpp" line="123"/>
255255
<source>Aircrafts</source>
256256
<translation>Авиация</translation>
257257
</message>

src/ProgramConstants.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ enum class GameObjectTypes
2222
class ProgramConstants
2323
{
2424
public:
25-
inline static std::unique_ptr<ProgramConstants> Instance = nullptr;
25+
inline static std::unique_ptr<ProgramConstants> Instance = nullptr;
2626
std::unique_ptr<Settings> pSettingsFile = nullptr;
2727

2828
// Folders
@@ -51,6 +51,11 @@ class ProgramConstants
5151
const QString LOAD_TITLE = SHORT_COMMON_TITLE + " — Load";
5252
const QString CREATE_TITLE = SHORT_COMMON_TITLE + " — New Set Up";
5353

54+
// JSON mappings
55+
const QString SHORT_NAME = "ShortName";
56+
const QString DISPLAY_NAME = "DisplayName";
57+
const QString DISPLAY_NAME_DESCRIPTION = "DisplayNameDescription";
58+
5459
// Magic numbers that become known
5560
const double START_WIDGET_SIZE_RATIO = 3./7.;
5661
const QSize START_BUTTON_SIZE = QSize(230, 110);

0 commit comments

Comments
 (0)