summaryrefslogtreecommitdiff
path: root/src/language.cpp
blob: b04068f8340d33d9a1e08d3e635328fbba9d896b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Anope IRC Services
 *
 * Copyright (C) 2003-2016 Anope Team <team@anope.org>
 *
 * This file is part of Anope. Anope 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, version 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see see <http://www.gnu.org/licenses/>.
 */

#include "services.h"
#include "modules.h"
#include "commands.h"
#include "config.h"
#include "language.h"
#include "modules/nickserv.h"

#if GETTEXT_FOUND
# include <libintl.h>
#endif

std::vector<Anope::string> Language::Languages;
std::vector<Anope::string> Language::Domains;

void Language::InitLanguages()
{
#if GETTEXT_FOUND
	Log(LOG_DEBUG) << "Initializing Languages...";

	Languages.clear();

	if (!bindtextdomain("anope", Anope::LocaleDir.c_str()))
		Log() << "Error calling bindtextdomain, " << Anope::LastError();
	else
		Log(LOG_DEBUG) << "Successfully bound anope to " << Anope::LocaleDir;

	setlocale(LC_ALL, "");

	spacesepstream sep(Config->GetBlock("options")->Get<Anope::string>("languages"));
	Anope::string language;
	while (sep.GetToken(language))
	{
		const Anope::string &lang_name = Translate(language.c_str(), _("English"));
		if (lang_name == "English")
		{
			Log() << "Unable to use language " << language;
			continue;
		}

		Log(LOG_DEBUG) << "Found language " << language;
		Languages.push_back(language);
	}
#else
	Log() << "Unable to initialize languages, gettext is not installed";
#endif
}

const char *Language::Translate(const char *string)
{
	return Translate("", string);
}

const char *Language::Translate(const Anope::string &string)
{
	return Translate("", string.c_str());
}

const char *Language::Translate(User *u, const char *string)
{
	if (u && u->Account())
		return Translate(u->Account(), string);
	else
		return Translate("", string);
}

const char *Language::Translate(User *u, const Anope::string &string)
{
	return Translate(u, string.c_str());
}

const char *Language::Translate(NickServ::Account *nc, const char *string)
{
	return Translate(nc ? nc->GetLanguage().c_str() : "", string);
}

const char *Language::Translate(NickServ::Account *nc, const Anope::string &string)
{
	return Translate(nc, string.c_str());
}

#if GETTEXT_FOUND

const char *Language::Translate(const char *lang, const char *string)
{
	if (!string || !*string)
		return "";

	if (!lang || !*lang)
		lang = Config->DefLanguage.c_str();

#ifdef _WIN32
	SetThreadLocale(MAKELCID(MAKELANGID(WindowsGetLanguage(lang), SUBLANG_DEFAULT), SORT_DEFAULT));
#else
	/* First, set LANG and LANGUAGE env variables.
	 * Some systems (Debian) don't care about this, so we must setlocale LC_ALL as well.
	 * BUT if this call fails because the LANGUAGE env variable is set, setlocale resets
	 * the locale to "C", which short circuits gettext and causes it to fail on systems that
	 * use the LANGUAGE env variable. We must reset the locale to en_US (or, anything not
	 * C or POSIX) then.
	 */
	setenv("LANG", lang, 1);
	setenv("LANGUAGE", lang, 1);
	if (setlocale(LC_ALL, lang) == NULL)
		setlocale(LC_ALL, "en_US");
#endif
	const char *translated_string = dgettext("anope", string);
	for (unsigned i = 0; translated_string == string && i < Domains.size(); ++i)
		translated_string = dgettext(Domains[i].c_str(), string);
#ifdef _WIN32
	SetThreadLocale(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT));
#else
	unsetenv("LANGUAGE");
	unsetenv("LANG");
	setlocale(LC_ALL, "");
#endif

	return translated_string;
}
#else
const char *Language::Translate(const char *lang, const char *string)
{
	return string != NULL ? string : "";
}
#endif