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.
25 #include <simgear/props/props_io.hxx>
26 #include <simgear/structure/exception.hxx>
28 #include "fg_props.hxx"
34 FGLocale::FGLocale(SGPropertyNode* root) :
35 _intl(root->getNode("/sim/intl",0, true)),
36 _defaultLocale(_intl->getChild("locale",0, true))
46 * Determine locale/language settings on Windows.
48 * Copyright (C) 1997, 2002, 2003 Martin von Loewis
50 * Permission to use, copy, modify, and distribute this software and its
51 * documentation for any purpose and without fee is hereby granted,
52 * provided that the above copyright notice appear in all copies.
54 * This software comes with no warranty. Use at your own risk.
57 FGLocale::getUserLanguage()
59 static char locale[100] = {0};
61 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
62 LOCALE_SISO639LANGNAME,
63 locale, sizeof(locale)))
65 size_t i = strlen(locale);
67 if (GetLocaleInfo(LOCALE_USER_DEFAULT,
68 LOCALE_SISO3166CTRYNAME,
69 locale+i, (int)(sizeof(locale)-i)))
77 * Determine locale/language settings on Linux (and Mac?).
80 FGLocale::getUserLanguage()
82 return ::getenv("LANG");
86 // Search property tree for matching locale description
88 FGLocale::findLocaleNode(const string& language)
90 SG_LOG(SG_GENERAL, SG_INFO, "Searching language resource for locale: " << language);
92 SGPropertyNode* node = NULL;
94 // remove character encoding from the locale spec, i.e. "de_DE.utf8" => "de_DE"
95 size_t pos = language.find(".");
96 if ((pos != string::npos)&&(pos>0))
98 node = findLocaleNode(language.substr(0, pos));
103 // try country's default resource, i.e. "de_DE" => "de"
104 pos = language.find("_");
105 if ((pos != string::npos)&&(pos>0))
107 node = findLocaleNode(language.substr(0, pos));
112 // search locale using full string
113 vector<SGPropertyNode_ptr> localeList = _intl->getChildren("locale");
115 for (size_t i = 0; i < localeList.size(); i++)
117 vector<SGPropertyNode_ptr> langList = localeList[i]->getChildren("lang");
119 for (size_t j = 0; j < langList.size(); j++)
121 if (!language.compare(langList[j]->getStringValue()))
122 return localeList[i];
129 // Select the language. When no language is given (NULL),
130 // a default is determined matching the system locale.
132 FGLocale::selectLanguage(const char *language)
134 // Use system setting when no language is given.
135 if ((language == NULL)||(language[0]==0))
136 language = getUserLanguage();
138 // Use plain C locale if nothing is available.
139 if ((language == NULL)||(language[0]==0))
141 SG_LOG(SG_GENERAL, SG_INFO, "Unable to detect the language" );
145 SGPropertyNode *locale = findLocaleNode(language);
148 SG_LOG(SG_GENERAL, SG_ALERT,
149 "No internationalization settings specified in preferences.xml" );
153 _currentLocale = locale;
157 // Load strings for requested resource and locale.
158 // Result is stored below "strings" in the property tree of the given locale.
160 FGLocale::loadResource(SGPropertyNode* localeNode, const char* resource)
162 SGPath path( globals->get_fg_root() );
164 SGPropertyNode* stringNode = localeNode->getNode("strings", 0, true);
166 const char *path_str = stringNode->getStringValue(resource, NULL);
169 SG_LOG(SG_GENERAL, SG_WARN, "No path in " << stringNode->getPath() << "/" << resource << ".");
173 path.append(path_str);
174 SG_LOG(SG_GENERAL, SG_INFO, "Reading localized strings for '" <<
175 localeNode->getStringValue("lang", "<none>")
176 <<"' from " << path.str());
178 // load the actual file
181 readProperties(path.str(), stringNode->getNode(resource, 0, true));
182 } catch (const sg_exception &e)
184 SG_LOG(SG_GENERAL, SG_ALERT, "Unable to read the localized strings from " << path.str() <<
185 ". Error: " << e.getFormattedMessage());
192 // Load strings for requested resource (for current and default locale).
193 // Result is stored below "strings" in the property tree of the locales.
195 FGLocale::loadResource(const char* resource)
197 // load defaults first
198 bool Ok = loadResource(_defaultLocale, resource);
200 // also load language specific resource, unless identical
201 if ((_currentLocale!=0)&&
202 (_defaultLocale != _currentLocale))
204 Ok &= loadResource(_currentLocale, resource);
211 FGLocale::getLocalizedString(SGPropertyNode *localeNode, const char* id, const char* context)
213 SGPropertyNode *n = localeNode->getNode("strings",0, true)->getNode(context);
215 return n->getStringValue(id, NULL);
220 FGLocale::getLocalizedString(const char* id, const char* resource, const char* Default)
224 const char* s = NULL;
226 s = getLocalizedString(_currentLocale, id, resource);
231 s = getLocalizedString(_defaultLocale, id, resource);
238 simgear::PropertyList
239 FGLocale::getLocalizedStrings(SGPropertyNode *localeNode, const char* id, const char* context)
241 SGPropertyNode *n = localeNode->getNode("strings",0, true)->getNode(context);
244 return n->getChildren(id);
246 return simgear::PropertyList();
249 simgear::PropertyList
250 FGLocale::getLocalizedStrings(const char* id, const char* resource)
256 simgear::PropertyList s = getLocalizedStrings(_currentLocale, id, resource);
263 simgear::PropertyList s = getLocalizedStrings(_defaultLocale, id, resource);
268 return simgear::PropertyList();
271 // Check for localized font
273 FGLocale::getDefaultFont(const char* fallbackFont)
275 const char* font = NULL;
278 font = _currentLocale->getStringValue("font", "");
284 font = _defaultLocale->getStringValue("font", "");
292 // Simple UTF8 to Latin1 encoder.
293 void FGLocale::utf8toLatin1(string& s)
297 // map '0xc3..' utf8 characters to Latin1
298 while ((string::npos != (pos = s.find('\xc3',pos)))&&
302 unsigned char p = s[pos+1];
303 if ((p>=0x80)&&(p<0xc0))
308 s.replace(pos, 2, v);
312 #ifdef DEBUG_ENCODING
313 printf("'%s': ", s.c_str());
314 for (pos = 0;pos<s.size();pos++)
315 printf("%02x ", (unsigned char) s[pos]);
319 // hack: also map some Latin2 characters to plain-text ASCII
321 while ((string::npos != (pos = s.find('\xc5',pos)))&&
325 unsigned char p = s[pos+1];
328 case 0x82:c='l';break;
329 case 0x9a:c='S';break;
330 case 0x9b:c='s';break;
331 case 0xba:c='z';break;
332 case 0xbc:c='z';break;
337 s.replace(pos, 2, v);