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 "i18np.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 I18npNodeFactory::I18npNodeFactory() = default;
0023 
0024 Node *I18npNodeFactory::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: i18np tag takes at least two arguments"));
0030 
0031     auto sourceText = expr.at(1);
0032 
0033     if (!(sourceText.startsWith(QLatin1Char('"')) && sourceText.endsWith(QLatin1Char('"')))
0034         && !(sourceText.startsWith(QLatin1Char('\'')) && sourceText.endsWith(QLatin1Char('\'')))) {
0035         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18np tag first argument must be a static string."));
0036     }
0037     sourceText = sourceText.mid(1, sourceText.size() - 2);
0038 
0039     auto pluralText = expr.at(2);
0040 
0041     auto argsStart = 3;
0042     if (!(pluralText.startsWith(QLatin1Char('"')) && pluralText.endsWith(QLatin1Char('"')))
0043         && !(pluralText.startsWith(QLatin1Char('\'')) && pluralText.endsWith(QLatin1Char('\'')))) {
0044         argsStart = 2;
0045         pluralText = sourceText;
0046     } else {
0047         pluralText = pluralText.mid(1, pluralText.size() - 2);
0048     }
0049 
0050     QList<FilterExpression> feList;
0051     for (auto i = argsStart; i < expr.size(); ++i) {
0052         feList.append(FilterExpression(expr.at(i), p));
0053     }
0054 
0055     return new I18npNode(sourceText, pluralText, feList);
0056 }
0057 
0058 I18npVarNodeFactory::I18npVarNodeFactory() = default;
0059 
0060 KTextTemplate::Node *I18npVarNodeFactory::getNode(const QString &tagContent, Parser *p) const
0061 {
0062     auto expr = smartSplit(tagContent);
0063 
0064     if (expr.size() < 5)
0065         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18np_var tag takes at least four arguments"));
0066 
0067     auto sourceText = expr.at(1);
0068 
0069     if (!(sourceText.startsWith(QLatin1Char('"')) && sourceText.endsWith(QLatin1Char('"')))
0070         && !(sourceText.startsWith(QLatin1Char('\'')) && sourceText.endsWith(QLatin1Char('\'')))) {
0071         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18np tag first argument must be a static string."));
0072     }
0073     sourceText = sourceText.mid(1, sourceText.size() - 2);
0074 
0075     auto pluralText = expr.at(2);
0076 
0077     auto argsStart = 3;
0078     if (!(pluralText.startsWith(QLatin1Char('"')) && pluralText.endsWith(QLatin1Char('"')))
0079         && !(pluralText.startsWith(QLatin1Char('\'')) && pluralText.endsWith(QLatin1Char('\'')))) {
0080         argsStart = 2;
0081         pluralText = sourceText;
0082     } else {
0083         pluralText = pluralText.mid(1, pluralText.size() - 2);
0084     }
0085 
0086     QList<FilterExpression> feList;
0087     for (auto i = argsStart; i < expr.size() - 2; ++i) {
0088         feList.append(FilterExpression(expr.at(i), p));
0089     }
0090 
0091     const auto resultName = expr.last();
0092 
0093     return new I18npVarNode(sourceText, pluralText, feList, resultName);
0094 }
0095 
0096 I18npNode::I18npNode(const QString &sourceText, const QString &pluralText, const QList<KTextTemplate::FilterExpression> &feList, QObject *parent)
0097     : Node(parent)
0098     , m_sourceText(sourceText)
0099     , m_pluralText(pluralText)
0100     , m_filterExpressionList(feList)
0101 {
0102 }
0103 
0104 void I18npNode::render(OutputStream *stream, Context *c) const
0105 {
0106     QVariantList args;
0107     for (const FilterExpression &fe : m_filterExpressionList)
0108         args.append(fe.resolve(c));
0109     const auto resultString = c->localizer()->localizePluralString(m_sourceText, m_pluralText, args);
0110 
0111     streamValueInContext(stream, resultString, c);
0112 }
0113 
0114 I18npVarNode::I18npVarNode(const QString &sourceText,
0115                            const QString &pluralText,
0116                            const QList<KTextTemplate::FilterExpression> &feList,
0117                            const QString &resultName,
0118                            QObject *parent)
0119     : Node(parent)
0120     , m_sourceText(sourceText)
0121     , m_pluralText(pluralText)
0122     , m_filterExpressionList(feList)
0123     , m_resultName(resultName)
0124 {
0125 }
0126 
0127 void I18npVarNode::render(OutputStream *stream, Context *c) const
0128 {
0129     Q_UNUSED(stream)
0130     QVariantList args;
0131     for (const FilterExpression &fe : m_filterExpressionList)
0132         args.append(fe.resolve(c));
0133     const auto resultString = c->localizer()->localizePluralString(m_sourceText, m_pluralText, args);
0134 
0135     c->insert(m_resultName, resultString);
0136 }
0137 
0138 #include "moc_i18np.cpp"