Cutelee 6.1.0
i18n.cpp
1/*
2 This file is part of the Cutelee template system.
3
4 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either version
9 2 of the Licence, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "i18n.h"
22
23#include <QtCore/QStringList>
24
25#include "abstractlocalizer.h"
26#include "engine.h"
27#include "exception.h"
28#include "parser.h"
29#include "template.h"
30
31#include <QtCore/QDebug>
32#include <complex>
33#include <util.h>
34
35I18nNodeFactory::I18nNodeFactory() {}
36
37Node *I18nNodeFactory::getNode(const QString &tagContent, Parser *p) const
38{
39 auto expr = smartSplit(tagContent);
40
41 if (expr.size() < 2)
43 TagSyntaxError,
44 QStringLiteral("Error: i18n tag takes at least one argument"));
45
46 auto sourceText = expr.at(1);
47 auto size = sourceText.size();
48
49 if (!(sourceText.startsWith(QLatin1Char('"'))
50 && sourceText.endsWith(QLatin1Char('"')))
51 && !(sourceText.startsWith(QLatin1Char('\''))
52 && sourceText.endsWith(QLatin1Char('\'')))) {
54 TagSyntaxError,
55 QStringLiteral(
56 "Error: i18n tag first argument must be a static string."));
57 }
58 sourceText = sourceText.mid(1, size - 2);
59
60 QList<FilterExpression> feList;
61 for (auto i = 2; i < expr.size(); ++i) {
62 feList.append(FilterExpression(expr.at(i), p));
63 }
64
65 return new I18nNode(sourceText, feList);
66}
67
68I18nVarNodeFactory::I18nVarNodeFactory() {}
69
71 Parser *p) const
72{
73
74 auto expr = smartSplit(tagContent);
75
76 if (expr.size() < 4)
78 TagSyntaxError,
79 QStringLiteral("Error: i18n_var tag takes at least three arguments"));
80
81 auto sourceText = expr.at(1);
82 auto size = sourceText.size();
83
84 if (!(sourceText.startsWith(QLatin1Char('"'))
85 && sourceText.endsWith(QLatin1Char('"')))
86 && !(sourceText.startsWith(QLatin1Char('\''))
87 && sourceText.endsWith(QLatin1Char('\'')))) {
89 TagSyntaxError,
90 QStringLiteral(
91 "Error: i18n tag first argument must be a static string."));
92 }
93 sourceText = sourceText.mid(1, size - 2);
94
95 QList<FilterExpression> feList;
96 for (auto i = 2; i < expr.size() - 2; ++i) {
97 feList.append(FilterExpression(expr.at(i), p));
98 }
99
100 auto resultName = expr.last();
101
102 return new I18nVarNode(sourceText, feList, resultName);
103}
104
105I18nNode::I18nNode(const QString &sourceText,
106 const QList<Cutelee::FilterExpression> &feList,
107 QObject *parent)
108 : Node(parent), m_sourceText(sourceText), m_filterExpressionList(feList)
109{
110}
111
112void I18nNode::render(OutputStream *stream, Context *c) const
113{
114 QVariantList args;
115 Q_FOREACH (const FilterExpression &fe, m_filterExpressionList)
116 args.append(fe.resolve(c));
117 const auto resultString = c->localizer()->localizeString(m_sourceText, args);
118
119 streamValueInContext(stream, resultString, c);
120}
121
122I18nVarNode::I18nVarNode(const QString &sourceText,
123 const QList<Cutelee::FilterExpression> &feList,
124 const QString &resultName, QObject *parent)
125 : Node(parent), m_sourceText(sourceText), m_filterExpressionList(feList),
126 m_resultName(resultName)
127{
128}
129
131{
132 Q_UNUSED(stream)
133 QVariantList args;
134 Q_FOREACH (const FilterExpression &fe, m_filterExpressionList)
135 args.append(fe.resolve(c));
136 auto resultString = c->localizer()->localizeString(m_sourceText, args);
137
138 c->insert(m_resultName, resultString);
139}
Q_INVOKABLE QStringList smartSplit(const QString &str) const
Definition node.cpp:202
The Context class holds the context to render a Template with.
Definition context.h:119
std::shared_ptr< AbstractLocalizer > localizer() const
Definition context.cpp:230
void insert(const QString &name, QObject *object)
Definition context.cpp:145
An exception for use when implementing template tags.
Definition exception.h:85
A FilterExpression object represents a filter expression in a template.
QVariant resolve(OutputStream *stream, Context *c) const
Base class for all nodes.
Definition node.h:78
void streamValueInContext(OutputStream *stream, const QVariant &input, Cutelee::Context *c) const
Definition node.cpp:88
The OutputStream class is used to render templates to a QTextStream.
The Parser class processes a string template into a tree of nodes.
Definition parser.h:49
Node * getNode(const QString &tagContent, Parser *p) const override
Definition i18n.cpp:37
void render(OutputStream *stream, Context *c) const override
Definition i18n.cpp:112
Node * getNode(const QString &tagContent, Parser *p) const override
Definition i18n.cpp:70
void render(OutputStream *stream, Context *c) const override
Definition i18n.cpp:130
Utility functions used throughout Cutelee.