The Battle for Wesnoth  1.19.27+dev
language.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2025
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "tstring.hpp"
20 
21 class game_config_view;
22 
23 //this module controls internationalization.
24 
26 {
27  /** Creates the 'System Default Language' definition. */
28  language_def();
29 
30  explicit language_def(const config& cfg);
31 
32  std::string localename;
33  std::vector<std::string> alternates;
34 
35  /**
36  * Windows has no knowledge of most POSIX locale IDs, so on that platform the OS-level
37  * setlocale() call (LC_COLLATE/LC_TIME only) uses these instead. Everything else —
38  * translation catalogue look-up, preferences saving, language_selection matching — always
39  * uses localename/alternates above, so languages with no real Windows locale don't
40  * collide with each other or lose their translations.
41  */
42  std::string windows_locale;
43  std::vector<std::string> windows_alternates;
44 
46  std::string sort_name;
47 
48  /** A right to left language? (e.g: Hebrew) */
49  bool rtl = false;
50 
51  /** % of translated text in core po-s */
52  int percent = 100;
53 
54  bool operator==(const language_def&) const;
55  bool operator<(const language_def& a) const
56  {
57  return sort_name < a.sort_name;
58  }
59 
60  std::string short_localename() const;
61 };
62 
64 {
65  /**
66  * Look up the string mappings given in [language] tags. If the key is not
67  * found, fall back to returning a string that's only meant for developers
68  * to see.
69  */
70  const t_string& operator[](const std::string& key) const;
71  const t_string& operator[](const char* key) const;
72  /**
73  * Look up the string mappings given in [language] tags. If the key is not
74  * found, returns symbol_table::end().
75  */
76  utils::string_map::const_iterator find(const std::string& key) const;
77  utils::string_map::const_iterator end() const;
78 };
79 
80 //table of strings which are displayed to the user. Maps ids -> text.
81 inline auto string_table = symbol_table{};
82 
83 bool& time_locale_correct();
84 
85 /**
86  * @param locale_id a posix or windows locale id based on the OS,
87  * like "en_US" or "en-US".
88  * @return name of the translation corresponding to that locale
89  * if available, or an empty string if no such name exists.
90  */
91 std::string get_translation_name(const std::string& locale_id);
92 
93 /**
94  * Return a list of available translations.
95  *
96  * The list will normally be filtered with incomplete (according to
97  * min_translation_percent) translations removed.
98  *
99  *@param all if true, include incomplete translations
100  *@pre load_language_list() has already been called
101  */
102 std::vector<language_def> get_languages(bool all=false);
103 
104 //function which, given the main configuration object, and a locale,
105 //will set string_table to be populated with data from that locale.
106 //locale may be either the full name of the language, like 'English',
107 //or the 2-letter version, like 'en'.
108 void set_language(const language_def& locale);
109 
110 //function which returns the name of the language currently used
111 const language_def& get_language();
112 
113 //function which attempts to query and return the locale on the system
114 const language_def& get_locale();
115 
116 /** Initializes the list of textdomains from a configuration object */
118 
119 /** Initializes certain English strings */
120 bool init_strings(const game_config_view& cfg);
121 
122 bool load_language_list();
123 
125 void set_min_translation_percent(int percent);
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:157
A class grating read only view to a vector of config objects, viewed as one config with all children ...
const config * cfg
const language_def & get_locale()
Definition: language.cpp:319
void init_textdomains(const game_config_view &cfg)
Initializes the list of textdomains from a configuration object.
Definition: language.cpp:339
bool & time_locale_correct()
Definition: language.cpp:77
int get_min_translation_percent()
Definition: language.cpp:184
const language_def & get_language()
Definition: language.cpp:317
std::vector< language_def > get_languages(bool all=false)
Return a list of available translations.
Definition: language.cpp:159
bool load_language_list()
Definition: language.cpp:138
auto string_table
Definition: language.hpp:81
std::string get_translation_name(const std::string &locale_id)
Definition: language.cpp:178
bool init_strings(const game_config_view &cfg)
Initializes certain English strings.
Definition: language.cpp:357
void set_language(const language_def &locale)
Definition: language.cpp:259
void set_min_translation_percent(int percent)
Definition: language.cpp:189
std::vector< std::string > windows_alternates
Definition: language.hpp:43
bool operator==(const language_def &) const
Definition: language.cpp:61
std::string windows_locale
Windows has no knowledge of most POSIX locale IDs, so on that platform the OS-level setlocale() call ...
Definition: language.hpp:42
t_string language
Definition: language.hpp:45
std::string short_localename() const
Definition: language.cpp:66
std::vector< std::string > alternates
Definition: language.hpp:33
int percent
% of translated text in core po-s
Definition: language.hpp:52
std::string sort_name
Definition: language.hpp:46
std::string localename
Definition: language.hpp:32
language_def()
Creates the 'System Default Language' definition.
Definition: language.cpp:114
bool rtl
A right to left language? (e.g: Hebrew)
Definition: language.hpp:49
bool operator<(const language_def &a) const
Definition: language.hpp:55
utils::string_map::const_iterator end() const
Definition: language.cpp:109
const t_string & operator[](const std::string &key) const
Look up the string mappings given in [language] tags.
Definition: language.cpp:83
utils::string_map::const_iterator find(const std::string &key) const
Look up the string mappings given in [language] tags.
Definition: language.cpp:104