Skip to content

Commit

Permalink
Merge pull request #500 from MAFINS/patch-translate
Browse files Browse the repository at this point in the history
  • Loading branch information
MAFINS authored Jan 31, 2023
2 parents 7477728 + 3bd4afb commit 5adc4d4
Show file tree
Hide file tree
Showing 22 changed files with 25,156 additions and 39 deletions.
24,596 changes: 24,596 additions & 0 deletions Solution/external/json/single_include/nlohmann/json.hpp

Large diffs are not rendered by default.

139 changes: 139 additions & 0 deletions Solution/source/Menu/Language.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Menyoo PC - Grand Theft Auto V single-player trainer mod
* Copyright (C) 2019 MAFINS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include "Language.h"

#include "..\Util\ExePath.h"
#include "..\Util\FileLogger.h"

#include <fstream>
#include <json\single_include\nlohmann\json.hpp>
using Json = nlohmann::json;


namespace Language
{
std::vector<Lang> allLangs;
Lang* selectedLang = nullptr;
std::string configLangName = std::string();


Lang::Lang(std::string aName, std::string aFilePath)
{
this->filepath = aFilePath;
this->name = aName;
this->pairs.clear();
}

std::string Lang::Translate(std::string text)
{
try
{
auto& ret = this->pairs.at(text);
return ret;
}
catch (std::out_of_range)
{
return text;
}
}

std::string TranslateToSelected(std::string text)
{
if (selectedLang != nullptr)
return selectedLang->Translate(text);
else
return text;
}

int Init()
{
allLangs.clear();
selectedLang = nullptr;

const std::string& dirpath = GetPathffA(Pathff::Language, false);
std::vector<std::string> allFiles;
const std::string& ext = ".json";
get_all_filenames_with_extension(dirpath, ext, allFiles, false);

for (const std::string& fn : allFiles)
{
const std::string& filePath = dirpath + "\\" + fn + ext;

Lang lang(fn, filePath);
//std::ifstream stream(filePath);
//Json doc = Json::parse(stream);
//lang.Dictionary() = doc;
allLangs.push_back(lang);
}

SetSelectedLangFromConfig();

return 0;
}

std::string GetSelectedLangTitle()
{
return configLangName.empty() ? "English" : configLangName;
}
int SetSelectedLangFromConfig()
{
std::for_each(allLangs.begin(), allLangs.end(), [](Lang& l) { l.Dictionary().clear(); });

if (configLangName.empty())
{
selectedLang = nullptr;
return 0;
}
for (auto& lang : allLangs)
{
if (lang.GetName() == configLangName)
{
selectedLang = &lang;
std::ifstream stream(lang.GetFilePath());
try
{
Json doc = Json::parse(stream);
lang.Dictionary() = doc;
ige::myLog << ige::LogType::LOG_INFO << "Loaded language file " << lang.GetFilePath();
}
catch (...)
{
ige::myLog << ige::LogType::LOG_ERROR << "Unable to load language file " << lang.GetFilePath();
return -1;
}

return 0;
}
}

ige::myLog << ige::LogType::LOG_ERROR << "Cannot find selected language in memory. Resetting to default";
ResetSelectedLang();
return -1;
}
int SetSelectedLangFromString(std::string aName)
{
configLangName = aName;
return SetSelectedLangFromConfig();
}
void SetSelectedLang(Lang* ptr)
{
configLangName.clear();
selectedLang = ptr;
if (ptr != nullptr)
configLangName = selectedLang->GetName();
SetSelectedLangFromConfig();
}
void ResetSelectedLang()
{
configLangName.clear();
selectedLang = nullptr;
}

}
48 changes: 48 additions & 0 deletions Solution/source/Menu/Language.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Menyoo PC - Grand Theft Auto V single-player trainer mod
* Copyright (C) 2019 MAFINS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#pragma once

#include <string>
#include <map>
#include <vector>

namespace Language
{
class Lang
{
private:
std::string filepath;
std::string name;
std::map<std::string, std::string> pairs;
public:
Lang(std::string aName, std::string afilePath);
inline const std::string& GetName() const { return this->name; }
inline const std::string& GetFilePath() const { return this->filepath; }
inline decltype(pairs)& Dictionary() { return this->pairs; }

std::string Translate(std::string text);
};


extern std::vector<Lang> allLangs;
extern Lang* selectedLang;
extern std::string configLangName;

std::string TranslateToSelected(std::string text);

int Init();

std::string GetSelectedLangTitle();
int SetSelectedLangFromConfig();
int SetSelectedLangFromString(std::string aName);
void SetSelectedLang(Lang* ptr);
void ResetSelectedLang();

}
43 changes: 28 additions & 15 deletions Solution/source/Menu/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "..\Scripting\GameplayCamera.h"
#include "..\Scripting\ModelNames.h" // _vNeonColours
#include "Routine.h" // (loop_no_clip_toggle, loop_hide_hud)
#include "Language.h"

#include <Windows.h>
#include <utility>
Expand Down Expand Up @@ -1126,8 +1127,10 @@ bool null;
int inull;
void nullFunc() { return; }

void AddTitle(const std::string& text)
void AddTitle(std::string text)
{
text = Language::TranslateToSelected(text);

if (titletext_ALPHA_DIS_TEMP)
{
Game::Print::setupdraw(font_title, Vector2(0.26, 0.26), true, false, false, titletext);
Expand Down Expand Up @@ -1204,7 +1207,8 @@ void AddOption(std::string text, bool& option_code_bool, void(&callback)(), int
OptionY = OptionY * 0.035f + 0.125f;

Game::Print::setupdraw();
if (font_options == 0) SET_TEXT_SCALE(0, 0.33f);
if (font_options == 0)
SET_TEXT_SCALE(0, 0.33f);
SET_TEXT_FONT(font_options);
SET_TEXT_COLOUR(optiontext.R, optiontext.G, optiontext.B, optiontext.A);
if (Menu::bit_mouse ? Menu::printingop == MouseSupport::currentopM : Menu::printingop == Menu::currentop)
Expand All @@ -1218,15 +1222,20 @@ void AddOption(std::string text, bool& option_code_bool, void(&callback)(), int
{
/*if (&option_code_bool != &null)*/ option_code_bool = true;
callback();
if (submenu_index != -1) Menu::SetSub_delayed = submenu_index;
if (submenu_index != -1)
Menu::SetSub_delayed = submenu_index;
}
}
else
{
if (font_options == 2 || font_options == 7) tempChar = " ~b~=="; // Font unsafe
else tempChar = " ~b~>"; // Font safe
if (font_options == 2 || font_options == 7)
tempChar = " ~b~=="; // Font unsafe
else
tempChar = " ~b~>"; // Font safe
}

text = Language::TranslateToSelected(text);

if (show_arrow || submenu_index != -1)
{
//Menu::possibleNameOfCurrentSubmenu = text;
Expand All @@ -1240,7 +1249,8 @@ void AddOption(std::string text, bool& option_code_bool, void(&callback)(), int
SET_TEXT_CENTRE(1);
Game::Print::drawstringGXT(text, 0.16f + menuPos.x, OptionY + menuPos.y);
}
else Game::Print::drawstringGXT(text, 0.066f + menuPos.x, OptionY + menuPos.y);
else
Game::Print::drawstringGXT(text, 0.066f + menuPos.x, OptionY + menuPos.y);
}
else
{
Expand All @@ -1249,7 +1259,8 @@ void AddOption(std::string text, bool& option_code_bool, void(&callback)(), int
SET_TEXT_CENTRE(1);
Game::Print::drawstring(text, 0.16f + menuPos.x, OptionY + menuPos.y);
}
else Game::Print::drawstring(text, 0.066f + menuPos.x, OptionY + menuPos.y);
else
Game::Print::drawstring(text, 0.066f + menuPos.x, OptionY + menuPos.y);
}
}
inline void AddOption(std::ostream& os, bool& option_code_bool, void(&callback)(), int submenu_index, bool show_arrow, bool gxt)
Expand Down Expand Up @@ -1364,7 +1375,7 @@ void AddLocal(const std::string& text, BOOL condition, void(&callback_ON)(), voi

OptionStatus(condition); // Display ON/OFF
}
void AddBreak(const std::string& text)
void AddBreak(std::string text)
{
Menu::printingop++; Menu::breakcount++;

Expand Down Expand Up @@ -1419,6 +1430,9 @@ void AddBreak(const std::string& text)
}

}

text = Language::TranslateToSelected(text);

if (Menu::bit_centre_breaks)
{
SET_TEXT_CENTRE(1);
Expand Down Expand Up @@ -1631,9 +1645,8 @@ inline void AddTexter(const std::string& text, int selectedindex, const TA& text
{
chartickStr = textarray.at(selectedindex);
}
const char* chartick = chartickStr.c_str();

chartick = DOES_TEXT_LABEL_EXIST(chartick) ? GET_FILENAME_FOR_AUDIO_CONVERSATION(chartick) : chartick;
chartickStr = DOES_TEXT_LABEL_EXIST(chartickStr.c_str()) ? GET_FILENAME_FOR_AUDIO_CONVERSATION(chartickStr.c_str()) : Language::TranslateToSelected(chartickStr);
FLOAT newXpos;
Game::Print::setupdraw(0, Vector2(0.26, 0.26), true, true, false, optiontext);

Expand All @@ -1646,26 +1659,26 @@ inline void AddTexter(const std::string& text, int selectedindex, const TA& text
textureRes.y /= (Game::defaultScreenRes.second * 2);
newXpos = get_xcoord_at_menu_rightEdge(textureRes.x - 0.005, 0.0f, true);
DRAW_SPRITE("CommonMenu", "arrowright", newXpos, OptionY + 0.016f + menuPos.y, textureRes.x, textureRes.y, 0.0f, selectedtext.R, selectedtext.G, selectedtext.B, selectedtext.A, false, 0); // Right
newXpos = get_xcoord_at_menu_rightEdge(textureRes.x - 0.005, textureRes.x - 0.005 + Game::Print::GetTextWidth(chartick), true);
newXpos = get_xcoord_at_menu_rightEdge(textureRes.x - 0.005, textureRes.x - 0.005 + Game::Print::GetTextWidth(chartickStr), true);
DRAW_SPRITE("CommonMenu", "arrowleft", newXpos, OptionY + 0.016f + menuPos.y, textureRes.x, textureRes.y, 0.0f, selectedtext.R, selectedtext.G, selectedtext.B, selectedtext.A, false, 0); // Left

Game::Print::setupdraw(0, Vector2(0.26, 0.26), true, true, false, selectedtext);
newXpos = get_xcoord_at_menu_rightEdge(Game::Print::GetTextWidth(chartick), textureRes.x - 0.005, true);
newXpos = get_xcoord_at_menu_rightEdge(Game::Print::GetTextWidth(chartickStr), textureRes.x - 0.005, true);
Game::Print::setupdraw(0, Vector2(0.26, 0.26), true, true, false, selectedtext);
}
else
{
newXpos = get_xcoord_at_menu_rightEdge(Game::Print::GetTextWidth(chartick), 0.0024f, true);
newXpos = get_xcoord_at_menu_rightEdge(Game::Print::GetTextWidth(chartickStr), 0.0024f, true);
Game::Print::setupdraw(0, Vector2(0.26, 0.26), true, true, false, selectedtext);
}
}
else
{
newXpos = get_xcoord_at_menu_rightEdge(Game::Print::GetTextWidth(chartick), 0.0024f, true);
newXpos = get_xcoord_at_menu_rightEdge(Game::Print::GetTextWidth(chartickStr), 0.0024f, true);
Game::Print::setupdraw(0, Vector2(0.26, 0.26), true, true, false, optiontext);
}

Game::Print::drawstring(chartick, newXpos, OptionY + 0.0056 + menuPos.y);
Game::Print::drawstring(chartickStr, newXpos, OptionY + 0.0056 + menuPos.y);
}

if (Menu::printingop == *Menu::currentopATM)
Expand Down
4 changes: 2 additions & 2 deletions Solution/source/Menu/Menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ enum class TICKOL : UINT8
PERCENTAGESTICKER,
};

void AddTitle(const std::string& text);
void AddTitle(std::string text);
void AddOption(std::string text, bool &option_code_bool = null, void(&callback)() = nullFunc, int submenu_index = -1, bool show_arrow = 0, bool gxt = 0);
inline void AddOption(std::ostream& os, bool &option_code_bool = null, void(&callback)() = nullFunc, int submenu_index = -1, bool show_arrow = 0, bool gxt = 0);
void OptionStatus(BOOL status);
void AddToggle(const std::string& text, bool &loop_variable, bool &extra_option_code_ON = null, bool &extra_option_code_OFF = null, bool gxt = 0);
void AddToggle(const std::string& text, bool &loop_variable, void(&callback_ON)(), void(&callback_OFF)(), bool gxt = 0);
void AddLocal(const std::string& text, BOOL condition, bool &option_code_ON, bool &option_code_OFF, bool gxt = 0);
void AddLocal(const std::string& text, BOOL condition, void(&callback_ON)(), void(&callback_OFF)(), bool gxt = 0);
void AddBreak(const std::string& text);
void AddBreak(std::string text);
void AddNumber(const std::string& text, float value, __int8 decimal_places, bool &A_PRESS = null, bool &RIGHT_PRESS = null, bool &LEFT_PRESS = null, bool gxt = 0);
void draw_tickol_tick_BNW(const std::string& textureDict, const std::string& normal, const std::string& selected, const RGBA& colour);
inline void draw_tickol_tick(TICKOL tickType);
Expand Down
4 changes: 4 additions & 0 deletions Solution/source/Menu/MenuConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "Menu.h"
#include "Routine.h"
#include "Language.h"

#include "..\Util\ExePath.h"
#include "..\Natives\types.h" // RGBA/RgbS
Expand Down Expand Up @@ -74,6 +75,8 @@ void MenuConfig::ConfigRead()
Menu::bit_centre_title = ini.GetBoolValue(section_settings.c_str(), "centre_title", Menu::bit_centre_title);
Menu::bit_centre_options = ini.GetBoolValue(section_settings.c_str(), "centre_options", Menu::bit_centre_options);
Menu::bit_centre_breaks = ini.GetBoolValue(section_settings.c_str(), "centre_breaks", Menu::bit_centre_breaks);
Language::configLangName = ini.GetValue(section_settings.c_str(), "language", Language::configLangName.c_str());
Language::Init();


std::string section_general = "general";/////////
Expand Down Expand Up @@ -322,6 +325,7 @@ void MenuConfig::ConfigSave()
ini.SetBoolValue(section_settings.c_str(), "centre_title", Menu::bit_centre_title);
ini.SetBoolValue(section_settings.c_str(), "centre_options", Menu::bit_centre_options);
ini.SetBoolValue(section_settings.c_str(), "centre_breaks", Menu::bit_centre_breaks);
ini.SetValue(section_settings.c_str(), "language", Language::configLangName.c_str());


std::string section_general = "general";/////////
Expand Down
1 change: 1 addition & 0 deletions Solution/source/Menu/Routine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ void set_no_clip_off1()
ENABLE_CONTROL_ACTION(2, INPUT_LOOK_BEHIND, TRUE);
ENABLE_CONTROL_ACTION(2, INPUT_VEH_LOOK_BEHIND, TRUE);
ENABLE_CONTROL_ACTION(2, INPUT_SELECT_WEAPON, TRUE);
bit_noclip_show_help = true;
}
void set_no_clip_off2()
{
Expand Down
1 change: 1 addition & 0 deletions Solution/source/Menu/submenu_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace SUB {
SETTINGS_COLOURS2,
SETTINGS_FONTS,
SETTINGS_FONTS2,
SETTINGS_LANGUAGE,

OBJECTSPAWNER_OBJS, // now used for object gun, etc.

Expand Down
1 change: 1 addition & 0 deletions Solution/source/Menu/submenu_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void Menu::submenu_switch()
case SUB::SETTINGS_COLOURS2: sub::SettingsColours2(); break;
case SUB::SETTINGS_FONTS: sub::SettingsFonts(); break;
case SUB::SETTINGS_FONTS2: sub::SettingsFonts2(); break;
case SUB::SETTINGS_LANGUAGE: sub::SettingsLanguage(); break;
case SUB::TIMECYCLES: sub::Timecycles_(); break;

case SUB::OBJECTSPAWNER_OBJS: sub::ObjectSpawner_objs(); break;
Expand Down
Loading

0 comments on commit 5adc4d4

Please sign in to comment.