anope- supernets anope source code & configuration |
git clone git://git.acid.vegas/anope.git |
Log | Files | Refs | Archive | README |
language.cpp (3306B)
1 /* 2 * 3 * (C) 2003-2022 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 #include "services.h" 13 #include "modules.h" 14 #include "commands.h" 15 #include "config.h" 16 #include "language.h" 17 18 #if GETTEXT_FOUND 19 # include <libintl.h> 20 #endif 21 22 std::vector<Anope::string> Language::Languages; 23 std::vector<Anope::string> Language::Domains; 24 25 void Language::InitLanguages() 26 { 27 #if GETTEXT_FOUND 28 Log(LOG_DEBUG) << "Initializing Languages..."; 29 30 Languages.clear(); 31 32 if (!bindtextdomain("anope", Anope::LocaleDir.c_str())) 33 Log() << "Error calling bindtextdomain, " << Anope::LastError(); 34 else 35 Log(LOG_DEBUG) << "Successfully bound anope to " << Anope::LocaleDir; 36 37 setlocale(LC_ALL, ""); 38 39 spacesepstream sep(Config->GetBlock("options")->Get<const Anope::string>("languages")); 40 Anope::string language; 41 while (sep.GetToken(language)) 42 { 43 const Anope::string &lang_name = Translate(language.c_str(), _("English")); 44 if (lang_name == "English") 45 { 46 Log() << "Unable to use language " << language; 47 continue; 48 } 49 50 Log(LOG_DEBUG) << "Found language " << language; 51 Languages.push_back(language); 52 } 53 #else 54 Log() << "Unable to initialize languages, gettext is not installed"; 55 #endif 56 } 57 58 const char *Language::Translate(const char *string) 59 { 60 return Translate("", string); 61 } 62 63 const char *Language::Translate(User *u, const char *string) 64 { 65 if (u && u->Account()) 66 return Translate(u->Account(), string); 67 else 68 return Translate("", string); 69 } 70 71 const char *Language::Translate(const NickCore *nc, const char *string) 72 { 73 return Translate(nc ? nc->language.c_str() : "", string); 74 } 75 76 #if GETTEXT_FOUND 77 78 #if defined(__GLIBC__) && defined(__USE_GNU_GETTEXT) 79 extern "C" int _nl_msg_cat_cntr; 80 #endif 81 82 const char *Language::Translate(const char *lang, const char *string) 83 { 84 if (!string || !*string) 85 return ""; 86 87 if (!lang || !*lang) 88 lang = Config->DefLanguage.c_str(); 89 90 #if defined(__GLIBC__) && defined(__USE_GNU_GETTEXT) 91 ++_nl_msg_cat_cntr; 92 #endif 93 94 #ifdef _WIN32 95 SetThreadLocale(MAKELCID(MAKELANGID(WindowsGetLanguage(lang), SUBLANG_DEFAULT), SORT_DEFAULT)); 96 #else 97 /* First, set LANG and LANGUAGE env variables. 98 * Some systems (Debian) don't care about this, so we must setlocale LC_ALL as well. 99 * BUT if this call fails because the LANGUAGE env variable is set, setlocale resets 100 * the locale to "C", which short circuits gettext and causes it to fail on systems that 101 * use the LANGUAGE env variable. We must reset the locale to en_US (or, anything not 102 * C or POSIX) then. 103 */ 104 setenv("LANG", lang, 1); 105 setenv("LANGUAGE", lang, 1); 106 if (setlocale(LC_ALL, lang) == NULL) 107 setlocale(LC_ALL, "en_US"); 108 #endif 109 const char *translated_string = dgettext("anope", string); 110 for (unsigned i = 0; translated_string == string && i < Domains.size(); ++i) 111 translated_string = dgettext(Domains[i].c_str(), string); 112 #ifdef _WIN32 113 SetThreadLocale(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT)); 114 #else 115 unsetenv("LANGUAGE"); 116 unsetenv("LANG"); 117 setlocale(LC_ALL, ""); 118 #endif 119 120 return translated_string; 121 } 122 #else 123 const char *Language::Translate(const char *lang, const char *string) 124 { 125 return string != NULL ? string : ""; 126 } 127 #endif