Skip to content

Commit

Permalink
Change language in game
Browse files Browse the repository at this point in the history
  • Loading branch information
qweasdd136963 committed Nov 16, 2018
1 parent e7e2ab8 commit 980b515
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 8 deletions.
9 changes: 9 additions & 0 deletions res/gamedata/configs/text/eng/ui_st_mm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -969,4 +969,13 @@
<string id="ui_mm_confirm_changes">
<text>Apply changes? All changes will be reverted in</text>
</string>
<string id="ui_mm_localization">
<text>Language</text>
</string>
<string id="eng">
<text>English</text>
</string>
<string id="rus">
<text>Ðóññêèé</text>
</string>
</string_table>
9 changes: 9 additions & 0 deletions res/gamedata/configs/text/rus/ui_st_mm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -969,4 +969,13 @@
<string id="ui_mm_confirm_changes">
<text>Ïðèìåíèòü ñäåëàííûå èçìåíåíèÿ? Âñå èçìåíåíèÿ áóäóò îòìåíåíû ÷åðåç</text>
</string>
<string id="ui_mm_localization">
<text>ßçûê</text>
</string>
<string id="eng">
<text>English</text>
</string>
<string id="rus">
<text>Ðóññêèé</text>
</string>
</string_table>
12 changes: 12 additions & 0 deletions res/gamedata/configs/ui/ui_mm_opt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,18 @@
<options_item entry="rs_loadingstages" group="mm_opt_gameplay" is_integer="1"/>
</check_loadingstages>

<cap_localization x="20" y="3" width="135" height="24">
<text r="170" g="170" b="170" font="letterica16" align="r" vert_align="c">ui_mm_localization</text>
</cap_localization>
<list_localization x="180" y="5" width="235" height="20">
<options_item entry="g_language" group="mm_opt_gameplay"/>
<list_font r="170" g="170" b="170" font="letterica16"/>
<text_color>
<e r="170" g="170" b="170"/>
<d r="70" g="70" b="70"/>
</text_color>
</list_localization>

<btn_check_updates x="338" y="322" width="108" height="24">
<text align="c" font="letterica16">ui_mm_check_updates</text>
<texture>ui_inGame2_button</texture>
Expand Down
12 changes: 12 additions & 0 deletions res/gamedata/configs/ui/ui_mm_opt_16.xml
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,18 @@
<options_item entry="rs_loadingstages" group="mm_opt_gameplay" is_integer="1"/>
</check_loadingstages>

<cap_localization x="16" y="3" width="108" height="24">
<text r="170" g="170" b="170" font="letterica16" align="r" vert_align="c">ui_mm_localization</text>
</cap_localization>
<list_localization x="144" y="5" width="188" height="20">
<options_item entry="g_language" group="mm_opt_gameplay"/>
<list_font r="170" g="170" b="170" font="letterica16"/>
<text_color>
<e r="170" g="170" b="170"/>
<d r="70" g="70" b="70"/>
</text_color>
</list_localization>

<btn_check_updates x="270" y="322" width="86" height="24" stretch="1">
<text align="c" font="letterica16">ui_mm_check_updates</text>
<texture>ui_inGame2_button</texture>
Expand Down
5 changes: 5 additions & 0 deletions res/gamedata/scripts/ui_mm_opt_gameplay.script
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function opt_gameplay:InitControls(x, y, xml, handler)
xml:InitStatic("tab_gameplay:cap_difficulty", _st)
xml:InitComboBox("tab_gameplay:list_difficulty", _st)
self.scroll_v:AddWindow(_st, true)

_st = xml:InitStatic("tab_gameplay:templ_item", nil)
xml:InitStatic("tab_gameplay:cap_localization", _st)
xml:InitComboBox("tab_gameplay:list_localization", _st)
self.scroll_v:AddWindow(_st, true)

_st = xml:InitStatic("tab_gameplay:templ_item", nil)
xml:InitStatic("tab_gameplay:cap_ui_style", _st)
Expand Down
7 changes: 6 additions & 1 deletion src/xrGame/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ CMainMenu::CMainMenu()
g_btnHint = NULL;
g_statHint = NULL;
m_deactivated_frame = 0;
languageChanged = false;

m_sPatchURL = "";
#ifdef WINDOWS
Expand Down Expand Up @@ -347,6 +348,9 @@ bool CMainMenu::ReloadUI()
bool CMainMenu::IsActive() const { return m_Flags.test(flActive); }
bool CMainMenu::CanSkipSceneRendering() { return IsActive() && !m_Flags.test(flGameSaveScreenshot); }

bool CMainMenu::IsLanguageChanged() { return languageChanged; }
void CMainMenu::SetLanguageChanged(bool status) { languageChanged = status; }

// IInputReceiver
void CMainMenu::IR_OnMousePress(int btn)
{
Expand Down Expand Up @@ -557,8 +561,9 @@ void CMainMenu::OnFrame()
{
CheckForErrorDlg();
bool b_is_16_9 = (float)Device.dwWidth / (float)Device.dwHeight > (UI_BASE_WIDTH / UI_BASE_HEIGHT + 0.01f);
if (b_is_16_9 != m_activatedScreenRatio)
if (b_is_16_9 != m_activatedScreenRatio || languageChanged)
{
languageChanged = false;
ReloadUI();
m_startDialog->SendMessage(m_startDialog, MAIN_MENU_RELOADED, NULL);
}
Expand Down
5 changes: 5 additions & 0 deletions src/xrGame/MainMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class CMainMenu : public IMainMenu,

xr_vector<CUIWindow*> m_pp_draw_wnds;

bool languageChanged;

CGameSpy_Full* m_pGameSpyFull;
gamespy_gp::account_manager* m_account_mngr;
gamespy_gp::login_manager* m_login_mngr;
Expand Down Expand Up @@ -145,6 +147,9 @@ class CMainMenu : public IMainMenu,
bool IsActive() const override;
virtual bool CanSkipSceneRendering();

virtual bool IsLanguageChanged();
virtual void SetLanguageChanged(bool status);

virtual bool IgnorePause() { return true; }
virtual void IR_OnMousePress(int btn);
virtual void IR_OnMouseRelease(int btn);
Expand Down
35 changes: 35 additions & 0 deletions src/xrGame/console_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,40 @@ class CCC_GameDifficulty : public CCC_Token
virtual void Info(TInfo& I) { xr_strcpy(I, "game difficulty"); }
};

class CCC_GameLanguage : public CCC_Token
{
public:
CCC_GameLanguage(LPCSTR N) : CCC_Token(N, (u32*)&CStringTable::LanguageID, NULL){};

virtual void Execute(LPCSTR args)
{
CCC_Token::Execute(args);
StringTable().ReloadLanguage();

if (g_pGamePersistent && g_pGamePersistent->IsMainMenuActive())
MainMenu()->SetLanguageChanged(true);

if (!g_pGameLevel)
return;

for (u16 id = 0; id < 0xffff; id++)
{
IGameObject* gameObj = Level().Objects.net_Find(id);
if (gameObj)
{
if (CInventoryItem* invItem = gameObj->cast_inventory_item())
invItem->ReloadNames();
}
}
}

const xr_token* GetToken() noexcept override
{
tokens = StringTable().GetLanguagesToken();
return CCC_Token::GetToken();
}
};

#ifdef DEBUG
class CCC_ALifePath : public IConsole_Command
{
Expand Down Expand Up @@ -1753,6 +1787,7 @@ void CCC_RegisterCommands()
// game
CMD3(CCC_Mask, "g_crouch_toggle", &psActorFlags, AF_CROUCH_TOGGLE);
CMD1(CCC_GameDifficulty, "g_game_difficulty");
CMD1(CCC_GameLanguage, "g_language");

CMD3(CCC_Mask, "g_backrun", &psActorFlags, AF_RUN_BACKWARD);

Expand Down
7 changes: 7 additions & 0 deletions src/xrGame/inventory_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ void CInventoryItem::Load(LPCSTR section)
m_icon_name = READ_IF_EXISTS(pSettings, r_string, section, "icon_name", NULL);
}

void CInventoryItem::ReloadNames()
{
m_name = StringTable().translate(pSettings->r_string(m_object->cNameSect(), "inv_name"));
m_nameShort = StringTable().translate(pSettings->r_string(m_object->cNameSect(), "inv_name_short"));
m_Description = StringTable().translate(pSettings->r_string(m_object->cNameSect(), "description"));
}

void CInventoryItem::ChangeCondition(float fDeltaCondition)
{
m_fCondition += fDeltaCondition;
Expand Down
1 change: 1 addition & 0 deletions src/xrGame/inventory_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class CInventoryItem : public CAttachableItem,

public:
virtual void Load(LPCSTR section);
virtual void ReloadNames();

LPCSTR NameItem(); // remove <virtual> by sea
LPCSTR NameShort();
Expand Down
69 changes: 62 additions & 7 deletions src/xrGame/string_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@

CStringTable& StringTable() { return *((CStringTable*)gStringTable); }

STRING_TABLE_DATA* CStringTable::pData = NULL;
STRING_TABLE_DATA* CStringTable::pData = nullptr;
BOOL CStringTable::m_bWriteErrorsToLog = FALSE;
u32 CStringTable::LanguageID = std::numeric_limits<u32>::max();
xr_vector<xr_token> CStringTable::languagesToken;

CStringTable::CStringTable()
{
pData = nullptr;
FillLanguageToken();
}

CStringTable::~CStringTable() { Destroy(); }
void CStringTable::Destroy() { xr_delete(pData); }
void CStringTable::rescan()
{
if (NULL != pData)
if (pData != nullptr)
return;
Destroy();
Init();
}

void CStringTable::Init()
{
if (NULL != pData)
if (pData != nullptr)
return;

pData = new STRING_TABLE_DATA();

//имя языка, если не задано (NULL), то первый <text> в <string> в XML
pData->m_sLanguage = pSettings->r_string("string_table", "language");
SetLanguage();

//---
FS_FileSet fset;
string_path files_mask;
xr_sprintf(files_mask, "text" DELIMITER "%s" DELIMITER "*.xml", pData->m_sLanguage.c_str());
Expand All @@ -52,10 +53,55 @@ void CStringTable::Init()
#ifdef DEBUG
Msg("StringTable: loaded %d files", fset.size());
#endif // #ifdef DEBUG
//---

ReparseKeyBindings();
}

void CStringTable::FillLanguageToken()
{
if (languagesToken.size() == 0)
{
u32 lineCount = pSettings->line_count("Languages");
R_ASSERT2(lineCount > 0, "Section \"Languages\" is empty!");

LPCSTR lineName, lineVal;
for (u16 i = 0; i < lineCount; i++)
{
pSettings->r_line("Languages", i, &lineName, &lineVal);
languagesToken.emplace_back(lineName, i);
}
languagesToken.emplace_back(nullptr, -1);
}
}

void CStringTable::SetLanguage()
{
if (LanguageID != (u32)-1)
pData->m_sLanguage = languagesToken.at(LanguageID).name;
else
{
pData->m_sLanguage = pSettings->r_string("string_table", "language");
bool found = false;
for (const auto& it : languagesToken)
{
if (it.name && it.name == pData->m_sLanguage)
{
LanguageID = it.id;
found = true;
break;
}
}

if (!found)
R_ASSERT2(false, "Check localization.ltx");
}
}

xr_token* CStringTable::GetLanguagesToken() const
{
return languagesToken.data();
}

void CStringTable::Load(LPCSTR xml_file_full)
{
CUIXml uiXml;
Expand Down Expand Up @@ -87,6 +133,15 @@ void CStringTable::Load(LPCSTR xml_file_full)
}
}

void CStringTable::ReloadLanguage()
{
if (0 == xr_strcmp(languagesToken.at(LanguageID).name, pData->m_sLanguage.c_str()))
return;

Destroy();
Init();
}

void CStringTable::ReparseKeyBindings()
{
if (!pData)
Expand Down
8 changes: 8 additions & 0 deletions src/xrGame/string_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ class CStringTable : public IStringTable
STRING_VALUE translate(const STRING_ID& str_id) const;
void rescan();

void ReloadLanguage();

static BOOL m_bWriteErrorsToLog;
static void ReparseKeyBindings();

xr_token* GetLanguagesToken() const;
static u32 LanguageID;

private:
void Load(LPCSTR xml_file);
void FillLanguageToken();
void SetLanguage();
static STRING_VALUE ParseLine(LPCSTR str, LPCSTR key, bool bFirst);
static STRING_TABLE_DATA* pData;
static xr_vector<xr_token> languagesToken;
};

CStringTable& StringTable();

0 comments on commit 980b515

Please sign in to comment.