23 #include <QtCore/QStringList>
25 #include "abstractlocalizer.h"
27 #include "exception.h"
31 #include <QtCore/QDebug>
35 I18npNodeFactory::I18npNodeFactory() {}
44 QStringLiteral(
"Error: i18np tag takes at least two arguments"));
46 auto sourceText = expr.at(1);
48 if (!(sourceText.startsWith(QLatin1Char(
'"'))
49 && sourceText.endsWith(QLatin1Char(
'"')))
50 && !(sourceText.startsWith(QLatin1Char(
'\''))
51 && sourceText.endsWith(QLatin1Char(
'\'')))) {
55 "Error: i18np tag first argument must be a static string."));
57 sourceText = sourceText.mid(1, sourceText.size() - 2);
59 auto pluralText = expr.at(2);
62 if (!(pluralText.startsWith(QLatin1Char(
'"'))
63 && pluralText.endsWith(QLatin1Char(
'"')))
64 && !(pluralText.startsWith(QLatin1Char(
'\''))
65 && pluralText.endsWith(QLatin1Char(
'\'')))) {
67 pluralText = sourceText;
69 pluralText = pluralText.mid(1, pluralText.size() - 2);
72 QList<FilterExpression> feList;
73 for (
auto i = argsStart; i < expr.size(); ++i) {
77 return new I18npNode(sourceText, pluralText, feList);
80 I18npVarNodeFactory::I18npVarNodeFactory() {}
90 QStringLiteral(
"Error: i18np_var tag takes at least four arguments"));
92 auto sourceText = expr.at(1);
94 if (!(sourceText.startsWith(QLatin1Char(
'"'))
95 && sourceText.endsWith(QLatin1Char(
'"')))
96 && !(sourceText.startsWith(QLatin1Char(
'\''))
97 && sourceText.endsWith(QLatin1Char(
'\'')))) {
101 "Error: i18np tag first argument must be a static string."));
103 sourceText = sourceText.mid(1, sourceText.size() - 2);
105 auto pluralText = expr.at(2);
108 if (!(pluralText.startsWith(QLatin1Char(
'"'))
109 && pluralText.endsWith(QLatin1Char(
'"')))
110 && !(pluralText.startsWith(QLatin1Char(
'\''))
111 && pluralText.endsWith(QLatin1Char(
'\'')))) {
113 pluralText = sourceText;
115 pluralText = pluralText.mid(1, pluralText.size() - 2);
118 QList<FilterExpression> feList;
119 for (
auto i = argsStart; i < expr.size() - 2; ++i) {
123 auto resultName = expr.last();
125 return new I18npVarNode(sourceText, pluralText, feList, resultName);
128 I18npNode::I18npNode(
const QString &sourceText,
const QString &pluralText,
129 const QList<Cutelee::FilterExpression> &feList,
131 :
Node(parent), m_sourceText(sourceText), m_pluralText(pluralText),
132 m_filterExpressionList(feList)
142 = c->
localizer()->localizePluralString(m_sourceText, m_pluralText, args);
147 I18npVarNode::I18npVarNode(
const QString &sourceText,
const QString &pluralText,
148 const QList<Cutelee::FilterExpression> &feList,
149 const QString &resultName, QObject *parent)
150 :
Node(parent), m_sourceText(sourceText), m_pluralText(pluralText),
151 m_filterExpressionList(feList), m_resultName(resultName)
162 = c->
localizer()->localizePluralString(m_sourceText, m_pluralText, args);
164 c->
insert(m_resultName, resultString);
Q_INVOKABLE QStringList smartSplit(const QString &str) const
The Context class holds the context to render a Template with.
std::shared_ptr< AbstractLocalizer > localizer() const
void insert(const QString &name, QObject *object)
An exception for use when implementing template tags.
A FilterExpression object represents a filter expression in a template.
QVariant resolve(OutputStream *stream, Context *c) const
Base class for all nodes.
void streamValueInContext(OutputStream *stream, const QVariant &input, Cutelee::Context *c) const
The OutputStream class is used to render templates to a QTextStream.
The Parser class processes a string template into a tree of nodes.
Node * getNode(const QString &tagContent, Parser *p) const override
void render(OutputStream *stream, Context *c) const override
Node * getNode(const QString &tagContent, Parser *p) const override
void render(OutputStream *stream, Context *c) const override
Utility functions used throughout Cutelee.