yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
language_manager.h
Go to the documentation of this file.
1#ifndef YAZE_UTIL_I18N_LANGUAGE_MANAGER_H
2#define YAZE_UTIL_I18N_LANGUAGE_MANAGER_H
3
4#include <functional>
5#include <map>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10namespace yaze {
11namespace i18n {
12
13// Holds the loaded translation catalogs and the active locale. Mirrors the
14// structure of yaze::gui::ThemeManager (Meyers singleton + single std::function
15// changed-callback + graceful fallback to the default locale).
16//
17// The English ("en") locale is the identity: it needs no catalog because
18// untranslated lookups fall back to the source string. Any locale is loaded
19// lazily from assets/i18n/<locale>.json via util::PlatformPaths::FindAsset.
21 public:
22 static LanguageManager& Get();
23
24 // Locale codes that have a catalog on disk, always including "en". Sorted.
25 std::vector<std::string> GetAvailableLocales() const;
26
27 const std::string& GetCurrentLocale() const { return current_locale_; }
28
29 // Switches the active language, loading its catalog on first use. Unknown or
30 // unreadable locales fall back to "en". Clears the translator caches and
31 // fires the changed-callback so the UI re-resolves strings next frame.
32 void SetLanguage(const std::string& locale);
33
34 using LanguageChangedCallback = std::function<void(const std::string&)>;
38
39 // Returns the translation of a visible key in the current locale, or nullptr
40 // if there is none (caller then keeps the source string). The pointer stays
41 // valid for the lifetime of the loaded catalog (catalogs are never evicted).
42 const std::string* Find(const std::string& visible_key) const;
43
44 // Test-only: injects a catalog for `locale` parsed from an in-memory flat
45 // JSON object, bypassing the filesystem. Returns false if the text is not a
46 // valid flat string->string object. Enables deterministic i18n tests.
47 bool LoadCatalogFromStringForTesting(const std::string& locale,
48 const std::string& json_text);
49
50 private:
52
53 // Loads assets/i18n/<locale>.json into catalogs_. Returns false on failure.
54 // "en" always succeeds (empty identity catalog).
55 bool LoadCatalog(const std::string& locale);
56
57 std::map<std::string, std::unordered_map<std::string, std::string>> catalogs_;
58 std::string current_locale_ = "en";
60};
61
62} // namespace i18n
63} // namespace yaze
64
65#endif // YAZE_UTIL_I18N_LANGUAGE_MANAGER_H
std::vector< std::string > GetAvailableLocales() const
const std::string * Find(const std::string &visible_key) const
void SetLanguage(const std::string &locale)
static LanguageManager & Get()
LanguageChangedCallback on_changed_
const std::string & GetCurrentLocale() const
std::function< void(const std::string &)> LanguageChangedCallback
void SetOnLanguageChangedCallback(LanguageChangedCallback cb)
bool LoadCatalog(const std::string &locale)
std::map< std::string, std::unordered_map< std::string, std::string > > catalogs_
bool LoadCatalogFromStringForTesting(const std::string &locale, const std::string &json_text)