use boost locale gettext
This includuces an alternative implementation of gettext.cpp. Libintl gettext doesn't seem to support utf8 filenames on windows. Also it causes problems when changign the language on my msvc build. That why i want to use boost locale gettext at least for our windows builds. This requires boost locale which is introduced in boost 1.48 (That's why i still hesitate to make it the default on all systems).
This commit is contained in:
parent
9742c9cb54
commit
c4e2c4bf51
4 changed files with 122 additions and 1 deletions
|
@ -100,4 +100,8 @@ void set_default_textdomain(const char* domain)
|
|||
textdomain(domain);
|
||||
}
|
||||
|
||||
void set_language(const char* /*language*/)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -64,6 +64,8 @@ namespace translation
|
|||
|
||||
void bind_textdomain(const char* domain, const char* direcory, const char* encoding);
|
||||
void set_default_textdomain(const char* domain);
|
||||
|
||||
void set_language(const char* language);
|
||||
}
|
||||
|
||||
//#define _(String) translation::dsgettext(GETTEXT_DOMAIN,String)
|
||||
|
|
115
src/gettext_boost.cpp
Normal file
115
src/gettext_boost.cpp
Normal file
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
|
||||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#include "global.hpp"
|
||||
#include "gettext.hpp"
|
||||
#include <iostream>
|
||||
#include <locale>
|
||||
#include <boost/locale.hpp>
|
||||
#include <set>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct translation_manager
|
||||
{
|
||||
translation_manager()
|
||||
: loaded_paths_()
|
||||
, loaded_domains_()
|
||||
, current_language_()
|
||||
, generator_()
|
||||
, current_locale_()
|
||||
{
|
||||
generator_.use_ansi_encoding(false);
|
||||
update_locale();
|
||||
}
|
||||
void update_locale() { current_locale_ = generator_.generate(current_language_); }
|
||||
|
||||
std::set<std::string> loaded_paths_;
|
||||
std::set<std::string> loaded_domains_;
|
||||
std::string current_language_;
|
||||
boost::locale::generator generator_;
|
||||
std::locale current_locale_;
|
||||
};
|
||||
|
||||
translation_manager& get_manager()
|
||||
{
|
||||
static translation_manager* mng = new translation_manager();
|
||||
return *mng;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace translation
|
||||
{
|
||||
|
||||
std::string dgettext(const char* domain, const char* msgid)
|
||||
{
|
||||
return boost::locale::dgettext(domain, msgid, get_manager().current_locale_);
|
||||
}
|
||||
std::string egettext(char const *msgid)
|
||||
{
|
||||
return msgid[0] == '\0' ? msgid : boost::locale::gettext(msgid, get_manager().current_locale_);
|
||||
}
|
||||
|
||||
std::string dsgettext (const char * domainname, const char *msgid)
|
||||
{
|
||||
std::string msgval = dgettext (domainname, msgid);
|
||||
if (msgval == msgid) {
|
||||
const char* firsthat = std::strrchr (msgid, '^');
|
||||
if (firsthat == NULL)
|
||||
msgval = msgid;
|
||||
else
|
||||
msgval = firsthat + 1;
|
||||
}
|
||||
return msgval;
|
||||
}
|
||||
|
||||
std::string dsngettext (const char * domainname, const char *singular, const char *plural, int n)
|
||||
{
|
||||
std::string msgval = boost::locale::dngettext(domainname, singular, plural, n, get_manager().current_locale_);
|
||||
if (msgval == singular) {
|
||||
const char* firsthat = std::strrchr (singular, '^');
|
||||
if (firsthat == NULL)
|
||||
msgval = singular;
|
||||
else
|
||||
msgval = firsthat + 1;
|
||||
}
|
||||
return msgval;
|
||||
}
|
||||
|
||||
void bind_textdomain(const char* domain, const char* direcory, const char* encoding)
|
||||
{
|
||||
std::cerr << "adding textdomain '" << domain << "' in directory '" << direcory << "'\n";
|
||||
get_manager().generator_.add_messages_domain(domain);
|
||||
get_manager().generator_.add_messages_path(direcory);
|
||||
get_manager().update_locale();
|
||||
}
|
||||
|
||||
void set_default_textdomain(const char* domain)
|
||||
{
|
||||
get_manager().generator_.set_default_messages_domain(domain);
|
||||
get_manager().update_locale();
|
||||
}
|
||||
|
||||
|
||||
void set_language(const char* language)
|
||||
{
|
||||
|
||||
std::cerr << "setting language to '" << language << "' \n";
|
||||
get_manager().current_language_ = language;
|
||||
get_manager().current_language_ += ".UTF-8";
|
||||
get_manager().update_locale();
|
||||
}
|
||||
|
||||
}
|
|
@ -221,7 +221,7 @@ void set_language(const language_def& locale)
|
|||
wesnoth_setlocale(LC_COLLATE, locale.localename, &locale.alternates);
|
||||
wesnoth_setlocale(LC_TIME, locale.localename, &locale.alternates);
|
||||
wesnoth_setlocale(LC_MESSAGES, locale.localename, &locale.alternates);
|
||||
|
||||
translation::set_language(locale.localename.c_str());
|
||||
load_strings(false);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue