Language settings menu

This commit is contained in:
MihailRis
2023-12-13 04:04:23 +03:00
parent e97eb441d5
commit 4de8759ea2
10 changed files with 104 additions and 33 deletions
+11 -7
View File
@@ -17,7 +17,7 @@ using std::filesystem::path;
namespace fs = std::filesystem;
unique_ptr<langs::Lang> langs::current;
vector<langs::LocaleInfo> langs::locales_info;
unordered_map<string, langs::LocaleInfo> langs::locales_info;
langs::Lang::Lang(string locale) : locale(locale) {
}
@@ -34,6 +34,10 @@ void langs::Lang::put(const wstring& key, const wstring& text) {
map[key] = text;
}
const string& langs::Lang::getId() const {
return locale;
}
/* Language key-value txt files parser */
class Reader : public BasicParser {
void skipWhitespace() override {
@@ -84,14 +88,14 @@ void langs::loadLocalesInfo(const path& resdir, string& fallback) {
}
std::cout << "locale " << entry.first << " (" << name << ") added" << std::endl;
langs::locales_info.push_back(LocaleInfo {entry.first, name});
langs::locales_info[entry.first] = LocaleInfo {entry.first, name};
}
}
}
void langs::load(const path& resdir,
const string& locale,
vector<const ContentPack*>& packs,
const vector<ContentPack>& packs,
Lang& lang) {
path filename = path(TEXTS_FOLDER)/path(locale + LANG_FILE_EXT);
path core_file = resdir/filename;
@@ -101,11 +105,11 @@ void langs::load(const path& resdir,
reader.read(lang, "");
}
for (auto pack : packs) {
path file = pack->getFolder()/filename;
path file = pack.getFolder()/filename;
if (fs::is_regular_file(file)) {
string text = files::read_string(file);
Reader reader(file.string(), text);
reader.read(lang, pack->getId()+":");
reader.read(lang, pack.getId()+":");
}
}
}
@@ -113,7 +117,7 @@ void langs::load(const path& resdir,
void langs::load(const path& resdir,
const string& locale,
const string& fallback,
vector<const ContentPack*>& packs) {
const vector<ContentPack>& packs) {
unique_ptr<Lang> lang (new Lang(locale));
load(resdir, fallback, packs, *lang.get());
load(resdir, locale, packs, *lang.get());
@@ -122,7 +126,7 @@ void langs::load(const path& resdir,
void langs::setup(const path& resdir,
const string& locale,
vector<const ContentPack*>& packs) {
const vector<ContentPack>& packs) {
string fallback = langs::FALLBACK_DEFAULT;
langs::loadLocalesInfo(resdir, fallback);
langs::load(resdir, locale, fallback, packs);
+7 -5
View File
@@ -7,7 +7,7 @@
#include <filesystem>
#include <unordered_map>
class ContentPack;
#include "../../content/ContentPack.h"
namespace langs {
const char LANG_FILE_EXT[] = ".txt";
@@ -32,6 +32,8 @@ namespace langs {
const std::wstring& get(const std::wstring& key) const;
void put(const std::wstring& key, const std::wstring& text);
const std::string& getId() const;
};
struct LocaleInfo {
@@ -40,7 +42,7 @@ namespace langs {
};
extern std::unique_ptr<Lang> current;
extern std::vector<LocaleInfo> locales_info;
extern std::unordered_map<std::string, LocaleInfo> locales_info;
extern void loadLocalesInfo(
const std::filesystem::path& resdir,
@@ -48,12 +50,12 @@ namespace langs {
extern void load(const std::filesystem::path& resdir,
const std::string& locale,
std::vector<const ContentPack*>& packs,
const std::vector<ContentPack>& packs,
Lang& lang);
extern void load(const std::filesystem::path& resdir,
const std::string& locale,
const std::string& fallback,
std::vector<const ContentPack*>& packs);
const std::vector<ContentPack>& packs);
extern const std::wstring& get(const std::wstring& key);
extern const std::wstring& get(const std::wstring& key,
@@ -61,7 +63,7 @@ namespace langs {
extern void setup(const std::filesystem::path& resdir,
const std::string& locale,
std::vector<const ContentPack*>& packs);
const std::vector<ContentPack>& packs);
}
#endif // FRONTEND_LOCALE_LANGS_H