Skip to content

Commit

Permalink
fix compilation
Browse files Browse the repository at this point in the history
it seems like sometimes libintl.h is needed for LC_MESSAGES so we need
to move that last code that uses LV_MESSAGES out of language.cpp into
gettext.cpp
  • Loading branch information
gfgtdf committed Oct 25, 2014
1 parent 2fee9e2 commit 9c799b9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 25 deletions.
76 changes: 72 additions & 4 deletions src/gettext.cpp
Expand Up @@ -12,13 +12,22 @@
See the COPYING file for more details.
*/

#include <libintl.h>
#include "global.hpp"

#include "gettext.hpp"
#include "log.hpp"
#include <stdlib.h>


#include <libintl.h>
#include <cstring>

#ifdef _WIN32
#include <windows.h>
#endif

#define DBG_G LOG_STREAM(debug, lg::general)
#define LOG_G LOG_STREAM(info, lg::general)
#define WRN_G LOG_STREAM(warn, lg::general)
#define ERR_G LOG_STREAM(err, lg::general)
namespace translation
{
std::string dgettext(const char* domain, const char* msgid)
Expand Down Expand Up @@ -100,8 +109,67 @@ void set_default_textdomain(const char* domain)
textdomain(domain);
}

void set_language(const char* /*language*/)
void set_language(const std::string& slocale, const std::vector<std::string>* alternates)
{

//Code copied from language.cpp::wesnoth_setlocale()
std::string locale = slocale;
// FIXME: ideally we should check LANGUAGE and on first invocation
// use that value, so someone with es would get the game in Spanish
// instead of en_US the first time round
// LANGUAGE overrides other settings, so for now just get rid of it

#ifdef _WIN32
(void)alternates;
std::string win_locale(locale, 0, 2);
#include "language_win32.ii"
SetEnvironmentVariableA("LANG", win_locale.c_str());
std::string env = "LANGUAGE=" + locale;
_putenv(env.c_str());
return;
#else
// FIXME: add configure check for unsetenv
unsetenv ("LANGUAGE"); // void so no return value to check
#ifdef __APPLE__
if (setenv("LANG", locale.c_str(), 1) == -1) {
ERR_G << "setenv LANG failed: " << strerror(errno);
}
#endif

char *res = NULL;
std::vector<std::string>::const_iterator i;
if (alternates) i = alternates->begin();

for (;;)
{
std::string lang = locale, extra;
std::string::size_type pos = locale.find('@');
if (pos != std::string::npos) {
lang.erase(pos);
extra = locale.substr(pos);
}

/*
* The "" is the last item to work-around a problem in glibc picking
* the non utf8 locale instead an utf8 version if available.
*/
char const *encoding[] = { ".utf-8", ".UTF-8", "" };
for (int j = 0; j != 3; ++j)
{
locale = lang + encoding[j] + extra;
res = std::setlocale(LC_MESSAGES, locale.c_str());
if (res) {
LOG_G << "Set locale to '" << locale << "' result: '" << res << "'.\n";
return;
}
}

if (!alternates || i == alternates->end()) break;
locale = *i;
++i;
}
WRN_G << "setlocale() failed for '" << slocale << "'." << std::endl;
#endif //win32
}

}
3 changes: 2 additions & 1 deletion src/gettext.hpp
Expand Up @@ -36,6 +36,7 @@
// gettext-related declarations
#include "wesconfig.h"
#include <string>
#include <vector>

#ifndef GETTEXT_DOMAIN
# define GETTEXT_DOMAIN PACKAGE
Expand Down Expand Up @@ -71,7 +72,7 @@ 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);
void set_language(const std::string& language, const std::vector<std::string>* alternates);
}

//#define _(String) translation::dsgettext(GETTEXT_DOMAIN,String)
Expand Down
5 changes: 3 additions & 2 deletions src/gettext_boost.cpp
Expand Up @@ -103,9 +103,10 @@ void set_default_textdomain(const char* domain)
}


void set_language(const char* language)
void set_language(const std::string& language, const std::vector<std::string>* /*alternates*/)
{

// why shoudl we need alternates? which languages we support shoudl only be related
// to which languages we ship with and not which the os supports
std::cerr << "setting language to '" << language << "' \n";
get_manager().current_language_ = language;
get_manager().current_language_ += ".UTF-8";
Expand Down
37 changes: 19 additions & 18 deletions src/language.cpp
Expand Up @@ -133,25 +133,27 @@ static void wesnoth_setlocale(int category, std::string const &slocale,
// instead of en_US the first time round
// LANGUAGE overrides other settings, so for now just get rid of it
// FIXME: add configure check for unsetenv

//category is never LC_MESSAGES since that case was moved to gettext.cpp to remove teh dependency to libintl.h in this file
//that why code like if (category == LC_MESSAGES) is outcommented here.
#ifndef _WIN32
unsetenv ("LANGUAGE"); // void so no return value to check
#endif

#ifdef __APPLE__
if (category == LC_MESSAGES && setenv("LANG", locale.c_str(), 1) == -1) {
ERR_G << "setenv LANG failed: " << strerror(errno);
}
//if (category == LC_MESSAGES && setenv("LANG", locale.c_str(), 1) == -1) {
// ERR_G << "setenv LANG failed: " << strerror(errno);
//}
#endif

#ifdef _WIN32
std::string win_locale(locale, 0, 2);
#include "language_win32.ii"
if(category == LC_MESSAGES) {
SetEnvironmentVariableA("LANG", win_locale.c_str());
std::string env = "LANGUAGE=" + locale;
_putenv(env.c_str());
return;
}
//if(category == LC_MESSAGES) {
// SetEnvironmentVariableA("LANG", win_locale.c_str());
// std::string env = "LANGUAGE=" + locale;
// _putenv(env.c_str());
// return;
//}
locale = win_locale;
#endif

Expand Down Expand Up @@ -195,11 +197,11 @@ static void wesnoth_setlocale(int category, std::string const &slocale,
}

#ifndef _WIN32
if(category == LC_MESSAGES) {
WRN_G << "Setting LANGUAGE to '" << slocale << "'." << std::endl;
setenv("LANGUAGE", slocale.c_str(), 1);
std::setlocale(LC_MESSAGES, "");
}
//if(category == LC_MESSAGES) {
// WRN_G << "Setting LANGUAGE to '" << slocale << "'." << std::endl;
// setenv("LANGUAGE", slocale.c_str(), 1);
// std::setlocale(LC_MESSAGES, "");
//}
#endif

done:
Expand All @@ -220,8 +222,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());
translation::set_language(locale.localename, &locale.alternates);
load_strings(false);
}

Expand Down Expand Up @@ -258,7 +259,7 @@ const language_def& get_locale()

const std::string& prefs_locale = preferences::language();
if(prefs_locale.empty() == false) {
wesnoth_setlocale(LC_MESSAGES, prefs_locale, NULL);
translation::set_language(prefs_locale, NULL);
for(language_list::const_iterator i = known_languages.begin();
i != known_languages.end(); ++i) {
if (prefs_locale == i->localename)
Expand Down

0 comments on commit 9c799b9

Please sign in to comment.