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 <simgear/props/props_io.hxx>
30 #include <simgear/structure/exception.hxx>
32 #include "fg_props.hxx"
38 FGLocale::FGLocale(SGPropertyNode* root) :
39 _intl(root->getNode("/sim/intl",0, true)),
40 _defaultLocale(_intl->getChild("locale",0, true))
50 * Determine locale/language settings on Windows.
52 * Copyright (C) 1997, 2002, 2003 Martin von Loewis
54 * Permission to use, copy, modify, and distribute this software and its
55 * documentation for any purpose and without fee is hereby granted,
56 * provided that the above copyright notice appear in all copies.
58 * This software comes with no warranty. Use at your own risk.
61 FGLocale::getUserLanguage()
63 static char locale[100] = {0};
65 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
66 LOCALE_SISO639LANGNAME,
67 locale, sizeof(locale)))
69 SG_LOG(SG_GENERAL, SG_DEBUG, "Detected locale's language setting: " << locale);
70 size_t i = strlen(locale);
72 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
73 LOCALE_SISO3166CTRYNAME,
74 locale+i, (int)(sizeof(locale)-i)))
78 SG_LOG(SG_GENERAL, SG_WARN, "Failed to detected locale's country setting.");
86 * Determine locale/language settings on Linux (and Mac?).
89 FGLocale::getUserLanguage()
91 return ::getenv("LANG");
95 // Search property tree for matching locale description
97 FGLocale::findLocaleNode(const string& language)
99 SGPropertyNode* node = NULL;
101 // remove character encoding from the locale spec, i.e. "de_DE.utf8" => "de_DE"
102 size_t pos = language.find(".");
103 if ((pos != string::npos)&&(pos>0))
105 node = findLocaleNode(language.substr(0, pos));
110 SG_LOG(SG_GENERAL, SG_DEBUG, "Searching language resource for locale: " << language);
111 // search locale using full string
112 vector<SGPropertyNode_ptr> localeList = _intl->getChildren("locale");
114 for (size_t i = 0; i < localeList.size(); i++)
116 vector<SGPropertyNode_ptr> langList = localeList[i]->getChildren("lang");
118 for (size_t j = 0; j < langList.size(); j++)
120 if (!language.compare(langList[j]->getStringValue()))
122 SG_LOG(SG_GENERAL, SG_INFO, "Found language resource for: " << language);
123 return localeList[i];
128 // try country's default resource, i.e. "de_DE" => "de"
129 pos = language.find("_");
130 if ((pos != string::npos)&&(pos>0))
132 node = findLocaleNode(language.substr(0, pos));
140 // Select the language. When no language is given (NULL),
141 // a default is determined matching the system locale.
143 FGLocale::selectLanguage(const char *language)
145 // Use system setting when no language is given.
146 if ((language == NULL)||(language[0]==0))
148 language = getUserLanguage();
149 SG_LOG(SG_GENERAL, SG_INFO, "System language: " << ((language) ? language : "<unavailable>"));
152 // Use plain C locale if nothing is available.
153 if ((language == NULL)||(language[0]==0))
155 SG_LOG(SG_GENERAL, SG_WARN, "Unable to detect system language" );
159 SGPropertyNode *locale = findLocaleNode(language);
162 SG_LOG(SG_GENERAL, SG_ALERT,
163 "No internationalization settings specified in preferences.xml" );
167 _currentLocale = locale;
169 // load resource for system messages (translations for fgfs internal messages)
175 // Load strings for requested resource and locale.
176 // Result is stored below "strings" in the property tree of the given locale.
178 FGLocale::loadResource(SGPropertyNode* localeNode, const char* resource)
180 SGPath path( globals->get_fg_root() );
182 SGPropertyNode* stringNode = localeNode->getNode("strings", 0, true);
184 const char *path_str = stringNode->getStringValue(resource, NULL);
187 SG_LOG(SG_GENERAL, SG_WARN, "No path in " << stringNode->getPath() << "/" << resource << ".");
191 path.append(path_str);
192 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings for '" <<
193 localeNode->getStringValue("lang", "<none>")
194 <<"' from " << path.str());
196 // load the actual file
199 readProperties(path.str(), stringNode->getNode(resource, 0, true));
200 } catch (const sg_exception &e)
202 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings from " << path.str() <<
203 ". Error: " << e.getFormattedMessage());
210 // Load strings for requested resource (for current and default locale).
211 // Result is stored below "strings" in the property tree of the locales.
213 FGLocale::loadResource(const char* resource)
215 // load defaults first
216 bool Ok = loadResource(_defaultLocale, resource);
218 // also load language specific resource, unless identical
219 if ((_currentLocale!=0)&&
220 (_defaultLocale != _currentLocale))
222 Ok &= loadResource(_currentLocale, resource);
229 FGLocale::getLocalizedString(SGPropertyNode *localeNode, const char* id, const char* context)
231 SGPropertyNode *n = localeNode->getNode("strings",0, true)->getNode(context);
233 return n->getStringValue(id, NULL);
238 FGLocale::getLocalizedString(const char* id, const char* resource, const char* Default)
242 const char* s = NULL;
244 s = getLocalizedString(_currentLocale, id, resource);
249 s = getLocalizedString(_defaultLocale, id, resource);
256 simgear::PropertyList
257 FGLocale::getLocalizedStrings(SGPropertyNode *localeNode, const char* id, const char* context)
259 SGPropertyNode *n = localeNode->getNode("strings",0, true)->getNode(context);
262 return n->getChildren(id);
264 return simgear::PropertyList();
267 simgear::PropertyList
268 FGLocale::getLocalizedStrings(const char* id, const char* resource)
274 simgear::PropertyList s = getLocalizedStrings(_currentLocale, id, resource);
281 simgear::PropertyList s = getLocalizedStrings(_defaultLocale, id, resource);
286 return simgear::PropertyList();
289 // Check for localized font
291 FGLocale::getDefaultFont(const char* fallbackFont)
293 const char* font = NULL;
296 font = _currentLocale->getStringValue("font", "");
302 font = _defaultLocale->getStringValue("font", "");
310 // Simple UTF8 to Latin1 encoder.
311 void FGLocale::utf8toLatin1(string& s)
315 // map '0xc3..' utf8 characters to Latin1
316 while ((string::npos != (pos = s.find('\xc3',pos)))&&
320 unsigned char p = s[pos+1];
321 if ((p>=0x80)&&(p<0xc0))
326 s.replace(pos, 2, v);
330 #ifdef DEBUG_ENCODING
331 printf("'%s': ", s.c_str());
332 for (pos = 0;pos<s.size();pos++)
333 printf("%02x ", (unsigned char) s[pos]);
337 // hack: also map some Latin2 characters to plain-text ASCII
339 while ((string::npos != (pos = s.find('\xc5',pos)))&&
343 unsigned char p = s[pos+1];
346 case 0x82:c='l';break;
347 case 0x9a:c='S';break;
348 case 0x9b:c='s';break;
349 case 0xba:c='z';break;
350 case 0xbc:c='z';break;
355 s.replace(pos, 2, v);