MetaImGUI 1.0.0
ImGui Application Template for C++20
Loading...
Searching...
No Matches
Localization.h
Go to the documentation of this file.
1/*
2 MetaImGUI
3 Copyright (C) 2026 A P Nicholson
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#include <map>
22#include <string>
23#include <unordered_map>
24#include <vector>
25
26namespace MetaImGUI {
27
38public:
42 static Localization& Instance();
43
44 // Delete copy and move
45 Localization(const Localization&) = delete;
49
54 void SetLanguage(const std::string& languageCode);
55
59 [[nodiscard]] std::string GetCurrentLanguage() const;
60
64 [[nodiscard]] std::vector<std::string> GetAvailableLanguages() const;
65
71 [[nodiscard]] std::string Tr(const std::string& key) const;
72
79 void AddTranslation(const std::string& languageCode, const std::string& key, const std::string& value);
80
86 bool LoadTranslations(const std::string& filepath);
87
88private:
90 ~Localization() = default;
91
92 std::string m_currentLanguage;
93 std::map<std::string, std::map<std::string, std::string>> m_translations; // [language][key] = value
94
95 // Tr() is on the hot path — every menu item, button and tooltip looks up
96 // the same handful of keys per frame. The cache resolves a key to a
97 // ready-made std::string, avoiding the two-tier map lookup + English
98 // fallback dance on every call. Invalidated on SetLanguage / load.
99 //
100 // Mutable because Tr() is logically const but caches the result of an
101 // expensive lookup. Single-threaded UI usage, no synchronisation needed.
102 mutable std::unordered_map<std::string, std::string> m_trCache;
103};
104
105} // namespace MetaImGUI
106
107// Convenience macro for translation
108#define TR(key) MetaImGUI::Localization::Instance().Tr(key)
Simple localization/internationalization system.
Localization(const Localization &)=delete
static Localization & Instance()
Get singleton instance.
Localization(Localization &&)=delete
void SetLanguage(const std::string &languageCode)
Set current language.
std::vector< std::string > GetAvailableLanguages() const
Get list of available languages.
std::string Tr(const std::string &key) const
Get translated string.
bool LoadTranslations(const std::string &filepath)
Load translations from JSON file.
std::string GetCurrentLanguage() const
Get current language code.
Localization & operator=(const Localization &)=delete
void AddTranslation(const std::string &languageCode, const std::string &key, const std::string &value)
Add translation for a language.
Localization & operator=(Localization &&)=delete