The Battle for Wesnoth  1.19.24+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;
35  std::string sort_name;
36 
37  /** A right to left language? (e.g: Hebrew) */
38  bool rtl = false;
39 
40  /** % of translated text in core po-s */
41  int percent = 100;
42 
43  bool operator==(const language_def&) const;
44  bool operator<(const language_def& a) const
45  {
46  return sort_name < a.sort_name;
47  }
48 
49  std::string short_localename() const;
50 };
51 
53 {
54  /**
55  * Look up the string mappings given in [language] tags. If the key is not
56  * found, fall back to returning a string that's only meant for developers
57  * to see.
58  */
59  const t_string& operator[](const std::string& key) const;
60  const t_string& operator[](const char* key) const;
61  /**
62  * Look up the string mappings given in [language] tags. If the key is not
63  * found, returns symbol_table::end().
64  */
65  utils::string_map::const_iterator find(const std::string& key) const;
66  utils::string_map::const_iterator end() const;
67 };
68 
69 //table of strings which are displayed to the user. Maps ids -> text.
70 inline auto string_table = symbol_table{};
71 
72 bool& time_locale_correct();
73 
74 /**
75  * @param locale_id a posix or windows locale id based on the OS,
76  * like "en_US" or "en-US".
77  * @return name of the translation corresponding to that locale
78  * if available, or an empty string if no such name exists.
79  */
80 std::string get_translation_name(const std::string& locale_id);
81 
82 /**
83  * Return a list of available translations.
84  *
85  * The list will normally be filtered with incomplete (according to
86  * min_translation_percent) translations removed.
87  *
88  *@param all if true, include incomplete translations
89  *@pre load_language_list() has already been called
90  */
91 std::vector<language_def> get_languages(bool all=false);
92 
93 //function which, given the main configuration object, and a locale,
94 //will set string_table to be populated with data from that locale.
95 //locale may be either the full name of the language, like 'English',
96 //or the 2-letter version, like 'en'.
97 void set_language(const language_def& locale);
98 
99 //function which returns the name of the language currently used
100 const language_def& get_language();
101 
102 //function which attempts to query and return the locale on the system
103 const language_def& get_locale();
104 
105 /** Initializes the list of textdomains from a configuration object */
107 
108 /** Initializes certain English strings */
109 bool init_strings(const game_config_view& cfg);
110 
111 bool load_language_list();
112 
114 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:313
void init_textdomains(const game_config_view &cfg)
Initializes the list of textdomains from a configuration object.
Definition: language.cpp:333
bool & time_locale_correct()
Definition: language.cpp:77
int get_min_translation_percent()
Definition: language.cpp:187
const language_def & get_language()
Definition: language.cpp:311
std::vector< language_def > get_languages(bool all=false)
Return a list of available translations.
Definition: language.cpp:162
bool load_language_list()
Definition: language.cpp:141
auto string_table
Definition: language.hpp:70
std::string get_translation_name(const std::string &locale_id)
Definition: language.cpp:181
bool init_strings(const game_config_view &cfg)
Initializes certain English strings.
Definition: language.cpp:351
void set_language(const language_def &locale)
Definition: language.cpp:262
void set_min_translation_percent(int percent)
Definition: language.cpp:192
bool operator==(const language_def &) const
Definition: language.cpp:61
t_string language
Definition: language.hpp:34
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:41
std::string sort_name
Definition: language.hpp:35
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:38
bool operator<(const language_def &a) const
Definition: language.hpp:44
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