Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
TranslationManager.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Air-Trap
4** File description:
5** TranslationManager implementation
6*/
7
9#include <sstream>
10
11namespace rtp::client
12{
14 switch (lang) {
15 case Language::English: return "config/lang/en.txt";
16 case Language::French: return "config/lang/fr.txt";
17 case Language::Spanish: return "config/lang/es.txt";
18 case Language::German: return "config/lang/de.txt";
19 case Language::Italian: return "config/lang/it.txt";
20 default: return "config/lang/en.txt";
21 }
22 }
23
25 _translations.clear();
26 _currentLanguage = language;
27
28 std::string filePath = getLanguageFilePath(language);
29 std::ifstream file(filePath);
30
31 if (!file.is_open()) {
32 log::error("Failed to load language file: {}", filePath);
33 return false;
34 }
35
36 std::string line;
37 while (std::getline(file, line)) {
38 if (line.empty() || line[0] == '#') continue;
39
40 auto delimPos = line.find('=');
41 if (delimPos == std::string::npos) continue;
42
43 std::string key = line.substr(0, delimPos);
44 std::string value = line.substr(delimPos + 1);
45
46 key.erase(0, key.find_first_not_of(" \t"));
47 key.erase(key.find_last_not_of(" \t") + 1);
48 value.erase(0, value.find_first_not_of(" \t"));
49 value.erase(value.find_last_not_of(" \t") + 1);
50
51 _translations[key] = value;
52 }
53
54 log::info("Loaded {} translations for language: {}",
55 _translations.size(), static_cast<int>(language));
56 return true;
57 }
58
59 std::string TranslationManager::get(const std::string& key) const {
60 auto it = _translations.find(key);
61 if (it != _translations.end()) {
62 return it->second;
63 }
64
65 log::warning("Translation key not found: {}", key);
66 return key;
67 }
68} // namespace rtp::client
std::string getLanguageFilePath(Language lang) const
Get file path for a given language.
std::string get(const std::string &key) const
Get translated string for a key.
std::unordered_map< std::string, std::string > _translations
Map of translation keys to translated strings.
bool loadLanguage(Language language)
Load translations for a specific language.
Language _currentLanguage
Currently loaded language.
R-Type client namespace.
Language
Supported languages.
Definition Settings.hpp:33
void error(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an error message.
void warning(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a warning message.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.