Exemple #1
0
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
}
Exemple #2
0
static void wesnoth_setlocale(int category, std::string const &slocale,
	std::vector<std::string> const *alternates)
{
	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
	// FIXME: add configure check for unsetenv
#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);
	}
#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;
	}
	locale = win_locale;
#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(category, locale.c_str());
			if (res) {
				LOG_G << "Set locale to '" << locale << "' result: '" << res << "'.\n";
				goto done;
			}
		}

		if (!alternates || i == alternates->end()) break;
		locale = *i;
		++i;
	}

	WRN_G << "setlocale() failed for '" << slocale << "'." << std::endl;

	if (category == LC_TIME) {
		time_locale_correct() = false;
	}

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

	done:
	DBG_G << "Numeric locale: " << std::setlocale(LC_NUMERIC, NULL) << '\n';
	DBG_G << "Full locale: " << std::setlocale(LC_ALL, NULL) << '\n';
}