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 "i18ncp.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 I18ncpNodeFactory::I18ncpNodeFactory() = default;
0023 
0024 Node *I18ncpNodeFactory::getNode(const QString &tagContent, Parser *p) const
0025 {
0026     auto expr = smartSplit(tagContent);
0027 
0028     if (expr.size() < 4)
0029         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18ncp tag takes at least three 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: i18ncp 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: i18ncp tag second argument must be a static string."));
0044     }
0045     sourceText = sourceText.mid(1, sourceText.size() - 2);
0046 
0047     auto pluralText = expr.at(3);
0048 
0049     auto argsStart = 4;
0050     if (!(pluralText.startsWith(QLatin1Char('"')) && pluralText.endsWith(QLatin1Char('"')))
0051         && !(pluralText.startsWith(QLatin1Char('\'')) && pluralText.endsWith(QLatin1Char('\'')))) {
0052         argsStart = 3;
0053         pluralText = sourceText;
0054     } else {
0055         pluralText = pluralText.mid(1, pluralText.size() - 2);
0056     }
0057 
0058     QList<FilterExpression> feList;
0059     for (auto i = argsStart; i < expr.size(); ++i) {
0060         feList.append(FilterExpression(expr.at(i), p));
0061     }
0062 
0063     return new I18ncpNode(contextText, sourceText, pluralText, feList);
0064 }
0065 
0066 I18ncpVarNodeFactory::I18ncpVarNodeFactory() = default;
0067 
0068 KTextTemplate::Node *I18ncpVarNodeFactory::getNode(const QString &tagContent, Parser *p) const
0069 {
0070     auto expr = smartSplit(tagContent);
0071 
0072     if (expr.size() < 6) {
0073         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18ncp_var tag takes at least five arguments"));
0074     }
0075 
0076     auto contextText = expr.at(1);
0077 
0078     if (!(contextText.startsWith(QLatin1Char('"')) && contextText.endsWith(QLatin1Char('"')))
0079         && !(contextText.startsWith(QLatin1Char('\'')) && contextText.endsWith(QLatin1Char('\'')))) {
0080         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18ncp_var tag first argument must be a static string."));
0081     }
0082     contextText = contextText.mid(1, contextText.size() - 2);
0083 
0084     auto sourceText = expr.at(2);
0085 
0086     if (!(sourceText.startsWith(QLatin1Char('"')) && sourceText.endsWith(QLatin1Char('"')))
0087         && !(sourceText.startsWith(QLatin1Char('\'')) && sourceText.endsWith(QLatin1Char('\'')))) {
0088         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18ncp_var tag second argument must be a static string."));
0089     }
0090     sourceText = sourceText.mid(1, sourceText.size() - 2);
0091 
0092     auto pluralText = expr.at(3);
0093 
0094     auto argsStart = 4;
0095     if (!(pluralText.startsWith(QLatin1Char('"')) && pluralText.endsWith(QLatin1Char('"')))
0096         && !(pluralText.startsWith(QLatin1Char('\'')) && pluralText.endsWith(QLatin1Char('\'')))) {
0097         argsStart = 3;
0098         pluralText = sourceText;
0099     } else {
0100         pluralText = pluralText.mid(1, pluralText.size() - 2);
0101     }
0102 
0103     QList<FilterExpression> feList;
0104     for (auto i = argsStart; i < expr.size() - 2; ++i) {
0105         feList.append(FilterExpression(expr.at(i), p));
0106     }
0107 
0108     const auto resultName = expr.last();
0109 
0110     return new I18ncpVarNode(contextText, sourceText, pluralText, feList, resultName);
0111 }
0112 
0113 I18ncpNode::I18ncpNode(const QString &contextText,
0114                        const QString &sourceText,
0115                        const QString &pluralText,
0116                        const QList<KTextTemplate::FilterExpression> &feList,
0117                        QObject *parent)
0118     : Node(parent)
0119     , m_contextText(contextText)
0120     , m_sourceText(sourceText)
0121     , m_pluralText(pluralText)
0122     , m_filterExpressionList(feList)
0123 {
0124 }
0125 
0126 void I18ncpNode::render(OutputStream *stream, Context *c) const
0127 {
0128     QVariantList args;
0129     for (const FilterExpression &fe : m_filterExpressionList)
0130         args.append(fe.resolve(c));
0131     auto resultString = c->localizer()->localizePluralContextString(m_sourceText, m_pluralText, m_contextText, args);
0132 
0133     streamValueInContext(stream, resultString, c);
0134 }
0135 
0136 I18ncpVarNode::I18ncpVarNode(const QString &contextText,
0137                              const QString &sourceText,
0138                              const QString &pluralText,
0139                              const QList<KTextTemplate::FilterExpression> &feList,
0140                              const QString &resultName,
0141                              QObject *parent)
0142     : Node(parent)
0143     , m_contextText(contextText)
0144     , m_sourceText(sourceText)
0145     , m_pluralText(pluralText)
0146     , m_filterExpressionList(feList)
0147     , m_resultName(resultName)
0148 {
0149 }
0150 
0151 void I18ncpVarNode::render(OutputStream *stream, Context *c) const
0152 {
0153     Q_UNUSED(stream)
0154     QVariantList args;
0155     for (const FilterExpression &fe : m_filterExpressionList)
0156         args.append(fe.resolve(c));
0157     const auto resultString = c->localizer()->localizePluralContextString(m_sourceText, m_pluralText, m_contextText, args);
0158 
0159     c->insert(m_resultName, resultString);
0160 }
0161 
0162 #include "moc_i18ncp.cpp"