Skip to content

Commit

Permalink
修复语言文件的定位问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohengying committed Jul 6, 2021
1 parent c4390a2 commit ec25bef
Showing 1 changed file with 61 additions and 59 deletions.
120 changes: 61 additions & 59 deletions api/language/I18nManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,85 @@
//

#include "I18nManager.h"
#include "BDSMod.h"
#include "tools/DirtyLogger.h"
#include <filesystem>
#include <fstream>
#include "BDSMod.h"
#include <iostream>

namespace trapdoor {
void I18nManager::findLanguageFile() {
namespace fs = std::filesystem;
fs::path path("plugins/trapdoor/lang");
if (!fs::exists(path)) {
L_WARNING("can't find the language dictionary");
return;
}
fs::directory_entry entry(path); //文件入口
if (entry.status().type() == fs::file_type::directory) {
for (const auto &iter: fs::directory_iterator(path)) {
if (fs::is_regular_file(iter.path())) {
readLanguageFile(iter.path().string());
}
}
}
void I18nManager::findLanguageFile() {
namespace fs = std::filesystem;
fs::path path("plugins/trapdoor/lang");
if (!fs::exists(path)) {
L_WARNING("can't find the language dictionary");
return;
}

void I18nManager::readLanguageFile(const std::string &path) {
if (path.size() < 10) { return; }
auto langName = path.substr(5, path.size() - 10);
L_DEBUG("find language file %s", langName.c_str());
json langJson;
try {
std::ifstream i(path);
i >> langJson;
} catch (std::exception &e) {
L_ERROR("can not read config file %s ", path.c_str());
return;
}

this->languages.insert(langName);
try {
for (auto&[key, value] : langJson.items()) {
this->strings[langName][key] = value.get<std::string>();
fs::directory_entry entry(path); //文件入口
if (entry.status().type() == fs::file_type::directory) {
for (const auto &iter : fs::directory_iterator(path)) {
if (fs::is_regular_file(iter.path())) {
readLanguageFile(iter.path().string());
}
} catch (std::exception &e) {
L_ERROR("can not read config file %s ", path.c_str());
}
}
}

bool I18nManager::tryChangeLanguage(const std::string &language) {
auto iter = this->languages.find(language);
if (iter == this->languages.end())return false;
this->current_lang = language;
return true;
void I18nManager::readLanguageFile(const std::string &path) {
if (path.size() < 10) {
return;
}

std::string I18nManager::get(const std::string &id) {
//找不到就按照原样返回
auto iter = this->strings[current_lang].find(id);
if (iter != this->strings[current_lang].end()) {
return iter->second;
}
return id;
// auto langName = path.substr(5, path.size() - 10);
auto langName = path.substr(path.find_last_of('\\') + 1,
path.size() - path.find_last_of('.'));
// L_WARNING("find language file [%s]", langName.c_str());
json langJson;
try {
std::ifstream i(path);
i >> langJson;
} catch (std::exception &e) {
L_ERROR("can not read config file %s ", path.c_str());
return;
}

void I18nManager::getSystemLangAndSet() {
LCID localeID = GetUserDefaultLCID();
unsigned short lang = localeID & 0xFF;
if (lang == LANG_CHINESE) {
this->tryChangeLanguage("zh_cn");
} else {
this->tryChangeLanguage("en_us");
this->languages.insert(langName);
try {
for (auto &[key, value] : langJson.items()) {
this->strings[langName][key] = value.get<std::string>();
}
} catch (std::exception &e) {
L_ERROR("can not read config file %s ", path.c_str());
}
}

std::string LANG(const std::string &l) {
return trapdoor::bdsMod->getI18NManager().get(l);
}
bool I18nManager::tryChangeLanguage(const std::string &language) {
auto iter = this->languages.find(language);
if (iter == this->languages.end())
return false;
this->current_lang = language;
return true;
}

std::string I18nManager::get(const std::string &id) {
//找不到就按照原样返回
auto iter = this->strings[current_lang].find(id);
if (iter != this->strings[current_lang].end()) {
return iter->second;
}
return id;
}

void I18nManager::getSystemLangAndSet() {
LCID localeID = GetUserDefaultLCID();
unsigned short lang = localeID & 0xFF;
if (lang == LANG_CHINESE) {
this->tryChangeLanguage("zh_cn");
} else {
this->tryChangeLanguage("en_us");
}
}

std::string LANG(const std::string &l) {
return trapdoor::bdsMod->getI18NManager().get(l);
}
} // namespace trapdoor

0 comments on commit ec25bef

Please sign in to comment.