From 4bc85a617976d617e3542b6da64f1011c220de70 Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 20 Mar 2021 11:22:06 +0800 Subject: [PATCH] Allow translating in UTF8 --- src/utils/translation.cpp | 60 +++++++++++++++++++++++++++++++++++++++ src/utils/translation.hpp | 18 +++++++++--- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/utils/translation.cpp b/src/utils/translation.cpp index 28292d36b..6b001d420 100644 --- a/src/utils/translation.cpp +++ b/src/utils/translation.cpp @@ -567,6 +567,38 @@ irr::core::stringw Translations::w_gettext(const char* original, const char* con } // w_gettext +// ---------------------------------------------------------------------------- +/** + * \param original Message to translate + * \param context Optional, can be set to differentiate 2 strings that are identical + * in English but could be different in other languages + */ +std::string Translations::gettext(const char* original, const char* context) +{ + +#ifdef SERVER_ONLY + return ""; +#else + + if (original[0] == '\0') return ""; + +#if TRANSLATE_VERBOSE + Log::info("Translations", "Translating %s", original); +#endif + + const std::string& original_t = (context == NULL ? + m_dictionary->translate(original) : + m_dictionary->translate_ctxt(context, original)); + +#if TRANSLATE_VERBOSE + std::cout << " translation : " << original_t << std::endl; +#endif + + return original_t; +#endif + +} // gettext + // ---------------------------------------------------------------------------- /** * \param singular Message to translate in singular form @@ -614,6 +646,34 @@ irr::core::stringw Translations::w_ngettext(const char* singular, const char* pl } // w_ngettext +// ---------------------------------------------------------------------------- +/** + * \param singular Message to translate in singular form + * \param plural Message to translate in plural form (can be the same as the singular form) + * \param num Count used to obtain the correct plural form. + * \param context Optional, can be set to differentiate 2 strings that are identical + * in English but could be different in other languages + */ +std::string Translations::ngettext(const char* singular, const char* plural, int num, const char* context) +{ +#ifdef SERVER_ONLY + return ""; + +#else + + const std::string& res = (context == NULL ? + m_dictionary->translate_plural(singular, plural, num) : + m_dictionary->translate_ctxt_plural(context, singular, plural, num)); + +#if TRANSLATE_VERBOSE + std::cout << " translation: " << res << std::endl; +#endif + + return res; +#endif + +} // ngettext + // ---------------------------------------------------------------------------- #ifndef SERVER_ONLY std::set Translations::getCurrentAllChar() diff --git a/src/utils/translation.hpp b/src/utils/translation.hpp index f075326fd..8f70f51be 100644 --- a/src/utils/translation.hpp +++ b/src/utils/translation.hpp @@ -31,12 +31,20 @@ #include "tinygettext/tinygettext.hpp" #endif -# define _(String, ...) (StringUtils::insertValues(translations->w_gettext(String), ##__VA_ARGS__)) +#ifdef STK_UTF8_GETTEXT +#define STK_GETTEXT gettext +#define STK_NGETTEXT ngettext +#else +#define STK_GETTEXT w_gettext +#define STK_NGETTEXT w_ngettext +#endif + +# define _(String, ...) (StringUtils::insertValues(translations->STK_GETTEXT(String), ##__VA_ARGS__)) #undef _C #undef _P -# define _C(Ctx, String, ...) (StringUtils::insertValues(translations->w_gettext(String, Ctx), ##__VA_ARGS__)) -# define _P(Singular, Plural, Num, ...) (StringUtils::insertValues(translations->w_ngettext(Singular, Plural, Num), Num, ##__VA_ARGS__)) -# define _CP(Ctx, Singular, Plural, Num, ...) (StringUtils::insertValues(translations->w_ngettext(Singular, Plural, Num, Ctx), Num, ##__VA_ARGS__)) +# define _C(Ctx, String, ...) (StringUtils::insertValues(translations->STK_GETTEXT(String, Ctx), ##__VA_ARGS__)) +# define _P(Singular, Plural, Num, ...) (StringUtils::insertValues(translations->STK_NGETTEXT(Singular, Plural, Num), Num, ##__VA_ARGS__)) +# define _CP(Ctx, Singular, Plural, Num, ...) (StringUtils::insertValues(translations->STK_NGETTEXT(Singular, Plural, Num, Ctx), Num, ##__VA_ARGS__)) # define gettext_noop(String) (String) # define N_(String) (gettext_noop (String)) // libintl defines its own fprintf, which doesn't work properly @@ -64,9 +72,11 @@ public: irr::core::stringw w_gettext(const wchar_t* original, const char* context=NULL); irr::core::stringw w_gettext(const char* original, const char* context=NULL); + std::string gettext(const char* original, const char* context=NULL); irr::core::stringw w_ngettext(const wchar_t* singular, const wchar_t* plural, int num, const char* context=NULL); irr::core::stringw w_ngettext(const char* singular, const char* plural, int num, const char* context=NULL); + std::string ngettext(const char* singular, const char* plural, int num, const char* context=NULL); #ifndef SERVER_ONLY const std::vector* getLanguageList() const;