1 // locale.cxx -- FlightGear Localization Support
3 // Written by Thorsten Brehm, started April 2012.
5 // Copyright (C) 2012 Thorsten Brehm - brehmt (at) gmail com
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.
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.
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.
29 #include <boost/foreach.hpp>
31 #include <simgear/props/props_io.hxx>
32 #include <simgear/structure/exception.hxx>
34 #include "fg_props.hxx"
40 FGLocale::FGLocale(SGPropertyNode* root) :
41 _intl(root->getNode("/sim/intl",0, true)),
42 _defaultLocale(_intl->getChild("locale",0, true))
52 * Determine locale/language settings on Windows.
54 * Copyright (C) 1997, 2002, 2003 Martin von Loewis
56 * Permission to use, copy, modify, and distribute this software and its
57 * documentation for any purpose and without fee is hereby granted,
58 * provided that the above copyright notice appear in all copies.
60 * This software comes with no warranty. Use at your own risk.
63 FGLocale::getUserLanguage()
66 static char locale[100] = {0};
68 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
69 LOCALE_SISO639LANGNAME,
70 locale, sizeof(locale)))
72 SG_LOG(SG_GENERAL, SG_DEBUG, "Detected locale's language setting: " << locale);
73 size_t i = strlen(locale);
75 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
76 LOCALE_SISO3166CTRYNAME,
77 locale+i, (int)(sizeof(locale)-i)))
79 result.push_back(locale);
84 SG_LOG(SG_GENERAL, SG_WARN, "Failed to detected locale's country setting.");
85 result.push_back(locale);
93 // determine locale / langauge on Mac
94 #include <CoreFoundation/CoreFoundation.h>
97 FGLocale::getUserLanguage()
100 CFArrayRef langs = CFLocaleCopyPreferredLanguages();
103 for (int i=0; i<CFArrayGetCount(langs); ++i) {
104 CFStringRef s = (CFStringRef) CFArrayGetValueAtIndex(langs, i);
105 CFStringGetCString(s, buffer, 64, kCFStringEncodingASCII);
106 result.push_back(buffer);
115 * Determine locale/language settings on Linux/Unix.
118 FGLocale::getUserLanguage()
121 const char* langEnv = ::getenv("LANG");
123 result.push_back(langEnv);
130 // Search property tree for matching locale description
132 FGLocale::findLocaleNode(const string& language)
134 SGPropertyNode* node = NULL;
136 // remove character encoding from the locale spec, i.e. "de_DE.utf8" => "de_DE"
137 size_t pos = language.find(".");
138 if ((pos != string::npos)&&(pos>0))
140 node = findLocaleNode(language.substr(0, pos));
145 SG_LOG(SG_GENERAL, SG_DEBUG, "Searching language resource for locale: " << language);
146 // search locale using full string
147 vector<SGPropertyNode_ptr> localeList = _intl->getChildren("locale");
149 for (size_t i = 0; i < localeList.size(); i++)
151 vector<SGPropertyNode_ptr> langList = localeList[i]->getChildren("lang");
153 for (size_t j = 0; j < langList.size(); j++)
155 if (!language.compare(langList[j]->getStringValue()))
157 SG_LOG(SG_GENERAL, SG_INFO, "Found language resource for: " << language);
158 return localeList[i];
163 // try country's default resource, i.e. "de_DE" => "de"
164 pos = language.find("_");
165 if ((pos != string::npos)&&(pos>0))
167 node = findLocaleNode(language.substr(0, pos));
175 // Select the language. When no language is given (NULL),
176 // a default is determined matching the system locale.
178 FGLocale::selectLanguage(const char *language)
180 string_list languages = getUserLanguage();
181 if (languages.empty()) {
182 // Use plain C locale if nothing is available.
183 SG_LOG(SG_GENERAL, SG_WARN, "Unable to detect system language" );
184 languages.push_back("C");
187 // if we were passed a language option, try it first
188 if ((language != NULL) && (strlen(language) > 0)) {
189 languages.insert(languages.begin(), string(language));
193 SGPropertyNode *locale = NULL;
194 BOOST_FOREACH(string lang, languages) {
195 locale = findLocaleNode(lang);
203 SG_LOG(SG_GENERAL, SG_ALERT,
204 "No internationalization settings specified in preferences.xml" );
208 _currentLocale = locale;
210 // load resource for system messages (translations for fgfs internal messages)
216 // Load strings for requested resource and locale.
217 // Result is stored below "strings" in the property tree of the given locale.
219 FGLocale::loadResource(SGPropertyNode* localeNode, const char* resource)
221 SGPath path( globals->get_fg_root() );
223 SGPropertyNode* stringNode = localeNode->getNode("strings", 0, true);
225 const char *path_str = stringNode->getStringValue(resource, NULL);
228 SG_LOG(SG_GENERAL, SG_WARN, "No path in " << stringNode->getPath() << "/" << resource << ".");
232 path.append(path_str);
233 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings for '" <<
234 localeNode->getStringValue("lang", "<none>")
235 <<"' from " << path.str());
237 // load the actual file
240 readProperties(path.str(), stringNode->getNode(resource, 0, true));
241 } catch (const sg_exception &e)
243 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings from " << path.str() <<
244 ". Error: " << e.getFormattedMessage());
251 // Load strings for requested resource (for current and default locale).
252 // Result is stored below "strings" in the property tree of the locales.
254 FGLocale::loadResource(const char* resource)
256 // load defaults first
257 bool Ok = loadResource(_defaultLocale, resource);
259 // also load language specific resource, unless identical
260 if ((_currentLocale!=0)&&
261 (_defaultLocale != _currentLocale))
263 Ok &= loadResource(_currentLocale, resource);
270 FGLocale::getLocalizedString(SGPropertyNode *localeNode, const char* id, const char* context)
272 SGPropertyNode *n = localeNode->getNode("strings",0, true)->getNode(context);
274 return n->getStringValue(id, NULL);
279 FGLocale::getLocalizedString(const char* id, const char* resource, const char* Default)
283 const char* s = NULL;
285 s = getLocalizedString(_currentLocale, id, resource);
290 s = getLocalizedString(_defaultLocale, id, resource);
297 simgear::PropertyList
298 FGLocale::getLocalizedStrings(SGPropertyNode *localeNode, const char* id, const char* context)
300 SGPropertyNode *n = localeNode->getNode("strings",0, true)->getNode(context);
303 return n->getChildren(id);
305 return simgear::PropertyList();
308 simgear::PropertyList
309 FGLocale::getLocalizedStrings(const char* id, const char* resource)
315 simgear::PropertyList s = getLocalizedStrings(_currentLocale, id, resource);
322 simgear::PropertyList s = getLocalizedStrings(_defaultLocale, id, resource);
327 return simgear::PropertyList();
330 // Check for localized font
332 FGLocale::getDefaultFont(const char* fallbackFont)
334 const char* font = NULL;
337 font = _currentLocale->getStringValue("font", "");
343 font = _defaultLocale->getStringValue("font", "");
351 // Simple UTF8 to Latin1 encoder.
352 void FGLocale::utf8toLatin1(string& s)
356 // map '0xc3..' utf8 characters to Latin1
357 while ((string::npos != (pos = s.find('\xc3',pos)))&&
361 unsigned char p = s[pos+1];
362 if ((p>=0x80)&&(p<0xc0))
367 s.replace(pos, 2, v);
371 #ifdef DEBUG_ENCODING
372 printf("'%s': ", s.c_str());
373 for (pos = 0;pos<s.size();pos++)
374 printf("%02x ", (unsigned char) s[pos]);
378 // hack: also map some Latin2 characters to plain-text ASCII
380 while ((string::npos != (pos = s.find('\xc5',pos)))&&
384 unsigned char p = s[pos+1];
387 case 0x82:c='l';break;
388 case 0x9a:c='S';break;
389 case 0x9b:c='s';break;
390 case 0xba:c='z';break;
391 case 0xbc:c='z';break;
396 s.replace(pos, 2, v);