Cutelee  6.1.0
i18nc.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 "i18nc.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 
35 I18ncNodeFactory::I18ncNodeFactory() {}
36 
37 Node *I18ncNodeFactory::getNode(const QString &tagContent, Parser *p) const
38 {
39  auto expr = smartSplit(tagContent);
40 
41  if (expr.size() < 3)
42  throw Cutelee::Exception(
43  TagSyntaxError,
44  QStringLiteral("Error: i18nc tag takes at least two arguments"));
45 
46  auto contextText = expr.at(1);
47 
48  if (!(contextText.startsWith(QLatin1Char('"'))
49  && contextText.endsWith(QLatin1Char('"')))
50  && !(contextText.startsWith(QLatin1Char('\''))
51  && contextText.endsWith(QLatin1Char('\'')))) {
52  throw Cutelee::Exception(
53  TagSyntaxError,
54  QStringLiteral(
55  "Error: i18nc tag first argument must be a static string."));
56  }
57  contextText = contextText.mid(1, contextText.size() - 2);
58 
59  auto sourceText = expr.at(2);
60 
61  if (!(sourceText.startsWith(QLatin1Char('"'))
62  && sourceText.endsWith(QLatin1Char('"')))
63  && !(sourceText.startsWith(QLatin1Char('\''))
64  && sourceText.endsWith(QLatin1Char('\'')))) {
65  throw Cutelee::Exception(
66  TagSyntaxError,
67  QStringLiteral(
68  "Error: i18nc tag second argument must be a static string."));
69  }
70  sourceText = sourceText.mid(1, sourceText.size() - 2);
71 
72  QList<FilterExpression> feList;
73  for (auto i = 3; i < expr.size(); ++i) {
74  feList.append(FilterExpression(expr.at(i), p));
75  }
76 
77  return new I18ncNode(sourceText, contextText, feList);
78 }
79 
80 I18ncVarNodeFactory::I18ncVarNodeFactory() {}
81 
83  Parser *p) const
84 {
85  auto expr = smartSplit(tagContent);
86 
87  if (expr.size() < 5)
88  throw Cutelee::Exception(
89  TagSyntaxError,
90  QStringLiteral("Error: i18nc_var tag takes at least four arguments"));
91 
92  auto contextText = expr.at(1);
93 
94  if (!(contextText.startsWith(QLatin1Char('"'))
95  && contextText.endsWith(QLatin1Char('"')))
96  && !(contextText.startsWith(QLatin1Char('\''))
97  && contextText.endsWith(QLatin1Char('\'')))) {
98  throw Cutelee::Exception(
99  TagSyntaxError,
100  QStringLiteral(
101  "Error: i18nc_var tag first argument must be a static string."));
102  }
103  contextText = contextText.mid(1, contextText.size() - 2);
104 
105  auto sourceText = expr.at(2);
106 
107  if (!(sourceText.startsWith(QLatin1Char('"'))
108  && sourceText.endsWith(QLatin1Char('"')))
109  && !(sourceText.startsWith(QLatin1Char('\''))
110  && sourceText.endsWith(QLatin1Char('\'')))) {
111  throw Cutelee::Exception(
112  TagSyntaxError,
113  QStringLiteral(
114  "Error: i18nc_var tag second argument must be a static string."));
115  }
116  sourceText = sourceText.mid(1, sourceText.size() - 2);
117 
118  QList<FilterExpression> feList;
119  for (auto i = 3; i < expr.size() - 2; ++i) {
120  feList.append(FilterExpression(expr.at(i), p));
121  }
122 
123  auto resultName = expr.last();
124 
125  return new I18ncVarNode(sourceText, contextText, feList, resultName);
126 }
127 
128 I18ncNode::I18ncNode(const QString &sourceText, const QString &context,
129  const QList<Cutelee::FilterExpression> &feList,
130  QObject *parent)
131  : Node(parent), m_sourceText(sourceText), m_context(context),
132  m_filterExpressionList(feList)
133 {
134 }
135 
136 void I18ncNode::render(OutputStream *stream, Context *c) const
137 {
138  QVariantList args;
139  Q_FOREACH (const FilterExpression &fe, m_filterExpressionList)
140  args.append(fe.resolve(c));
141  auto resultString
142  = c->localizer()->localizeContextString(m_sourceText, m_context, args);
143 
144  streamValueInContext(stream, resultString, c);
145 }
146 
147 I18ncVarNode::I18ncVarNode(const QString &sourceText, const QString &context,
148  const QList<Cutelee::FilterExpression> &feList,
149  const QString &resultName, QObject *parent)
150  : Node(parent), m_sourceText(sourceText), m_context(context),
151  m_filterExpressionList(feList), m_resultName(resultName)
152 {
153 }
154 
156 {
157  Q_UNUSED(stream)
158  QVariantList args;
159  Q_FOREACH (const FilterExpression &fe, m_filterExpressionList)
160  args.append(fe.resolve(c));
161  auto resultString
162  = c->localizer()->localizeContextString(m_sourceText, m_context, args);
163 
164  c->insert(m_resultName, resultString);
165 }
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.
Definition: outputstream.h:81
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: i18nc.cpp:37
void render(OutputStream *stream, Context *c) const override
Definition: i18nc.cpp:136
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: i18nc.cpp:82
void render(OutputStream *stream, Context *c) const override
Definition: i18nc.cpp:155
Utility functions used throughout Cutelee.