Debian Codegraph @

src/Main/locale.hxx

raw symbols SCIP
1 // locale.hxx -- FlightGear Localization Support
2 //
3 // Written by Thorsten Brehm, started April 2012.
4 //
5 // Copyright (C) 2012 Thorsten Brehm - brehmt (at) gmail com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20
21 #ifndef __FGLOCALE_HXX
22 #define __FGLOCALE_HXX
23
24 #include <string>
25 #include <cstdarg> // for va_start/_end
26
27 #include <simgear/props/propsfwd.hxx>
28 #include <simgear/misc/strutils.hxx>
29
30 // forward decls
31 class SGPath;
32
33 ///////////////////////////////////////////////////////////////////////////////
34 // FGLocale //////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////////
36
37 class FGLocale
38 {
39 public:
40 FGLocale(SGPropertyNode* root);
41 virtual ~FGLocale();
42
43 /**
44 * Select the locale's primary language. When no language is given
45 * (nullptr), a default is determined matching the system locale.
46 */
47 bool selectLanguage(const std::string& language = {});
48
49 /** Return the preferred language according to user choice and/or settings.
50 *
51 * Examples: 'fr_CA', 'de_DE'... or the empty string if nothing could be
52 * found.
53 *
54 * Note that this is not necessarily the same as the last value passed to
55 * selectLanguage(), assuming it was non-null and non-empty, because the
56 * latter may have an encoding specifier, while values returned by
57 * getPreferredLanguage() never have that.
58 */
59 std::string getPreferredLanguage() const;
60
61 /**
62 * Load strings for requested resource, i.e. "menu", "options", "dialogs".
63 * Loads data for current and default locale (the latter is the fallback).
64 * Result is stored below the "strings" node in the property tree of the
65 * respective locale.
66 */
67 bool loadResource (const char* resource);
68
69 /**
70 * Obtain a single string from the localized resource matching the given identifier.
71 * Selected context refers to "menu", "options", "dialog" etc.
72 */
73 std::string getLocalizedString(const char* id, const char* resource,
74 const char* Default = nullptr);
75
76 std::string getLocalizedString(const std::string& id, const char* resource, const std::string& defaultValue = {});
77
78 /**
79 * Obtain a list of strings from the localized resource matching the given identifier.
80 * Selected context refers to "menu", "options", "dialog" etc.
81 * Returns a list of (string) properties.
82 */
83 simgear::PropertyList getLocalizedStrings(const char* id, const char* resource);
84
85
86 /**
87 * Obtain a single string from the resource matching an identifier and ID.
88 */
89 std::string getLocalizedStringWithIndex(const char* id, const char* resource, unsigned int index) const;
90
91 /**
92 * Return the number of strings matching a resource
93 */
94 size_t getLocalizedStringCount(const char* id, const char* resource) const;
95
96 /**
97 * Obtain default font for current locale.
98 */
99 std::string getDefaultFont (const char* fallbackFont);
100
101 /**
102 * Obtain a message string, from a localized resource ID, and use it as
103 * a printf format string.
104 */
105 std::string localizedPrintf(const char* id, const char* resource, ... );
106
107 std::string vlocalizedPrintf(const char* id, const char* resource, va_list args);
108
109 /**
110 * Simple UTF8 to Latin1 encoder.
111 */
112 static void utf8toLatin1 (std::string& s);
113
114 /**
115 * reset all data in the locale. This is needed to allow the launcher to use the code,
116 without disturbing the main behaviour. Afteer calling this you can do
117 selectLangauge again without problems.
118 */
119 void clear();
120
121 /**
122 @ brief given a node with children corresponding to different language / locale codes,
123 select one based on the user preferred langauge
124 */
125 SGPropertyNode_ptr selectLanguageNode(SGPropertyNode* langs) const;
126
127 protected:
128 /**
129 * Find property node matching given language.
130 */
131 SGPropertyNode* findLocaleNode (const std::string& language);
132
133 /**
134 * Load resource data for given locale node.
135 */
136 bool loadResource (SGPropertyNode* localeNode, const char* resource);
137
138 /**
139 * Obtain a single string from locale node matching the given identifier and context.
140 */
141 std::string innerGetLocalizedString(SGPropertyNode* localeNode, const char* id, const char* context, int index) const;
142
143 /**
144 * Obtain a list of strings from locale node matching the given identifier and context.
145 */
146 simgear::PropertyList getLocalizedStrings(SGPropertyNode *localeNode, const char* id, const char* context);
147
148 /**
149 * Obtain user's default language setting.
150 */
151 string_list getUserLanguages();
152
153 SGPropertyNode_ptr _intl;
154 SGPropertyNode_ptr _currentLocale;
155 SGPropertyNode_ptr _defaultLocale;
156 std::string _currentLocaleString;
157
158 /**
159 * Parse an XLIFF 1.2 file into the standard property structure underneath
160 * the provided locale node
161 */
162 void parseXLIFF(SGPropertyNode* node);
163 private:
164 /** Return a new string with the character encoding part of the locale
165 * spec removed., i.e., "de_DE.UTF-8" becomes "de_DE". If there is no
166 * such part, return a copy of the input string.
167 */
168 static std::string removeEncodingPart(const std::string& locale);
169
170 // this is the ordered list of languages to try. It's the same as
171 // returned by getUserLanguages(), except if the user has used
172 // --language to override, that will be the first item.
173
174 string_list _languages;
175 bool _inited = false;
176 };
177
178 // global translation wrappers
179
180 std::string fgTrMsg(const char* key);
181 std::string fgTrPrintfMsg(const char* key, ...);
182
183
184 #endif // __FGLOCALE_HXX