1#include <CS2Kit/Core/Paths.hpp>
2#include <CS2Kit/Utils/Log.hpp>
3#include <CS2Kit/Utils/Translations.hpp>
6#include <nlohmann/json.hpp>
11bool Translations::Load(
const std::string& dirPath)
13 _translations.clear();
14 namespace fs = std::filesystem;
18 if (!fs::exists(resolvedPath) || !fs::is_directory(resolvedPath))
20 Log::Warn(
"Translations directory not found: {}", resolvedPath.string());
25 for (
const auto& entry : fs::directory_iterator(resolvedPath))
27 if (entry.path().extension() !=
".json")
30 std::string langCode = entry.path().stem().string();
33 std::ifstream file(entry.path());
37 auto data = nlohmann::json::parse(file);
38 auto& langMap = _translations[langCode];
39 for (
auto& [key, value] : data.items())
41 if (value.is_string())
42 langMap[key] = value.get<std::string>();
46 Log::Info(
"Loaded translations: {} ({} keys)", langCode, langMap.size());
48 catch (
const std::exception& e)
50 Log::Warn(
"Failed to parse {}: {}", entry.path().string(), e.what());
54 Log::Info(
"Loaded {} language(s).", loaded);
58void Translations::SetLanguage(
const std::string& lang)
63const std::string& Translations::GetLanguage()
const
68std::string Translations::Get(
const std::string& key)
const
70 auto langIt = _translations.find(_activeLang);
71 if (langIt != _translations.end())
73 auto keyIt = langIt->second.find(key);
74 if (keyIt != langIt->second.end())
78 if (_activeLang !=
"en")
80 langIt = _translations.find(
"en");
81 if (langIt != _translations.end())
83 auto keyIt = langIt->second.find(key);
84 if (keyIt != langIt->second.end())
std::filesystem::path ResolvePath(const std::string &relativePath)