File indexing completed on 2025-02-09 04:28:35
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 "i18n.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 I18nNodeFactory::I18nNodeFactory() = default; 0023 0024 Node *I18nNodeFactory::getNode(const QString &tagContent, Parser *p) const 0025 { 0026 const auto expr = smartSplit(tagContent); 0027 0028 if (expr.size() < 2) 0029 throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18n tag takes at least one argument")); 0030 0031 auto sourceText = expr.at(1); 0032 const auto size = sourceText.size(); 0033 0034 if (!(sourceText.startsWith(QLatin1Char('"')) && sourceText.endsWith(QLatin1Char('"'))) 0035 && !(sourceText.startsWith(QLatin1Char('\'')) && sourceText.endsWith(QLatin1Char('\'')))) { 0036 throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18n tag first argument must be a static string.")); 0037 } 0038 sourceText = sourceText.mid(1, size - 2); 0039 0040 QList<FilterExpression> feList; 0041 for (auto i = 2; i < expr.size(); ++i) { 0042 feList.append(FilterExpression(expr.at(i), p)); 0043 } 0044 0045 return new I18nNode(sourceText, feList); 0046 } 0047 0048 I18nVarNodeFactory::I18nVarNodeFactory() = default; 0049 0050 KTextTemplate::Node *I18nVarNodeFactory::getNode(const QString &tagContent, Parser *p) const 0051 { 0052 auto expr = smartSplit(tagContent); 0053 0054 if (expr.size() < 4) 0055 throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18n_var tag takes at least three arguments")); 0056 0057 auto sourceText = expr.at(1); 0058 const auto size = sourceText.size(); 0059 0060 if (!(sourceText.startsWith(QLatin1Char('"')) && sourceText.endsWith(QLatin1Char('"'))) 0061 && !(sourceText.startsWith(QLatin1Char('\'')) && sourceText.endsWith(QLatin1Char('\'')))) { 0062 throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("Error: i18n tag first argument must be a static string.")); 0063 } 0064 sourceText = sourceText.mid(1, size - 2); 0065 0066 QList<FilterExpression> feList; 0067 for (auto i = 2; i < expr.size() - 2; ++i) { 0068 feList.append(FilterExpression(expr.at(i), p)); 0069 } 0070 0071 auto resultName = expr.last(); 0072 0073 return new I18nVarNode(sourceText, feList, resultName); 0074 } 0075 0076 I18nNode::I18nNode(const QString &sourceText, const QList<KTextTemplate::FilterExpression> &feList, QObject *parent) 0077 : Node(parent) 0078 , m_sourceText(sourceText) 0079 , m_filterExpressionList(feList) 0080 { 0081 } 0082 0083 void I18nNode::render(OutputStream *stream, Context *c) const 0084 { 0085 QVariantList args; 0086 for (const FilterExpression &fe : m_filterExpressionList) 0087 args.append(fe.resolve(c)); 0088 const auto resultString = c->localizer()->localizeString(m_sourceText, args); 0089 0090 streamValueInContext(stream, resultString, c); 0091 } 0092 0093 I18nVarNode::I18nVarNode(const QString &sourceText, const QList<KTextTemplate::FilterExpression> &feList, const QString &resultName, QObject *parent) 0094 : Node(parent) 0095 , m_sourceText(sourceText) 0096 , m_filterExpressionList(feList) 0097 , m_resultName(resultName) 0098 { 0099 } 0100 0101 void I18nVarNode::render(OutputStream *stream, Context *c) const 0102 { 0103 Q_UNUSED(stream) 0104 QVariantList args; 0105 for (const FilterExpression &fe : m_filterExpressionList) 0106 args.append(fe.resolve(c)); 0107 const auto resultString = c->localizer()->localizeString(m_sourceText, args); 0108 0109 c->insert(m_resultName, resultString); 0110 } 0111 0112 #include "moc_i18n.cpp"