Made a patch which tries .utf8 locales when setting the locale

(i.e. de_DE.utf8 in addition to de_DE) and also added an "alternates"
WML tag to [locale], which works like e.g. "alternates=de_LI, de_LU,
de_CH, de_AT" for de_DE.
This commit is contained in:
Elias Pschernig 2007-04-09 22:15:43 +00:00
parent 221c63690e
commit 28fe8144f5
3 changed files with 34 additions and 9 deletions

View file

@ -1,4 +1,5 @@
[locale]
name="Deutsch"
locale=de_DE
alternates=de_LI, de_LU, de_CH, de_AT
[/locale]

View file

@ -95,7 +95,8 @@ bool load_language_list()
config::const_child_itors langs = cfg.child_range("locale");
for(;langs.first != langs.second; ++langs.first) {
known_languages.push_back(
language_def((**langs.first)["locale"], (**langs.first)["name"], (**langs.first)["dir"]));
language_def((**langs.first)["locale"], (**langs.first)["name"], (**langs.first)["dir"],
(**langs.first)["alternates"]));
}
return true;
@ -106,7 +107,8 @@ std::vector<language_def> get_languages()
return known_languages;
}
static void wesnoth_setlocale(int category, std::string const &slocale)
static void wesnoth_setlocale(int category, std::string const &slocale,
std::vector<std::string> const *alternates)
{
char const *locale = slocale.c_str();
// FIXME: ideally we should check LANGUAGE and on first invocation
@ -161,12 +163,29 @@ static void wesnoth_setlocale(int category, std::string const &slocale)
locale = xlocale.c_str();
}
#endif
char* res = setlocale (category, locale);
char *res = NULL;
char const *try_loc = locale;
std::vector<std::string>::const_iterator i;
if (alternates) i = alternates->begin();
while (true) {
res = setlocale(category, try_loc);
if (res) break;
std::string utf8 = std::string(try_loc) + std::string(".utf8");
try_loc = utf8.c_str();
res = setlocale(category, try_loc);
if (res) break;
if (!alternates) break;
if (i == alternates->end()) break;
try_loc = i->c_str();
i++;
}
if (res == NULL)
std::cerr << "WARNING: setlocale() failed for "
<< locale << ".\n";
else
std::cerr << "set locale to " << locale << "\n";
std::cerr << "set locale to " << try_loc << "\n";
}
bool set_language(const language_def& locale)
@ -180,8 +199,8 @@ bool set_language(const language_def& locale)
config cfg;
current_language = locale;
wesnoth_setlocale(LC_MESSAGES, locale.localename);
wesnoth_setlocale(LC_COLLATE, locale.localename);
wesnoth_setlocale(LC_MESSAGES, locale.localename, &locale.alternates);
wesnoth_setlocale(LC_COLLATE, locale.localename, &locale.alternates);
// fill string_table (should be moved somwhere else some day)
try {
@ -223,7 +242,7 @@ const language_def& get_locale()
const std::string& prefs_locale = preferences::language();
if(prefs_locale.empty() == false) {
wesnoth_setlocale(LC_MESSAGES, prefs_locale);
wesnoth_setlocale(LC_MESSAGES, prefs_locale, NULL);
for(std::vector<language_def>::const_iterator i = known_languages.begin();
i != known_languages.end(); ++i) {
if (prefs_locale == i->localename)

View file

@ -14,6 +14,7 @@
#define LANGUAGE_HPP_INCLUDED
#include "tstring.hpp"
#include "serialization/string_utils.hpp"
#include <map>
#include <string>
@ -28,10 +29,14 @@ class config;
struct language_def
{
language_def() {}
language_def(const std::string& name, const t_string& lang, const std::string& dir) :
language_def(const std::string& name, const t_string& lang, const std::string& dir,
const std::string &salternates = "") :
localename(name), language(lang), rtl(dir == "rtl")
{}
{
alternates = utils::split(salternates);
}
std::string localename;
std::vector<std::string> alternates;
t_string language;
bool rtl; // A right to left language? (e.g: Hebrew)
bool operator== (const language_def&) const;