Anope IRC Services  Version 2.0
language.cpp
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 #include "services.h"
14 #include "modules.h"
15 #include "commands.h"
16 #include "config.h"
17 #include "language.h"
18 
19 #if GETTEXT_FOUND
20 # include <libintl.h>
21 #endif
22 
23 std::vector<Anope::string> Language::Languages;
24 std::vector<Anope::string> Language::Domains;
25 
27 {
28 #if GETTEXT_FOUND
29  Log(LOG_DEBUG) << "Initializing Languages...";
30 
31  Languages.clear();
32 
33  if (!bindtextdomain("anope", Anope::LocaleDir.c_str()))
34  Log() << "Error calling bindtextdomain, " << Anope::LastError();
35  else
36  Log(LOG_DEBUG) << "Successfully bound anope to " << Anope::LocaleDir;
37 
38  setlocale(LC_ALL, "");
39 
40  spacesepstream sep(Config->GetBlock("options")->Get<const Anope::string>("languages"));
41  Anope::string language;
42  while (sep.GetToken(language))
43  {
44  const Anope::string &lang_name = Translate(language.c_str(), _("English"));
45  if (lang_name == "English")
46  {
47  Log() << "Unable to use language " << language;
48  continue;
49  }
50 
51  Log(LOG_DEBUG) << "Found language " << language;
52  Languages.push_back(language);
53  }
54 #else
55  Log() << "Unable to initialize languages, gettext is not installed";
56 #endif
57 }
58 
59 const char *Language::Translate(const char *string)
60 {
61  return Translate("", string);
62 }
63 
64 const char *Language::Translate(User *u, const char *string)
65 {
66  if (u && u->Account())
67  return Translate(u->Account(), string);
68  else
69  return Translate("", string);
70 }
71 
72 const char *Language::Translate(const NickCore *nc, const char *string)
73 {
74  return Translate(nc ? nc->language.c_str() : "", string);
75 }
76 
77 #if GETTEXT_FOUND
78 
79 extern "C" int _nl_msg_cat_cntr;
80 
81 const char *Language::Translate(const char *lang, const char *string)
82 {
83  if (!string || !*string)
84  return "";
85 
86  if (!lang || !*lang)
87  lang = Config->DefLanguage.c_str();
88 
89  ++_nl_msg_cat_cntr;
90 #ifdef _WIN32
91  SetThreadLocale(MAKELCID(MAKELANGID(WindowsGetLanguage(lang), SUBLANG_DEFAULT), SORT_DEFAULT));
92 #else
93  /* First, set LANG and LANGUAGE env variables.
94  * Some systems (Debian) don't care about this, so we must setlocale LC_ALL aswell.
95  * BUT if this call fails because the LANGUAGE env variable is set, setlocale resets
96  * the locale to "C", which short circuits gettext and causes it to fail on systems that
97  * use the LANGUAGE env variable. We must reset the locale to en_US (or, anything not
98  * C or POSIX) then.
99  */
100  setenv("LANG", lang, 1);
101  setenv("LANGUAGE", lang, 1);
102  if (setlocale(LC_ALL, lang) == NULL)
103  setlocale(LC_ALL, "en_US");
104 #endif
105  const char *translated_string = dgettext("anope", string);
106  for (unsigned i = 0; translated_string == string && i < Domains.size(); ++i)
107  translated_string = dgettext(Domains[i].c_str(), string);
108 #ifdef _WIN32
109  SetThreadLocale(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT));
110 #else
111  unsetenv("LANGUAGE");
112  unsetenv("LANG");
113  setlocale(LC_ALL, "");
114 #endif
115 
116  return translated_string;
117 }
118 #else
119 const char *Language::Translate(const char *lang, const char *string)
120 {
121  return string != NULL ? string : "";
122 }
123 #endif
124 
CoreExport const string LastError()
Definition: misc.cpp:606
Definition: users.h:34
Definition: Config.cs:26
void InitLanguages()
Definition: language.cpp:26
CoreExport std::vector< Anope::string > Languages
Definition: language.cpp:23
std::basic_string< char, ci_char_traits, std::allocator< char > > string
Definition: hashcomp.h:133
CoreExport const char * Translate(const char *string)
Definition: language.cpp:59
NickCore * Account() const
Definition: users.cpp:422
bool GetToken(Anope::string &token)
Definition: hashcomp.cpp:99
CoreExport Anope::string LocaleDir
Definition: init.cpp:33
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
#define _(x)
Definition: services.h:50
Anope::string language
Definition: account.h:118
std::vector< Anope::string > Domains
Definition: language.cpp:24