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 // Scanned once and cached: the Language menu asks for it every frame.
26 std::vector<std::string> GetAvailableLocales() const;
27 // Drops the cached list so the next call scans the catalog folder again.
29
30 const std::string& GetCurrentLocale() const { return current_locale_; }
31
32 // Switches the active language, loading its catalog on first use. Unknown or
33 // unreadable locales fall back to "en". Clears the translator caches and
34 // fires the changed-callback so the UI re-resolves strings next frame.
35 void SetLanguage(const std::string& locale);
36
37 using LanguageChangedCallback = std::function<void(const std::string&)>;
41
42 // Returns the translation of a visible key in the current locale, or nullptr
43 // if there is none (caller then keeps the source string). The pointer stays
44 // valid for the lifetime of the loaded catalog (catalogs are never evicted).
45 const std::string* Find(const std::string& visible_key) const;
46
47 // Test-only: injects a catalog for `locale` parsed from an in-memory flat
48 // JSON object, bypassing the filesystem. Returns false if the text is not a
49 // valid flat string->string object. Enables deterministic i18n tests.
50 bool LoadCatalogFromStringForTesting(const std::string& locale,
51 const std::string& json_text);
52
53 private:
55
56 // Loads assets/i18n/<locale>.json into catalogs_. Returns false on failure.
57 // "en" always succeeds (empty identity catalog).
58 bool LoadCatalog(const std::string& locale);
59
60 std::map<std::string, std::unordered_map<std::string, std::string>> catalogs_;
61 std::string current_locale_ = "en";
63 mutable std::vector<std::string> available_locales_; // empty = not scanned
64};
65
66} // namespace i18n
67} // namespace yaze
68
69#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
std::vector< std::string > available_locales_
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)