Cutelee  6.1.0
i18np.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 "i18np.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 I18npNodeFactory::I18npNodeFactory() {}
36 
37 Node *I18npNodeFactory::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: i18np tag takes at least two arguments"));
45 
46  auto sourceText = expr.at(1);
47 
48  if (!(sourceText.startsWith(QLatin1Char('"'))
49  && sourceText.endsWith(QLatin1Char('"')))
50  && !(sourceText.startsWith(QLatin1Char('\''))
51  && sourceText.endsWith(QLatin1Char('\'')))) {
52  throw Cutelee::Exception(
53  TagSyntaxError,
54  QStringLiteral(
55  "Error: i18np tag first argument must be a static string."));
56  }
57  sourceText = sourceText.mid(1, sourceText.size() - 2);
58 
59  auto pluralText = expr.at(2);
60 
61  auto argsStart = 3;
62  if (!(pluralText.startsWith(QLatin1Char('"'))
63  && pluralText.endsWith(QLatin1Char('"')))
64  && !(pluralText.startsWith(QLatin1Char('\''))
65  && pluralText.endsWith(QLatin1Char('\'')))) {
66  argsStart = 2;
67  pluralText = sourceText;
68  } else {
69  pluralText = pluralText.mid(1, pluralText.size() - 2);
70  }
71 
72  QList<FilterExpression> feList;
73  for (auto i = argsStart; i < expr.size(); ++i) {
74  feList.append(FilterExpression(expr.at(i), p));
75  }
76 
77  return new I18npNode(sourceText, pluralText, feList);
78 }
79 
80 I18npVarNodeFactory::I18npVarNodeFactory() {}
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: i18np_var tag takes at least four arguments"));
91 
92  auto sourceText = expr.at(1);
93 
94  if (!(sourceText.startsWith(QLatin1Char('"'))
95  && sourceText.endsWith(QLatin1Char('"')))
96  && !(sourceText.startsWith(QLatin1Char('\''))
97  && sourceText.endsWith(QLatin1Char('\'')))) {
98  throw Cutelee::Exception(
99  TagSyntaxError,
100  QStringLiteral(
101  "Error: i18np tag first argument must be a static string."));
102  }
103  sourceText = sourceText.mid(1, sourceText.size() - 2);
104 
105  auto pluralText = expr.at(2);
106 
107  auto argsStart = 3;
108  if (!(pluralText.startsWith(QLatin1Char('"'))
109  && pluralText.endsWith(QLatin1Char('"')))
110  && !(pluralText.startsWith(QLatin1Char('\''))
111  && pluralText.endsWith(QLatin1Char('\'')))) {
112  argsStart = 2;
113  pluralText = sourceText;
114  } else {
115  pluralText = pluralText.mid(1, pluralText.size() - 2);
116  }
117 
118  QList<FilterExpression> feList;
119  for (auto i = argsStart; i < expr.size() - 2; ++i) {
120  feList.append(FilterExpression(expr.at(i), p));
121  }
122 
123  auto resultName = expr.last();
124 
125  return new I18npVarNode(sourceText, pluralText, feList, resultName);
126 }
127 
128 I18npNode::I18npNode(const QString &sourceText, const QString &pluralText,
129  const QList<Cutelee::FilterExpression> &feList,
130  QObject *parent)
131  : Node(parent), m_sourceText(sourceText), m_pluralText(pluralText),
132  m_filterExpressionList(feList)
133 {
134 }
135 
136 void I18npNode::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()->localizePluralString(m_sourceText, m_pluralText, args);
143 
144  streamValueInContext(stream, resultString, c);
145 }
146 
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)
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()->localizePluralString(m_sourceText, m_pluralText, 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: i18np.cpp:37
void render(OutputStream *stream, Context *c) const override
Definition: i18np.cpp:136
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: i18np.cpp:82
void render(OutputStream *stream, Context *c) const override
Definition: i18np.cpp:155
Utility functions used throughout Cutelee.