Player12
An open source football management simulator
Loading...
Searching...
No Matches
language_manager.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9// -----------------------------------------------------------------------------
10// Football Management Project
11// Copyright (c) 2026 FlavioMili. All Rights Reserved.
12//
13// This file is part of the Football Management Project.
14// See the LICENSE file in the project root.
15// -----------------------------------------------------------------------------
16
17#ifndef LANGUAGE_MANAGER_H
18#define LANGUAGE_MANAGER_H
19
20#include <cstddef>
21#include <functional>
22#include <string>
23#include <string_view>
24#include <unordered_map>
25
26#include "global/languages.h"
28
39{
40 public:
42 struct Status
43 {
46 bool file_failed = false;
48 std::size_t missing_keys = 0;
49
50 [[nodiscard]] bool fallback() const
51 {
52 return file_failed || missing_keys > 0;
53 }
54 };
55
61 {
63 return instance;
64 }
65
73 bool loadLanguage(Language lang);
74
79 bool loadFromFile(const std::string& path, Language lang);
80
86 const char* get(const char* key) const;
87
89 [[nodiscard]] Language current() const { return status_.requested; }
90
92 [[nodiscard]] const Status& status() const { return status_; }
93
95 [[nodiscard]] const NumberFormat::Locale& numberLocale() const
96 {
97 return number_locale;
98 }
99
100 private:
102 {
103 using is_transparent = void;
104 std::size_t operator()(std::string_view text) const
105 {
106 return std::hash<std::string_view>{}(text);
107 }
108 };
109 using Table =
110 std::unordered_map<std::string, std::string, StringHash, std::equal_to<>>;
111
112 LanguageManager() = default;
114 bool ensureEnglish();
115 void refreshNumberLocale();
116
120 bool english_loaded = false;
123};
124
130#define LOC(key) LanguageManager::instance().get(key)
131
132#endif // LANGUAGE_MANAGER_H
Singleton class that manages localization and translations.
Definition language_manager.h:39
bool loadLanguage(Language lang)
Loads the JSON file for the specific language into memory.
Definition language_manager.cpp:103
Status status_
Definition language_manager.h:121
bool english_loaded
Definition language_manager.h:120
std::unordered_map< std::string, std::string, StringHash, std::equal_to<> > Table
Definition language_manager.h:110
const NumberFormat::Locale & numberLocale() const
Number and money conventions of the loaded texts.
Definition language_manager.h:95
const char * get(const char *key) const
Retrieves the localized string: the current language's, else the English one, else the key itself (th...
Definition language_manager.cpp:135
Table translations
Definition language_manager.h:118
Language current() const
The language last requested (even if it fell back).
Definition language_manager.h:89
LanguageManager()=default
void refreshNumberLocale()
Definition language_manager.cpp:76
bool ensureEnglish()
Definition language_manager.cpp:65
NumberFormat::Locale number_locale
Definition language_manager.h:122
const Status & status() const
Outcome of the last load, for a notice in the UI.
Definition language_manager.h:92
static LanguageManager & instance()
Gets the singleton instance of the LanguageManager.
Definition language_manager.h:60
bool loadFromFile(const std::string &path, Language lang)
Loads path as the texts of lang, with the same English fallback as loadLanguage() (which calls it wit...
Definition language_manager.cpp:113
Table english
Definition language_manager.h:119
ImGuiKey key
Definition input_actions.cpp:25
Language
Enum representing supported languages in the game.
Definition languages.h:24
What the last load left in place.
Definition language_manager.h:43
Language requested
Definition language_manager.h:44
bool fallback() const
Definition language_manager.h:50
std::size_t missing_keys
Definition language_manager.h:48
bool file_failed
Definition language_manager.h:46
Definition language_manager.h:102
std::size_t operator()(std::string_view text) const
Definition language_manager.h:104
void is_transparent
Definition language_manager.h:103
Separators and money units of one language.
Definition number_format.h:29