summaryrefslogtreecommitdiff
path: root/src/language.cpp
blob: 4e1b94e02975a203429e6afb768d2ab046aeba74 (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
#include "services.h"
#include <stack>

std::vector<Anope::string> languages;
std::vector<Anope::string> domains;

void InitLanguages()
{
#if GETTEXT_FOUND
	languages.clear();
	spacesepstream sep(Config->Languages);
	Anope::string language;

	while (sep.GetToken(language))
	{
		if (!IsFile("languages/" + language + "/LC_MESSAGES/anope.mo"))
		{
			Log() << "Error loading language " << language << ", file does not exist!";
		}
		else
		{
			Log(LOG_DEBUG) << "Found language file " << language;
			languages.push_back(language);
		}
	}

	if (!bindtextdomain("anope", (services_dir + "/languages/").c_str()))
		Log() << "Error calling bindtextdomain, " << Anope::LastError();
	else
		Log(LOG_DEBUG) << "Successfully bound anope to " << services_dir << "/languages/";
	
	setlocale(LC_ALL, "");
#else
	Log() << "Can not load languages, gettext is not installed";
#endif
}

const char *translate(const char *string)
{
	return anope_gettext(Config->NSDefLanguage.c_str(), string);
}

const char *translate(User *u, const char *string)
{
	return translate(u ? u->Account() : NULL, string);
}

const char *translate(NickCore *nc, const char *string)
{
	return anope_gettext(nc ? nc->language.c_str() : Config->NSDefLanguage.c_str(), string);
}

#if GETTEXT_FOUND

/* Used by gettext to make it always dynamically load language strings (so we can drop them in while Anope is running) */
extern "C" int _nl_msg_cat_cntr;

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

	++_nl_msg_cat_cntr;
#ifdef _WIN32
	SetThreadLocale(MAKELCID(MAKELANGID(WindowsGetLanguage(lang), SUBLANG_DEFAULT), SORT_DEFAULT));
#else
	/* First, set LANGUAGE env variable.
	 * Some systems (Debian) don't care about this, so we must setlocale LC_ALL aswell.
	 * 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("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");
	setlocale(LC_ALL, "");
#endif

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

void SyntaxError(CommandSource &source, const Anope::string &command, const Anope::string &message)
{
	source.Reply(_("Syntax: \002%s\002"), message.c_str());
	source.Reply(_(_(MORE_INFO)), Config->UseStrictPrivMsgString.c_str(), source.owner->nick.c_str(), command.c_str());
}