File indexing completed on 2024-03-24 15:40:50

0001 /*
0002     SPDX-FileCopyrightText: 2017 Alexander Potashev <aspotashev@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "ktoolbarhelper_p.h"
0008 
0009 #include <QVector>
0010 
0011 #include <KLocalizedString>
0012 
0013 namespace KToolbarHelper
0014 {
0015 QString i18nToolBarName(const QDomElement &element)
0016 {
0017     QDomElement textElement;
0018     bool textElementFound = false;
0019     const QVector<QString> textKeys = {QStringLiteral("text"), QStringLiteral("Text")};
0020     for (const QString &key : textKeys) {
0021         QDomNode textNode = element.namedItem(key);
0022         if (textNode.isElement()) {
0023             textElement = textNode.toElement();
0024             textElementFound = true;
0025             break;
0026         }
0027     }
0028 
0029     if (!textElementFound) {
0030         return element.attribute(QStringLiteral("name"));
0031     }
0032 
0033     QByteArray domain = textElement.attribute(QStringLiteral("translationDomain")).toUtf8();
0034     QByteArray text = textElement.text().toUtf8();
0035     QByteArray context = textElement.attribute(QStringLiteral("context")).toUtf8();
0036 
0037     if (domain.isEmpty()) {
0038         domain = element.ownerDocument().documentElement().attribute(QStringLiteral("translationDomain")).toUtf8();
0039         if (domain.isEmpty()) {
0040             domain = KLocalizedString::applicationDomain();
0041         }
0042     }
0043     QString i18nText;
0044     if (!text.isEmpty() && !context.isEmpty()) {
0045         i18nText = i18ndc(domain.constData(), context.constData(), text.constData());
0046     } else if (!text.isEmpty()) {
0047         i18nText = i18nd(domain.constData(), text.constData());
0048     }
0049     return i18nText;
0050 }
0051 
0052 } // namespace KToolbarHelper