The Battle for Wesnoth  1.19.10+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 
51 {
52  /**
53  * Look up the string mappings given in [language] tags. If the key is not
54  * found, fall back to returning a string that's only meant for developers
55  * to see.
56  */
57  const t_string& operator[](const std::string& key) const;
58  const t_string& operator[](const char* key) const;
59  /**
60  * Look up the string mappings given in [language] tags. If the key is not
61  * found, returns symbol_table::end().
62  */
63  utils::string_map::const_iterator find(const std::string& key) const;
64  utils::string_map::const_iterator end() const;
65 };
66 
67 //table of strings which are displayed to the user. Maps ids -> text.
68 inline auto string_table = symbol_table{};
69 
70 bool& time_locale_correct();
71 
72 /**
73  * Return a list of available translations.
74  *
75  * The list will normally be filtered with incomplete (according to
76  * min_translation_percent) translations removed.
77  *
78  *@param all if true, include incomplete translations
79  *@pre load_language_list() has already been called
80  */
81 std::vector<language_def> get_languages(bool all=false);
82 
83 //function which, given the main configuration object, and a locale,
84 //will set string_table to be populated with data from that locale.
85 //locale may be either the full name of the language, like 'English',
86 //or the 2-letter version, like 'en'.
87 void set_language(const language_def& locale);
88 
89 //function which returns the name of the language currently used
90 const language_def& get_language();
91 
92 //function which attempts to query and return the locale on the system
93 const language_def& get_locale();
94 
95 /** Initializes the list of textdomains from a configuration object */
96 void init_textdomains(const game_config_view& cfg);
97 
98 /** Initializes certain English strings */
99 bool init_strings(const game_config_view& cfg);
100 
101 bool load_language_list();
102 
104 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:158
A class grating read only view to a vector of config objects, viewed as one config with all children ...
const language_def & get_locale()
Definition: language.cpp:329
void init_textdomains(const game_config_view &cfg)
Initializes the list of textdomains from a configuration object.
Definition: language.cpp:367
bool & time_locale_correct()
Definition: language.cpp:58
int get_min_translation_percent()
Definition: language.cpp:147
const language_def & get_language()
Definition: language.cpp:327
std::vector< language_def > get_languages(bool all=false)
Return a list of available translations.
Definition: language.cpp:130
bool load_language_list()
Definition: language.cpp:111
auto string_table
Definition: language.hpp:68
bool init_strings(const game_config_view &cfg)
Initializes certain English strings.
Definition: language.cpp:385
void set_language(const language_def &locale)
Definition: language.cpp:295
void set_min_translation_percent(int percent)
Definition: language.cpp:152
bool operator==(const language_def &) const
Definition: language.cpp:53
t_string language
Definition: language.hpp:34
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:95
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:90
const t_string & operator[](const std::string &key) const
Look up the string mappings given in [language] tags.
Definition: language.cpp:64
utils::string_map::const_iterator find(const std::string &key) const
Look up the string mappings given in [language] tags.
Definition: language.cpp:85