1
0
mirror of https://github.com/anope/anope.git synced 2026-07-02 17:53:13 +02:00

Use the correct language for the date in Anope::strftime.

This commit is contained in:
Sadie Powell
2025-10-02 15:29:43 +01:00
parent ee6b022f4d
commit 9782c1f94b
4 changed files with 54 additions and 41 deletions
+36 -34
View File
@@ -97,41 +97,41 @@ const char *Language::Translate(const NickCore *nc, int count, const char *singu
extern "C" int _nl_msg_cat_cntr;
#endif
namespace
void Language::SetLocale(const char* lang)
{
void PreTranslate(const char* lang)
{
if (!lang || !*lang)
lang = Config->DefLanguage.c_str();
#if defined(__GLIBC__) && defined(__USE_GNU_GETTEXT)
++_nl_msg_cat_cntr;
++_nl_msg_cat_cntr;
#endif
#ifdef _WIN32
SetThreadLocale(MAKELCID(MAKELANGID(WindowsGetLanguage(lang), SUBLANG_DEFAULT), SORT_DEFAULT));
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");
/* 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
}
}
void PostTranslate()
{
void Language::ResetLocale()
{
#ifdef _WIN32
SetThreadLocale(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT));
SetThreadLocale(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT));
#else
unsetenv("LANGUAGE");
unsetenv("LANG");
setlocale(LC_ALL, "");
unsetenv("LANGUAGE");
unsetenv("LANG");
setlocale(LC_ALL, "");
#endif
}
}
const char *Language::Translate(const char *lang, const char *string)
@@ -139,16 +139,13 @@ const char *Language::Translate(const char *lang, const char *string)
if (!string || !*string)
return "";
if (!lang || !*lang)
lang = Config->DefLanguage.c_str();
PreTranslate(lang);
SetLocale(lang);
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);
PostTranslate();
ResetLocale();
return translated_string;
}
@@ -157,19 +154,24 @@ const char *Language::Translate(const char *lang, int count, const char *singula
if (!singular || !*singular || !plural || !*plural)
return "";
if (!lang || !*lang)
lang = Config->DefLanguage.c_str();
PreTranslate(lang);
SetLocale(lang);
const char *translated_string = dngettext("anope", singular, plural, count);
for (unsigned i = 0; (translated_string == singular || translated_string == plural) && i < Domains.size(); ++i)
translated_string = dngettext(Domains[i].c_str(), singular, plural, count);
PostTranslate();
ResetLocale();
return translated_string;
}
#else
void Language::SetLocale(const char* lang)
{
}
void Language::ResetLocale()
{
}
const char *Language::Translate(const char *lang, const char *string)
{
return string != NULL ? string : "";