23#include <nlohmann/json.hpp>
27using json = nlohmann::json;
48Localization::Localization() : m_currentLanguage(
"en") {
58 if (m_translations.contains(languageCode)) {
59 m_currentLanguage = languageCode;
61 LOG_INFO(
"Language set to: {}", languageCode);
63 LOG_ERROR(
"Language not available: {}", languageCode);
68 return m_currentLanguage;
72 std::vector<std::string> languages;
73 languages.reserve(m_translations.size());
74 for (
const auto& pair : m_translations) {
75 languages.push_back(pair.first);
81 if (
auto cacheIt = m_trCache.find(key); cacheIt != m_trCache.end()) {
82 return cacheIt->second;
86 auto resolve = [
this, &key]() -> std::string {
87 if (
auto langIt = m_translations.find(m_currentLanguage); langIt != m_translations.end()) {
88 if (
auto keyIt = langIt->second.find(key); keyIt != langIt->second.end()) {
92 if (m_currentLanguage !=
"en") {
93 if (
auto enIt = m_translations.find(
"en"); enIt != m_translations.end()) {
94 if (
auto keyIt = enIt->second.find(key); keyIt != enIt->second.end()) {
102 std::string resolved = resolve();
105 m_trCache.emplace(key, resolved);
110 m_translations[languageCode][key] = value;
113 if (languageCode == m_currentLanguage || languageCode ==
"en") {
114 m_trCache.erase(key);
120 std::ifstream file(filepath);
121 if (!file.is_open()) {
126 LOG_DEBUG(
"Translations file not found at: {}", filepath);
130 json j = json::parse(file);
135 m_translations.clear();
138 for (
const auto& [languageCode, translations] : j.items()) {
139 for (
const auto& [key, value] : translations.items()) {
144 LOG_INFO(
"Loaded translations from: {}", filepath);
146 }
catch (
const std::exception& e) {
149 LOG_ERROR(
"Failed to load translations from {}: {}", filepath, e.what());