2
This file is part of the Grantlee template system.
4
Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
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.
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.
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/>.
23
#include <QtCore/QStringList>
26
#include "abstractlocalizer.h"
30
#include "exception.h"
32
#include <QtCore/QDebug>
36
I18ncNodeFactory::I18ncNodeFactory()
41
Node* I18ncNodeFactory::getNode( const QString& tagContent, Parser* p ) const
43
QStringList expr = smartSplit( tagContent );
45
if ( expr.size() < 3 )
46
throw Grantlee::Exception( TagSyntaxError, QLatin1String( "Error: i18nc tag takes at least two arguments" ) );
48
QString contextText = expr.at( 1 );
50
if ( !( contextText.startsWith( QLatin1Char( '"' ) ) && contextText.endsWith( QLatin1Char( '"' ) ) )
51
&& !( contextText.startsWith( QLatin1Char( '\'' ) ) && contextText.endsWith( QLatin1Char( '\'' ) ) ) ) {
52
throw Grantlee::Exception( TagSyntaxError, QLatin1String( "Error: i18nc tag first argument must be a static string." ) );
54
contextText = contextText.mid( 1, contextText.size() - 2 );
56
QString sourceText = expr.at( 2 );
58
if ( !( sourceText.startsWith( QLatin1Char( '"' ) ) && sourceText.endsWith( QLatin1Char( '"' ) ) )
59
&& !( sourceText.startsWith( QLatin1Char( '\'' ) ) && sourceText.endsWith( QLatin1Char( '\'' ) ) ) ) {
60
throw Grantlee::Exception( TagSyntaxError, QLatin1String( "Error: i18nc tag second argument must be a static string." ) );
62
sourceText = sourceText.mid( 1, sourceText.size() - 2 );
64
QList<FilterExpression> feList;
65
for ( int i = 3; i < expr.size(); ++i ) {
66
feList.append( FilterExpression( expr.at( i ), p ) );
69
return new I18ncNode( sourceText, contextText, feList );
73
I18ncVarNodeFactory::I18ncVarNodeFactory()
78
Grantlee::Node* I18ncVarNodeFactory::getNode( const QString& tagContent, Parser* p ) const
80
QStringList expr = smartSplit( tagContent );
82
if ( expr.size() < 5 )
83
throw Grantlee::Exception( TagSyntaxError, QLatin1String( "Error: i18nc_var tag takes at least four arguments" ) );
85
QString contextText = expr.at( 1 );
87
if ( !( contextText.startsWith( QLatin1Char( '"' ) ) && contextText.endsWith( QLatin1Char( '"' ) ) )
88
&& !( contextText.startsWith( QLatin1Char( '\'' ) ) && contextText.endsWith( QLatin1Char( '\'' ) ) ) ) {
89
throw Grantlee::Exception( TagSyntaxError, QLatin1String( "Error: i18nc_var tag first argument must be a static string." ) );
91
contextText = contextText.mid( 1, contextText.size() - 2 );
93
QString sourceText = expr.at( 2 );
95
if ( !( sourceText.startsWith( QLatin1Char( '"' ) ) && sourceText.endsWith( QLatin1Char( '"' ) ) )
96
&& !( sourceText.startsWith( QLatin1Char( '\'' ) ) && sourceText.endsWith( QLatin1Char( '\'' ) ) ) ) {
97
throw Grantlee::Exception( TagSyntaxError, QLatin1String( "Error: i18nc_var tag second argument must be a static string." ) );
99
sourceText = sourceText.mid( 1, sourceText.size() - 2 );
101
QList<FilterExpression> feList;
102
for ( int i = 3; i < expr.size() - 2; ++i ) {
103
feList.append( FilterExpression( expr.at( i ), p ) );
106
QString resultName = expr.last();
108
return new I18ncVarNode( sourceText, contextText, feList, resultName );
112
I18ncNode::I18ncNode( const QString &sourceText, const QString &context, const QList<Grantlee::FilterExpression>& feList, QObject* parent )
113
: Node( parent ), m_sourceText( sourceText ), m_context( context ), m_filterExpressionList( feList )
118
void I18ncNode::render( OutputStream* stream, Context* c )
121
Q_FOREACH( const FilterExpression &fe, m_filterExpressionList )
122
args.append( fe.resolve( c ) );
123
QString resultString = c->localizer()->localizeContextString( m_sourceText, m_context, args );
125
streamValueInContext( stream, resultString, c );
128
I18ncVarNode::I18ncVarNode( const QString &sourceText, const QString &context, const QList<Grantlee::FilterExpression>& feList, const QString &resultName, QObject* parent )
129
: Node( parent ), m_sourceText( sourceText ), m_context( context ), m_filterExpressionList( feList ), m_resultName( resultName )
134
void I18ncVarNode::render( OutputStream* stream, Context* c )
138
Q_FOREACH( const FilterExpression &fe, m_filterExpressionList )
139
args.append( fe.resolve( c ) );
140
QString resultString = c->localizer()->localizeContextString( m_sourceText, m_context, args );
142
c->insert( m_resultName, resultString );