The Battle for Wesnoth  1.17.0-dev
language.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 #include <iterator>
22 
23 class game_config_view;
24 
25 //this module controls internationalization.
26 
27 class config;
28 
30 {
32  localename(),
33  alternates(),
34  language(),
35  sort_name(),
36  rtl(false),
37  percent(100)
38  {}
39 
40  language_def(const std::string& name, const t_string& lang, const std::string& dir,
41  const std::string &salternates = "", const std::string& sort_name = "", const std::string& percent = "100") :
42  localename(name),
43  alternates(utils::split(salternates)),
44  language(lang),
45  sort_name(sort_name.empty() ? std::string(lang) : sort_name),
46  rtl(dir == "rtl")
47  {
48  try {
49  this->percent = percent.empty() ? 100 : std::stoi(percent);
50  } catch(const std::invalid_argument&) {
51  this->percent = 100;
52  }
53  }
54 
55  std::string localename;
56  std::vector<std::string> alternates;
58  std::string sort_name;
59  bool rtl; // A right to left language? (e.g: Hebrew)
60  /** % of translated text in core po-s */
61  int percent;
62  bool operator== (const language_def&) const;
63  bool operator< (const language_def& a) const
64  { return sort_name < a.sort_name; }
65 };
66 
67 typedef std::vector<language_def> language_list;
68 
70 {
71  /**
72  * Look up the string mappings given in [language] tags. If the key is not
73  * found, fall back to returning a string that's only meant for developers
74  * to see.
75  */
76  const t_string& operator[](const std::string& key) const;
77  const t_string& operator[](const char* key) const;
78  /**
79  * Look up the string mappings given in [language] tags. If the key is not
80  * found, returns symbol_table::end().
81  */
82  utils::string_map::const_iterator find(const std::string& key) const;
83  utils::string_map::const_iterator end() const;
84 };
85 
86 //table of strings which are displayed to the user. Maps ids -> text.
87 //this table should be consulted whenever something is to be
88 //displayed on screen.
90 
91 bool& time_locale_correct();
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 bool current_language_rtl();
113 
114 //function which attempts to query and return the locale on the system
115 const language_def& get_locale();
116 
117 /** Initializes the list of textdomains from a configuration object */
118 void init_textdomains(const game_config_view& cfg);
119 
120 /** Initializes certain English strings */
121 bool init_strings(const game_config_view& cfg);
122 
123 bool load_language_list();
124 
#define a
std::vector< language_def > get_languages(bool all=false)
Return a list of available translations.
Definition: language.cpp:127
bool init_strings(const game_config_view &cfg)
Initializes certain English strings.
Definition: language.cpp:390
STL namespace.
bool operator==(const language_def &) const
Definition: language.cpp:60
bool operator<(const language_def &a) const
Definition: language.hpp:63
std::string localename
Definition: language.hpp:55
bool load_language_list()
Definition: language.cpp:104
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:40
std::vector< language_def > language_list
Definition: language.hpp:67
void set_language(const language_def &locale)
Definition: language.cpp:287
void set_min_translation_percent(int percent)
Definition: language.cpp:144
const language_def & get_language()
Definition: language.cpp:327
t_string language
Definition: language.hpp:57
int percent
% of translated text in core po-s
Definition: language.hpp:61
symbol_table string_table
Definition: language.cpp:65
std::vector< std::string > split(const config_attribute_value &val)
const language_def & get_locale()
Definition: language.cpp:329
std::string sort_name
Definition: language.hpp:58
void init_textdomains(const game_config_view &cfg)
Initializes the list of textdomains from a configuration object.
Definition: language.cpp:367
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
bool current_language_rtl()
Definition: language.cpp:55
bool & time_locale_correct()
Definition: language.cpp:67
std::vector< std::string > alternates
Definition: language.hpp:56