Added gettext context support

This commit is contained in:
Leon Styhre 2024-07-23 18:17:44 +02:00
parent 2d991a52b9
commit f4d1534bab
3 changed files with 37 additions and 1 deletions

View file

@ -43,6 +43,35 @@ namespace Utils
// clang-format on
float sMenuTitleScaleFactor {1.0f};
const char* pgettextBuiltin(const char* msgctxt, const char* msgid)
{
// This is an unbelievable hack but it's actually done pretty much the same way in
// the gettext.h header where a macro is used to wrap around the libintl functionality.
// Why this function is simply not part of libintl itself is anyone's guess, as that
// would be the logical thing to do.
std::string lookup;
lookup.append(msgctxt).append("\004").append(msgid);
const char* translation = gettext(lookup.c_str());
if (translation == lookup.c_str())
return msgid;
else
return translation;
}
const char* npgettextBuiltin(const char* msgctxt,
const char* msgid1,
const char* msgid2,
unsigned long int n)
{
std::string lookup;
lookup.append(msgctxt).append("\004").append(msgid1);
const char* translation = ngettext(lookup.c_str(), msgid2, n);
if (translation == lookup.c_str())
return msgid1;
else
return translation;
}
std::pair<std::string, std::string> getLocale()
{
#if defined(_WIN64)

View file

@ -16,6 +16,8 @@
#define _(STR) std::string(gettext(STR))
#define _n(STR1, STR2, NUM) std::string(ngettext(STR1, STR2, NUM))
#define _p(STR1, STR2) Utils::Localization::pgettextBuiltin(STR1, STR2)
#define _np(STR1, STR2, STR3, NUM) Utils::Localization::npgettextBuiltin(STR1, STR2, STR3, NUM)
namespace Utils
{
@ -24,6 +26,11 @@ namespace Utils
extern const std::vector<std::pair<std::string, std::string>> sSupportedLocales;
extern float sMenuTitleScaleFactor;
const char* pgettextBuiltin(const char* msgctxt, const char* msgid);
const char* npgettextBuiltin(const char* msgctxt,
const char* msgid1,
const char* msgid2,
unsigned long int n);
std::pair<std::string, std::string> getLocale();
void setLocale();

View file

@ -23,7 +23,7 @@ if [ ! $(which xgettext 2>/dev/null) ]; then
exit
fi
find ../es-app/src/ ../es-core/src -name '*.cpp' -o -name '*.h' | xgettext -f - -o ../locale/es-de.pot -k_ -k_n:1,2 --no-location \
find ../es-app/src/ ../es-core/src -name '*.cpp' -o -name '*.h' | xgettext -f - -o ../locale/es-de.pot -k_ -k_n:1,2 -k_p:1c,2 -k_np:1c,2,3 --no-location \
--copyright-holder="Northwestern Software AB" --package-name="ES-DE Frontend" --msgid-bugs-address "info@es-de.org"
sed -i "1s/.*/# ES-DE Frontend translation strings./" ../locale/es-de.pot