From 3eb74d00a84100a07b05ff98163084311461af52 Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Mon, 2 Mar 2026 02:21:22 +0100 Subject: [PATCH] COMMON: Properly process translations with context in TransMan in .po --- common/translation.cpp | 30 +++++++++++++++++++++++++++--- common/translation.h | 3 ++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/common/translation.cpp b/common/translation.cpp index 1107088cb39..5d246b6a6a2 100644 --- a/common/translation.cpp +++ b/common/translation.cpp @@ -120,7 +120,7 @@ U32String TranslationManager::getTranslation(const char *message) const { return getTranslation(message, nullptr); } -U32String TranslationManager::getPoTranslation(const char* message) const { +U32String TranslationManager::getPoTranslation(const char *message) const { if (_currentTranslationMessages.empty() || *message == '\0') return U32String(message); @@ -132,9 +132,27 @@ U32String TranslationManager::getPoTranslation(const char* message) const { return _currentTranslationMessages[messageIndex].msgstr.decode(); } +U32String TranslationManager::getPoTranslation(const char *message, const char *context) const { + if (_currentTranslationMessages.empty() || *message == '\0') + return U32String(message); + + Common::String key = message; + if (context && *context != '\0') { + key += "\x04"; // Use the same separation mark as gettext does for context + key += context; + } + + int messageIndex = _poTranslations.getValOrDefault(key.c_str(), -1); + + if (messageIndex == -1) + return U32String(message); + + return _currentTranslationMessages[messageIndex].msgstr.decode(); +} + U32String TranslationManager::getTranslation(const char *message, const char *context) const { if (_usingPo) - return getPoTranslation(message); + return getPoTranslation(message, context); // If no language is set or message is empty, return msgid as is if (_currentTranslationMessages.empty() || *message == '\0') @@ -447,8 +465,14 @@ bool TranslationManager::loadLanguagePo(int index) { _currentTranslationMessages.reserve(numEntries); for (int i = 0; i < numEntries; ++i) { const Common::PlainPoMessageEntry *entry = poData->entry(i); + Common::String key = entry->msgid; - _poTranslations[entry->msgid] = i; + if (entry->msgctxt) { + key += "\x04"; // Use the same separation mark as gettext does for context + key += entry->msgctxt; + } + + _poTranslations[key] = i; PoMessageEntry engineEntry; engineEntry.msgid = i; engineEntry.msgstr = entry->msgstr; diff --git a/common/translation.h b/common/translation.h index 7fffe31031b..479ab2b952f 100644 --- a/common/translation.h +++ b/common/translation.h @@ -257,11 +257,12 @@ private: /** * Return the translation of @p message into the current language loaded from a .po file. - * + * * In case the message is not found in the translation catalog, * return the original untranslated message, as a U32String. */ U32String getPoTranslation(const char *message) const; + U32String getPoTranslation(const char *message, const char *context) const; /** * Load the translation for the given language from its .po file.