-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #500 from MAFINS/patch-translate
- Loading branch information
Showing
22 changed files
with
25,156 additions
and
39 deletions.
There are no files selected for viewing
24,596 changes: 24,596 additions & 0 deletions
24,596
Solution/external/json/single_include/nlohmann/json.hpp
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ⟨ | ||
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.