Cutelee 6.1.0
with_locale.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.1 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 Lesser 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 "with_locale.h"
22
23#include "../lib/exception.h"
24#include "parser.h"
25#include <util.h>
26
27#include <QDebug>
28#include <abstractlocalizer.h>
29
30WithLocaleNodeFactory::WithLocaleNodeFactory() {}
31
33{
34 auto expr = smartSplit(tagContent);
35
36 if (expr.size() != 2) {
38 TagSyntaxError,
39 QStringLiteral(
40 "%1 expected format is for example 'with_locale \"de_DE\"'")
41 .arg(expr.first()));
42 }
43
44 FilterExpression fe(expr.at(1), p);
45
46 auto n = new WithLocaleNode(fe, p);
47 auto nodeList = p->parse(n, QStringLiteral("endwith_locale"));
48 n->setNodeList(nodeList);
49 p->removeNextToken();
50
51 return n;
52}
53
54WithLocaleNode::WithLocaleNode(const FilterExpression &localeName,
55 QObject *parent)
56 : Node(parent), m_localeName(localeName)
57{
58}
59
60void WithLocaleNode::setNodeList(const NodeList &nodeList)
61{
62 m_list = nodeList;
63}
64
66{
67 const QString name = Cutelee::getSafeString(m_localeName.resolve(c)).get();
68
69 c->push();
70 c->localizer()->pushLocale(name);
71 m_list.render(stream, c);
72 c->localizer()->popLocale();
73 c->pop();
74}
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
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
A list of Nodes with some convenience API for rendering them.
Definition node.h:148
void render(OutputStream *stream, Context *c) const
Definition node.cpp:177
Base class for all nodes.
Definition node.h:78
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
NodeList parse(Node *parent, const QStringList &stopAt={})
Definition parser.cpp:180
void removeNextToken()
Definition parser.cpp:297
const NestedString & get() const
Definition safestring.h:340
Node * getNode(const QString &tagContent, Parser *p) const override
void render(OutputStream *stream, Context *c) const override
Cutelee::SafeString getSafeString(const QVariant &input)
Definition util.cpp:108
Utility functions used throughout Cutelee.