1
0
Fork 0

Make the Unix impl. of FGLocale::getUserLanguage() consistent with the others

Windows and Mac implementations return a string without any encoding
specifier -> remove this specifier directly in the Unix/Linux
implementation for consistency.

Also do some small refactoring with the new static method
FGLocale::removeEncodingPart(). Slight difference with the previous
algorithm: if a '.' is found in the given locale spec, we assert() that
it is not the first character. The previous code in
FGLocale::findLocaleNode() used to consider such weird locale specs
starting with a dot as normal locale specs without any encoding part.

Note: the same change could be done where FGLocale::findLocaleNode()
      looks for an underscore in order to prepare for the fallback
      search (e.g., 'fr' after not finding translations for 'fr_FR').
This commit is contained in:
Florent Rougon 2017-04-26 17:55:44 +02:00
parent ed9f985f92
commit 1bf3aee5df
2 changed files with 41 additions and 15 deletions

View file

@ -27,6 +27,9 @@
#endif
#include <cstdio>
#include <cstddef> // std::size_t
#include <cassert>
#include <boost/foreach.hpp>
#include <simgear/props/props_io.hxx>
@ -48,6 +51,23 @@ FGLocale::~FGLocale()
{
}
// Static method
string FGLocale::removeEncodingPart(const string& locale)
{
string res;
std::size_t pos = locale.find('.');
if (pos != string::npos)
{
assert(pos > 0);
res = locale.substr(0, pos);
} else {
res = locale;
}
return res;
}
#ifdef _WIN32
@ -81,30 +101,29 @@ FGLocale::getUserLanguage()
{
string_list result;
const char* langEnv = ::getenv("LANG");
if (langEnv) {
result.push_back(langEnv);
// Remove character encoding from the locale spec, i.e. "de_DE.UTF-8"
// becomes "de_DE". This is for consistency with the Windows and MacOS
// implementations of this method.
result.push_back(removeEncodingPart(langEnv));
}
return result;
}
#endif
// Search property tree for matching locale description
SGPropertyNode*
FGLocale::findLocaleNode(const string& language)
FGLocale::findLocaleNode(const string& localeSpec)
{
SGPropertyNode* node = NULL;
// Remove the character encoding part of the locale spec, i.e.,
// "de_DE.utf8" => "de_DE"
string language = removeEncodingPart(localeSpec);
// remove character encoding from the locale spec, i.e. "de_DE.utf8" => "de_DE"
size_t pos = language.find(".");
if ((pos != string::npos)&&(pos>0))
{
node = findLocaleNode(language.substr(0, pos));
if (node)
return node;
}
SG_LOG(SG_GENERAL, SG_DEBUG, "Searching language resource for locale: " << language);
SG_LOG(SG_GENERAL, SG_DEBUG,
"Searching language resource for locale: '" << language << "'");
// search locale using full string
vector<SGPropertyNode_ptr> localeList = _intl->getChildren("locale");
@ -123,8 +142,8 @@ FGLocale::findLocaleNode(const string& language)
}
// try country's default resource, i.e. "de_DE" => "de"
pos = language.find("_");
if ((pos != string::npos)&&(pos>0))
std::size_t pos = language.find('_');
if ((pos != string::npos) && (pos > 0))
{
node = findLocaleNode(language.substr(0, pos));
if (node)

View file

@ -123,6 +123,13 @@ protected:
SGPropertyNode_ptr _intl;
SGPropertyNode_ptr _currentLocale;
SGPropertyNode_ptr _defaultLocale;
private:
/** Return a new string with the character encoding part of the locale
* spec removed., i.e., "de_DE.UTF-8" becomes "de_DE". If there is no
* such part, return a copy of the input string.
*/
static std::string removeEncodingPart(const std::string& locale);
};
// global translation wrappers