lxgui
gui_localizer.hpp
1 #ifndef LXGUI_GUI_LOCALIZER_HPP
2 #define LXGUI_GUI_LOCALIZER_HPP
3 
4 #include "lxgui/gui_code_point_range.hpp"
5 #include "lxgui/lxgui.hpp"
6 
7 #include <locale>
8 #include <lxgui/extern_fmt.hpp>
9 #include <lxgui/extern_sol2_state.hpp>
10 #include <lxgui/extern_sol2_variadic_args.hpp>
11 #include <string>
12 #include <string_view>
13 #include <unordered_map>
14 #include <variant>
15 #include <vector>
16 
17 namespace lxgui::gui {
18 
20 class localizer {
21 public:
23  localizer();
24 
25  // Non-copiable, non-movable
26  localizer(const localizer&) = delete;
27  localizer(localizer&&) = delete;
28  localizer& operator=(const localizer&) = delete;
30 
38  void set_locale(const std::locale& locale);
39 
54  void set_preferred_languages(const std::vector<std::string>& languages);
55 
57 
63 
68  const std::locale& get_locale() const;
69 
71 
75  const std::vector<std::string>& get_preferred_languages() const;
76 
82 
88  void add_allowed_code_points(const code_point_range& range);
89 
99  void add_allowed_code_points_for_group(const std::string& unicode_group);
100 
109  void add_allowed_code_points_for_language(const std::string& language_code);
110 
112 
118 
128  const std::vector<code_point_range>& get_allowed_code_points() const;
129 
134  void set_fallback_code_point(char32_t code_point);
135 
140  char32_t get_fallback_code_point() const;
141 
151  void load_translations(const std::string& folder_path);
152 
170  void load_translation_file(const std::string& file_name);
171 
178  void clear_translations();
179 
187  std::string format_string(std::string_view message, sol::variadic_args args) const;
188 
196  template<typename... Args>
197  std::string format_string(std::string_view message, Args&&... args) const {
198  return fmt::format(locale_, message, std::forward<Args>(args)...);
199  }
200 
209  std::string localize(std::string_view key, sol::variadic_args args) const;
210 
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};
227 
228  auto iter = find_key_(key);
229  if (iter == map_.end())
230  return std::string{key};
231 
232  return std::visit(
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) {
238  return item;
239  } else {
240  return fmt::format(
241  locale_, fmt::runtime(item), std::forward<Args>(args)...);
242  }
243  } else {
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>();
249  else
250  return std::string{key};
251  } else
252  return std::string{key};
253  }
254  },
255  iter->second);
256  }
257 
264  void register_on_lua(sol::state& lua);
265 
266 private:
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>;
270 
271  std::locale locale_;
272  std::vector<std::string> languages_;
273  std::vector<code_point_range> code_points_;
274  char32_t default_code_point_ = U'\u25a1'; // '□'
275  sol::state lua_;
276  map_type map_;
277 
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_();
281 };
282 
283 } // namespace lxgui::gui
284 
285 #endif
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.