The Battle for Wesnoth  1.19.7+dev
language.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 
22 class game_config_view;
23 
24 //this module controls internationalization.
25 
26 
28 {
30  localename(),
31  alternates(),
32  language(),
33  sort_name(),
34  rtl(false),
35  percent(100)
36  {}
37 
38  language_def(const std::string& name, const t_string& lang, const std::string& dir,
39  const std::string &salternates = "", const std::string& sort_name = "", const std::string& percent = "100") :
40  localename(name),
41  alternates(utils::split(salternates)),
42  language(lang),
43  sort_name(sort_name.empty() ? std::string(lang) : sort_name),
44  rtl(dir == "rtl")
45  {
46  try {
47  this->percent = percent.empty() ? 100 : std::stoi(percent);
48  } catch(const std::invalid_argument&) {
49  this->percent = 100;
50  }
51  }
52 
53  std::string localename;
54  std::vector<std::string> alternates;
56  std::string sort_name;
57  bool rtl; // A right to left language? (e.g: Hebrew)
58  /** % of translated text in core po-s */
59  int percent;
60  bool operator== (const language_def&) const;
61  bool operator< (const language_def& a) const
62  { return sort_name < a.sort_name; }
63 };
64 
65 typedef std::vector<language_def> language_list;
66 
68 {
69  /**
70  * Look up the string mappings given in [language] tags. If the key is not
71  * found, fall back to returning a string that's only meant for developers
72  * to see.
73  */
74  const t_string& operator[](const std::string& key) const;
75  const t_string& operator[](const char* key) const;
76  /**
77  * Look up the string mappings given in [language] tags. If the key is not
78  * found, returns symbol_table::end().
79  */
80  utils::string_map::const_iterator find(const std::string& key) const;
81  utils::string_map::const_iterator end() const;
82 };
83 
84 //table of strings which are displayed to the user. Maps ids -> text.
85 //this table should be consulted whenever something is to be
86 //displayed on screen.
88 
89 bool& time_locale_correct();
90 
91 /**
92  * Return a list of available translations.
93  *
94  * The list will normally be filtered with incomplete (according to
95  * min_translation_percent) translations removed.
96  *
97  *@param all if true, include incomplete translations
98  *@pre load_language_list() has already been called
99  */
100 std::vector<language_def> get_languages(bool all=false);
101 
102 //function which, given the main configuration object, and a locale,
103 //will set string_table to be populated with data from that locale.
104 //locale may be either the full name of the language, like 'English',
105 //or the 2-letter version, like 'en'.
106 void set_language(const language_def& locale);
107 
108 //function which returns the name of the language currently used
109 const language_def& get_language();
110 bool current_language_rtl();
111 
112 //function which attempts to query and return the locale on the system
113 const language_def& get_locale();
114 
115 /** Initializes the list of textdomains from a configuration object */
116 void init_textdomains(const game_config_view& cfg);
117 
118 /** Initializes certain English strings */
119 bool init_strings(const game_config_view& cfg);
120 
121 bool load_language_list();
122 
124 void set_min_translation_percent(int percent);
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:327
bool current_language_rtl()
Definition: language.cpp:54
void init_textdomains(const game_config_view &cfg)
Initializes the list of textdomains from a configuration object.
Definition: language.cpp:365
bool & time_locale_correct()
Definition: language.cpp:66
std::vector< language_def > language_list
Definition: language.hpp:65
int get_min_translation_percent()
Definition: language.cpp:143
const language_def & get_language()
Definition: language.cpp:325
std::vector< language_def > get_languages(bool all=false)
Return a list of available translations.
Definition: language.cpp:126
bool load_language_list()
Definition: language.cpp:103
bool init_strings(const game_config_view &cfg)
Initializes certain English strings.
Definition: language.cpp:383
void set_language(const language_def &locale)
Definition: language.cpp:291
symbol_table string_table
Definition: language.cpp:64
void set_min_translation_percent(int percent)
Definition: language.cpp:148
int stoi(std::string_view str)
Same interface as std::stoi and meant as a drop in replacement, except:
Definition: charconv.hpp:154
std::vector< std::string > split(const config_attribute_value &val)
bool operator==(const language_def &) const
Definition: language.cpp:59
language_def(const std::string &name, const t_string &lang, const std::string &dir, const std::string &salternates="", const std::string &sort_name="", const std::string &percent="100")
Definition: language.hpp:38
t_string language
Definition: language.hpp:55
std::vector< std::string > alternates
Definition: language.hpp:54
int percent
% of translated text in core po-s
Definition: language.hpp:59
std::string sort_name
Definition: language.hpp:56
std::string localename
Definition: language.hpp:53
bool operator<(const language_def &a) const
Definition: language.hpp:61
utils::string_map::const_iterator end() const
Definition: language.cpp:98
const t_string & operator[](const std::string &key) const
Look up the string mappings given in [language] tags.
Definition: language.cpp:72
utils::string_map::const_iterator find(const std::string &key) const
Look up the string mappings given in [language] tags.
Definition: language.cpp:93