[graphics/krita/krita/4.3] libs/widgetutils: Fix translation lookup in KisActionRegistry

Boudewijn Rempt null at kde.org
Wed Sep 30 09:02:41 BST 2020


Git commit fb6d6c616073b10a3af1f40cab616805daac108a by Boudewijn Rempt, on behalf of L. E. Segovia.
Committed on 30/09/2020 at 08:02.
Pushed by rempt into branch 'krita/4.3'.

Fix translation lookup in KisActionRegistry

In b55fdf3303efa79c9c315916ab44a91b794b2ebd, unknowingly for
translators, a default "action" message context was introduced for
.action files. However, this context was hardcoded, meaning that
translations would break if the text used a more specific disambiguation
context.

This is only part of the fix for bug 426992. To fix it completely, the
i18n team must start marking strings as verified -- translations marked
with 'fuzzy' are skipped by gettext.

CCBUG: 426992
CCMAIL: kimageshop at kde.org
(cherry picked from commit a48ec499fd471799e46bfa6f43ff0d544d558139)

M  +31   -10   libs/widgetutils/kis_action_registry.cpp

https://invent.kde.org/graphics/krita/commit/fb6d6c616073b10a3af1f40cab616805daac108a

diff --git a/libs/widgetutils/kis_action_registry.cpp b/libs/widgetutils/kis_action_registry.cpp
index 9022e1b9c9..e1b3092a22 100644
--- a/libs/widgetutils/kis_action_registry.cpp
+++ b/libs/widgetutils/kis_action_registry.cpp
@@ -80,6 +80,11 @@ namespace {
         bool m_explicitlyReset = false;
     };
 
+    // Convenience macros to extract a child node.
+    QDomElement getChild(QDomElement xml, QString node) {
+        return xml.firstChildElement(node);
+    }
+
     // Convenience macros to extract text of a child node.
     QString getChildContent(QDomElement xml, QString node) {
         return xml.firstChildElement(node).text();
@@ -87,17 +92,33 @@ namespace {
 
     // Use Krita debug logging categories instead of KDE's default qDebug() for
     // harmless empty strings and translations
-    QString quietlyTranslate(const QString &s) {
-        if (s.isEmpty()) {
-            return s;
+    QString quietlyTranslate(const QDomElement &s) {
+        if (s.isNull() || s.text().isEmpty()) {
+            return QString();
+        }
+        QString translatedString;
+        const QString attrContext = QStringLiteral("context");
+        const QString attrDomain = QStringLiteral("translationDomain");
+        QString context = QStringLiteral("action");
+
+        if (!s.attribute(attrContext).isEmpty()) {
+            context = s.attribute(attrContext);
+        }
+
+        QByteArray domain = s.attribute(attrDomain).toUtf8();
+        if (domain.isEmpty()) {
+            domain = s.ownerDocument().documentElement().attribute(attrDomain).toUtf8();
+            if (domain.isEmpty()) {
+                domain = KLocalizedString::applicationDomain();
+            }
         }
-        QString translatedString = i18nc("action", s.toUtf8());
-        if (translatedString == s) {
-            translatedString = i18n(s.toUtf8());
+        translatedString = i18ndc(domain.constData(), context.toUtf8().constData(), s.text().toUtf8().constData());
+        if (translatedString == s.text()) {
+            translatedString = i18n(s.text().toUtf8().constData());
         }
         if (translatedString.isEmpty()) {
-            dbgAction << "No translation found for" << s;
-            return s;
+            dbgAction << "No translation found for" << s.text();
+            return s.text();
         }
 
         return translatedString;
@@ -268,7 +289,7 @@ bool KisActionRegistry::propertizeAction(const QString &name, QAction * a)
     QDomElement actionXml = info.xmlData;
     if (!actionXml.text().isEmpty()) {
         // i18n requires converting format from QString.
-        auto getChildContent_i18n = [=](QString node){return quietlyTranslate(getChildContent(actionXml, node));};
+        auto getChildContent_i18n = [=](QString node){return quietlyTranslate(getChild(actionXml, node));};
 
         // Note: the fields in the .action documents marked for translation are determined by extractrc.
         QString icon      = getChildContent(actionXml, "icon");
@@ -340,7 +361,7 @@ void KisActionRegistry::Private::loadActionFiles()
 
             // <text> field
             QDomElement categoryTextNode = actions.firstChild().toElement();
-            QString categoryName         = quietlyTranslate(categoryTextNode.text());
+            QString categoryName         = quietlyTranslate(categoryTextNode);
 
             // <action></action> tags
             QDomElement actionXml  = categoryTextNode.nextSiblingElement();


More information about the kimageshop mailing list