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.1-or-later
0007 
0008 */
0009 
0010 #include "with_locale.h"
0011 
0012 #include "../lib/exception.h"
0013 #include "parser.h"
0014 #include <util.h>
0015 
0016 #include <QDebug>
0017 #include <abstractlocalizer.h>
0018 
0019 WithLocaleNodeFactory::WithLocaleNodeFactory() = default;
0020 
0021 Node *WithLocaleNodeFactory::getNode(const QString &tagContent, Parser *p) const
0022 {
0023     auto expr = smartSplit(tagContent);
0024 
0025     if (expr.size() != 2) {
0026         throw KTextTemplate::Exception(TagSyntaxError, QStringLiteral("%1 expected format is for example 'with_locale \"de_DE\"'").arg(expr.first()));
0027     }
0028 
0029     FilterExpression fe(expr.at(1), p);
0030 
0031     auto n = new WithLocaleNode(fe, p);
0032     const auto nodeList = p->parse(n, QStringLiteral("endwith_locale"));
0033     n->setNodeList(nodeList);
0034     p->removeNextToken();
0035 
0036     return n;
0037 }
0038 
0039 WithLocaleNode::WithLocaleNode(const FilterExpression &localeName, QObject *parent)
0040     : Node(parent)
0041     , m_localeName(localeName)
0042 {
0043 }
0044 
0045 void WithLocaleNode::setNodeList(const NodeList &nodeList)
0046 {
0047     m_list = nodeList;
0048 }
0049 
0050 void WithLocaleNode::render(OutputStream *stream, Context *c) const
0051 {
0052     const QString name = KTextTemplate::getSafeString(m_localeName.resolve(c)).get();
0053 
0054     c->push();
0055     c->localizer()->pushLocale(name);
0056     m_list.render(stream, c);
0057     c->localizer()->popLocale();
0058     c->pop();
0059 }
0060 
0061 #include "moc_with_locale.cpp"