File indexing completed on 2025-02-09 04:28:36

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008 */
0009 
0010 #include "i18nc.h"
0011 
0012 #include <QStringList>
0013 
0014 #include "abstractlocalizer.h"
0015 #include "exception.h"
0016 #include "parser.h"
0017 
0018 #include <QDebug>
0019 #include <complex>
0020 #include <util.h>
0021 
0022 I18ncNodeFactory::I18ncNodeFactory() = default;
0023 
0024 Node *I18ncNodeFactory::getNode(const QString &tagContent, Parser *p) const
0025 {
0026     auto expr = smartSplit(tagContent);
0027 
0028     if (expr.size() < 3)
0029         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18nc tag takes at least two arguments"));
0030 
0031     auto contextText = expr.at(1);
0032 
0033     if (!(contextText.startsWith(QLatin1Char('"')) && contextText.endsWith(QLatin1Char('"')))
0034         && !(contextText.startsWith(QLatin1Char('\'')) && contextText.endsWith(QLatin1Char('\'')))) {
0035         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18nc tag first argument must be a static string."));
0036     }
0037     contextText = contextText.mid(1, contextText.size() - 2);
0038 
0039     auto sourceText = expr.at(2);
0040 
0041     if (!(sourceText.startsWith(QLatin1Char('"')) && sourceText.endsWith(QLatin1Char('"')))
0042         && !(sourceText.startsWith(QLatin1Char('\'')) && sourceText.endsWith(QLatin1Char('\'')))) {
0043         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18nc tag second argument must be a static string."));
0044     }
0045     sourceText = sourceText.mid(1, sourceText.size() - 2);
0046 
0047     QList<FilterExpression> feList;
0048     for (auto i = 3; i < expr.size(); ++i) {
0049         feList.append(FilterExpression(expr.at(i), p));
0050     }
0051 
0052     return new I18ncNode(sourceText, contextText, feList);
0053 }
0054 
0055 I18ncVarNodeFactory::I18ncVarNodeFactory() = default;
0056 
0057 KTextTemplate::Node *I18ncVarNodeFactory::getNode(const QString &tagContent, Parser *p) const
0058 {
0059     auto expr = smartSplit(tagContent);
0060 
0061     if (expr.size() < 5)
0062         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18nc_var tag takes at least four arguments"));
0063 
0064     auto contextText = expr.at(1);
0065 
0066     if (!(contextText.startsWith(QLatin1Char('"')) && contextText.endsWith(QLatin1Char('"')))
0067         && !(contextText.startsWith(QLatin1Char('\'')) && contextText.endsWith(QLatin1Char('\'')))) {
0068         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18nc_var tag first argument must be a static string."));
0069     }
0070     contextText = contextText.mid(1, contextText.size() - 2);
0071 
0072     auto sourceText = expr.at(2);
0073 
0074     if (!(sourceText.startsWith(QLatin1Char('"')) && sourceText.endsWith(QLatin1Char('"')))
0075         && !(sourceText.startsWith(QLatin1Char('\'')) && sourceText.endsWith(QLatin1Char('\'')))) {
0076         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18nc_var tag second argument must be a static string."));
0077     }
0078     sourceText = sourceText.mid(1, sourceText.size() - 2);
0079 
0080     QList<FilterExpression> feList;
0081     for (auto i = 3; i < expr.size() - 2; ++i) {
0082         feList.append(FilterExpression(expr.at(i), p));
0083     }
0084 
0085     const auto resultName = expr.last();
0086 
0087     return new I18ncVarNode(sourceText, contextText, feList, resultName);
0088 }
0089 
0090 I18ncNode::I18ncNode(const QString &sourceText, const QString &context, const QList<KTextTemplate::FilterExpression> &feList, QObject *parent)
0091     : Node(parent)
0092     , m_sourceText(sourceText)
0093     , m_context(context)
0094     , m_filterExpressionList(feList)
0095 {
0096 }
0097 
0098 void I18ncNode::render(OutputStream *stream, Context *c) const
0099 {
0100     QVariantList args;
0101     for (const FilterExpression &fe : m_filterExpressionList)
0102         args.append(fe.resolve(c));
0103     const auto resultString = c->localizer()->localizeContextString(m_sourceText, m_context, args);
0104 
0105     streamValueInContext(stream, resultString, c);
0106 }
0107 
0108 I18ncVarNode::I18ncVarNode(const QString &sourceText,
0109                            const QString &context,
0110                            const QList<KTextTemplate::FilterExpression> &feList,
0111                            const QString &resultName,
0112                            QObject *parent)
0113     : Node(parent)
0114     , m_sourceText(sourceText)
0115     , m_context(context)
0116     , m_filterExpressionList(feList)
0117     , m_resultName(resultName)
0118 {
0119 }
0120 
0121 void I18ncVarNode::render(OutputStream *stream, Context *c) const
0122 {
0123     Q_UNUSED(stream)
0124     QVariantList args;
0125     for (const FilterExpression &fe : m_filterExpressionList)
0126         args.append(fe.resolve(c));
0127     const auto resultString = c->localizer()->localizeContextString(m_sourceText, m_context, args);
0128 
0129     c->insert(m_resultName, resultString);
0130 }
0131 
0132 #include "moc_i18nc.cpp"