1 #ifndef LXGUI_GUI_LOCALIZER_HPP
2 #define LXGUI_GUI_LOCALIZER_HPP
4 #include "lxgui/gui_code_point_range.hpp"
5 #include "lxgui/lxgui.hpp"
8 #include <lxgui/extern_fmt.hpp>
9 #include <lxgui/extern_sol2_state.hpp>
10 #include <lxgui/extern_sol2_variadic_args.hpp>
12 #include <string_view>
13 #include <unordered_map>
187 std::string
format_string(std::string_view message, sol::variadic_args args)
const;
196 template<
typename... Args>
198 return fmt::format(locale_, message, std::forward<Args>(args)...);
209 std::string
localize(std::string_view key, sol::variadic_args args)
const;
223 template<
typename... Args>
224 std::string
localize(std::string_view key, Args&&... args)
const {
225 if (!is_key_valid_(key))
226 return std::string{key};
228 auto iter = find_key_(key);
229 if (iter == map_.end())
230 return std::string{key};
233 [&](
const auto& item) {
234 constexpr
bool is_string =
235 std::is_same_v<std::decay_t<decltype(item)>, std::string>;
236 if constexpr (is_string) {
237 if constexpr (
sizeof...(Args) == 0) {
241 locale_, fmt::runtime(item), std::forward<Args>(args)...);
244 auto result = item(std::forward<Args>(args)...);
245 if (result.valid() && result.begin() != result.end()) {
246 auto&& first = *result.begin();
247 if (first.template is<std::string>())
248 return first.template as<std::string>();
250 return std::string{key};
252 return std::string{key};
267 using hash_type = std::size_t;
268 using mapped_item = std::variant<std::string, sol::protected_function>;
269 using map_type = std::unordered_map<hash_type, mapped_item>;
272 std::vector<std::string> languages_;
273 std::vector<code_point_range> code_points_;
274 char32_t default_code_point_ = U
'\u25a1';
278 bool is_key_valid_(std::string_view key)
const;
279 map_type::const_iterator find_key_(std::string_view key)
const;
280 void reset_language_fallback_();
Utility class to translate strings for display in GUI.
localizer(const localizer &)=delete
localizer & operator=(localizer &&)=delete
void add_allowed_code_points_for_group(const std::string &unicode_group)
Adds a new range to the set of allowed code points from a Unicode group.
localizer(localizer &&)=delete
void clear_translations()
Removes all previously loaded translations.
void clear_allowed_code_points()
Removes all allowed code points.
const std::vector< std::string > & get_preferred_languages() const
Returns the list of code names of the preferred languages (used to translate messages and.
void auto_detect_preferred_languages()
Attempts to automatically detect the current language (used to translate messages and.
void load_translations(const std::string &folder_path)
Loads new translations from a folder, selecting the language automatically.
localizer & operator=(const localizer &)=delete
void add_allowed_code_points(const code_point_range &range)
Adds a new range to the set of allowed code points.
char32_t get_fallback_code_point() const
Returns the default character to display if a character is missing from a font.
void set_preferred_languages(const std::vector< std::string > &languages)
Changes the current language (used to translate messages and strings).
std::string localize(std::string_view key, Args &&... args) const
Translates a string with a certain number of arguments from C++ (zero or many).
void register_on_lua(sol::state &lua)
Registers this localizer on a Lua state.
const std::vector< code_point_range > & get_allowed_code_points() const
Returns the list of allowed code points (Unicode characters), for text rendering.
void add_allowed_code_points_for_language(const std::string &language_code)
Adds a new range to the set of allowed code points for a given language.
std::string format_string(std::string_view message, Args &&... args) const
Translates a string with a certain number of arguments from C++ (zero or many).
std::string format_string(std::string_view message, sol::variadic_args args) const
Translates a string with a certain number of arguments from Lua (zero or many).
localizer()
Default constructor.
void set_locale(const std::locale &locale)
Changes the current locale (used to format numbers).
void auto_detect_allowed_code_points()
Attempts to automatically detect the set of allowed code points based on preferred.
void set_fallback_code_point(char32_t code_point)
Sets the default character to display if a character is missing from a font.
const std::locale & get_locale() const
Returns the current locale (used to format numbers).
std::string localize(std::string_view key, sol::variadic_args args) const
Translates a string with a certain number of arguments from Lua (zero or many).
void load_translation_file(const std::string &file_name)
Loads new translations from a file.
Represents a contiguous range of unicode code points.