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.
30 #include <boost/foreach.hpp>
32 #include <simgear/props/props_io.hxx>
33 #include <simgear/structure/exception.hxx>
35 #include "fg_props.hxx"
41 FGLocale::FGLocale(SGPropertyNode* root) :
42 _intl(root->getNode("/sim/intl",0, true)),
43 _defaultLocale(_intl->getChild("locale",0, true))
53 * Determine locale/language settings on Windows.
55 * Copyright (C) 1997, 2002, 2003 Martin von Loewis
57 * Permission to use, copy, modify, and distribute this software and its
58 * documentation for any purpose and without fee is hereby granted,
59 * provided that the above copyright notice appear in all copies.
61 * This software comes with no warranty. Use at your own risk.
64 FGLocale::getUserLanguage()
67 static char locale[100] = {0};
69 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
70 LOCALE_SISO639LANGNAME,
71 locale, sizeof(locale)))
73 SG_LOG(SG_GENERAL, SG_DEBUG, "Detected locale's language setting: " << locale);
74 size_t i = strlen(locale);
76 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
77 LOCALE_SISO3166CTRYNAME,
78 locale+i, (int)(sizeof(locale)-i)))
80 result.push_back(locale);
85 SG_LOG(SG_GENERAL, SG_WARN, "Failed to detected locale's country setting.");
86 result.push_back(locale);
94 // determine locale / langauge on Mac
95 #include <CoreFoundation/CoreFoundation.h>
98 FGLocale::getUserLanguage()
101 CFArrayRef langs = CFLocaleCopyPreferredLanguages();
104 for (int i=0; i<CFArrayGetCount(langs); ++i) {
105 CFStringRef s = (CFStringRef) CFArrayGetValueAtIndex(langs, i);
106 CFStringGetCString(s, buffer, 64, kCFStringEncodingASCII);
107 result.push_back(buffer);
116 * Determine locale/language settings on Linux/Unix.
119 FGLocale::getUserLanguage()
122 const char* langEnv = ::getenv("LANG");
124 result.push_back(langEnv);
131 // Search property tree for matching locale description
133 FGLocale::findLocaleNode(const string& language)
135 SGPropertyNode* node = NULL;
137 // remove character encoding from the locale spec, i.e. "de_DE.utf8" => "de_DE"
138 size_t pos = language.find(".");
139 if ((pos != string::npos)&&(pos>0))
141 node = findLocaleNode(language.substr(0, pos));
146 SG_LOG(SG_GENERAL, SG_DEBUG, "Searching language resource for locale: " << language);
147 // search locale using full string
148 vector<SGPropertyNode_ptr> localeList = _intl->getChildren("locale");
150 for (size_t i = 0; i < localeList.size(); i++)
152 vector<SGPropertyNode_ptr> langList = localeList[i]->getChildren("lang");
154 for (size_t j = 0; j < langList.size(); j++)
156 if (!language.compare(langList[j]->getStringValue()))
158 SG_LOG(SG_GENERAL, SG_INFO, "Found language resource for: " << language);
159 return localeList[i];
164 // try country's default resource, i.e. "de_DE" => "de"
165 pos = language.find("_");
166 if ((pos != string::npos)&&(pos>0))
168 node = findLocaleNode(language.substr(0, pos));
176 // Select the language. When no language is given (NULL),
177 // a default is determined matching the system locale.
179 FGLocale::selectLanguage(const char *language)
181 string_list languages = getUserLanguage();
182 if (languages.empty()) {
183 // Use plain C locale if nothing is available.
184 SG_LOG(SG_GENERAL, SG_WARN, "Unable to detect system language" );
185 languages.push_back("C");
188 // if we were passed a language option, try it first
189 if ((language != NULL) && (strlen(language) > 0)) {
190 languages.insert(languages.begin(), string(language));
194 SGPropertyNode *locale = NULL;
195 BOOST_FOREACH(string lang, languages) {
196 locale = findLocaleNode(lang);
204 SG_LOG(SG_GENERAL, SG_ALERT,
205 "No internationalization settings specified in preferences.xml" );
209 _currentLocale = locale;
211 // load resource for system messages (translations for fgfs internal messages)
217 // Load strings for requested resource and locale.
218 // Result is stored below "strings" in the property tree of the given locale.
220 FGLocale::loadResource(SGPropertyNode* localeNode, const char* resource)
222 SGPath path( globals->get_fg_root() );
224 SGPropertyNode* stringNode = localeNode->getNode("strings", 0, true);
226 const char *path_str = stringNode->getStringValue(resource, NULL);
229 SG_LOG(SG_GENERAL, SG_WARN, "No path in " << stringNode->getPath() << "/" << resource << ".");
233 path.append(path_str);
234 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings for '" <<
235 localeNode->getStringValue("lang", "<none>")
236 <<"' from " << path.str());
238 // load the actual file
241 readProperties(path.str(), stringNode->getNode(resource, 0, true));
242 } catch (const sg_exception &e)
244 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings from " << path.str() <<
245 ". Error: " << e.getFormattedMessage());
252 // Load strings for requested resource (for current and default locale).
253 // Result is stored below "strings" in the property tree of the locales.
255 FGLocale::loadResource(const char* resource)
257 // load defaults first
258 bool Ok = loadResource(_defaultLocale, resource);
260 // also load language specific resource, unless identical
261 if ((_currentLocale!=0)&&
262 (_defaultLocale != _currentLocale))
264 Ok &= loadResource(_currentLocale, resource);
271 FGLocale::getLocalizedString(SGPropertyNode *localeNode, const char* id, const char* context)
273 SGPropertyNode *n = localeNode->getNode("strings",0, true)->getNode(context);
275 return n->getStringValue(id, NULL);
280 FGLocale::getLocalizedString(const char* id, const char* resource, const char* Default)
284 const char* s = NULL;
286 s = getLocalizedString(_currentLocale, id, resource);
291 s = getLocalizedString(_defaultLocale, id, resource);
298 simgear::PropertyList
299 FGLocale::getLocalizedStrings(SGPropertyNode *localeNode, const char* id, const char* context)
301 SGPropertyNode *n = localeNode->getNode("strings",0, true)->getNode(context);
304 return n->getChildren(id);
306 return simgear::PropertyList();
309 simgear::PropertyList
310 FGLocale::getLocalizedStrings(const char* id, const char* resource)
316 simgear::PropertyList s = getLocalizedStrings(_currentLocale, id, resource);
323 simgear::PropertyList s = getLocalizedStrings(_defaultLocale, id, resource);
328 return simgear::PropertyList();
331 // Check for localized font
333 FGLocale::getDefaultFont(const char* fallbackFont)
335 const char* font = NULL;
338 font = _currentLocale->getStringValue("font", "");
344 font = _defaultLocale->getStringValue("font", "");
352 std::string FGLocale::localizedPrintf(const char* id, const char* resource, ... )
355 va_start(args, resource);
356 string r = vlocalizedPrintf(id, resource, args);
361 std::string FGLocale::vlocalizedPrintf(const char* id, const char* resource, va_list args)
363 const char* format = getLocalizedString(id, resource);
364 int len = ::vsprintf(NULL, format, args);
365 char* buf = (char*) alloca(len);
366 ::vsprintf(buf, format, args);
367 return std::string(buf);
370 // Simple UTF8 to Latin1 encoder.
371 void FGLocale::utf8toLatin1(string& s)
375 // map '0xc3..' utf8 characters to Latin1
376 while ((string::npos != (pos = s.find('\xc3',pos)))&&
380 unsigned char p = s[pos+1];
381 if ((p>=0x80)&&(p<0xc0))
386 s.replace(pos, 2, v);
390 #ifdef DEBUG_ENCODING
391 printf("'%s': ", s.c_str());
392 for (pos = 0;pos<s.size();pos++)
393 printf("%02x ", (unsigned char) s[pos]);
397 // hack: also map some Latin2 characters to plain-text ASCII
399 while ((string::npos != (pos = s.find('\xc5',pos)))&&
403 unsigned char p = s[pos+1];
406 case 0x82:c='l';break;
407 case 0x9a:c='S';break;
408 case 0x9b:c='s';break;
409 case 0xba:c='z';break;
410 case 0xbc:c='z';break;
415 s.replace(pos, 2, v);
420 const char* fgTrMsg(const char* key)
422 return globals->get_locale()->getLocalizedString(key, "message");
425 std::string fgTrPrintfMsg(const char* key, ...)
429 string r = globals->get_locale()->vlocalizedPrintf(key, "message", args);