~ubuntu-branches/ubuntu/trusty/grantlee/trusty

« back to all changes in this revision

Viewing changes to templates/i18n/i18nc.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-08 22:46:54 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111208224654-n2vxap05cxp0i2v7
Tags: 0.2.0-0ubuntu1
* New upstream release
* Merge with Debian, remaining changes:
 - conflict/replace old libgrantlee0
* Remove missing symbols from .symbols files, upstream says they are no longer
  all virtual but they are inline

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee 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
#include <QtPlugin>
 
25
 
 
26
#include "abstractlocalizer.h"
 
27
#include "parser.h"
 
28
#include "template.h"
 
29
#include "engine.h"
 
30
#include "exception.h"
 
31
 
 
32
#include <QtCore/QDebug>
 
33
#include <util.h>
 
34
#include <complex>
 
35
 
 
36
I18ncNodeFactory::I18ncNodeFactory()
 
37
{
 
38
 
 
39
}
 
40
 
 
41
Node* I18ncNodeFactory::getNode( const QString& tagContent, Parser* p ) const
 
42
{
 
43
  QStringList expr = smartSplit( tagContent );
 
44
 
 
45
  if ( expr.size() < 3 )
 
46
    throw Grantlee::Exception( TagSyntaxError, QLatin1String( "Error: i18nc tag takes at least two arguments" ) );
 
47
 
 
48
  QString contextText = expr.at( 1 );
 
49
 
 
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." ) );
 
53
  }
 
54
  contextText = contextText.mid( 1, contextText.size() - 2 );
 
55
 
 
56
  QString sourceText = expr.at( 2 );
 
57
 
 
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." ) );
 
61
  }
 
62
  sourceText = sourceText.mid( 1, sourceText.size() - 2 );
 
63
 
 
64
  QList<FilterExpression> feList;
 
65
  for ( int i = 3; i < expr.size(); ++i ) {
 
66
    feList.append( FilterExpression( expr.at( i ), p ) );
 
67
  }
 
68
 
 
69
  return new I18ncNode( sourceText, contextText, feList );
 
70
}
 
71
 
 
72
 
 
73
I18ncVarNodeFactory::I18ncVarNodeFactory()
 
74
{
 
75
 
 
76
}
 
77
 
 
78
Grantlee::Node* I18ncVarNodeFactory::getNode( const QString& tagContent, Parser* p ) const
 
79
{
 
80
  QStringList expr = smartSplit( tagContent );
 
81
 
 
82
  if ( expr.size() < 5 )
 
83
    throw Grantlee::Exception( TagSyntaxError, QLatin1String( "Error: i18nc_var tag takes at least four arguments" ) );
 
84
 
 
85
  QString contextText = expr.at( 1 );
 
86
 
 
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." ) );
 
90
  }
 
91
  contextText = contextText.mid( 1, contextText.size() - 2 );
 
92
 
 
93
  QString sourceText = expr.at( 2 );
 
94
 
 
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." ) );
 
98
  }
 
99
  sourceText = sourceText.mid( 1, sourceText.size() - 2 );
 
100
 
 
101
  QList<FilterExpression> feList;
 
102
  for ( int i = 3; i < expr.size() - 2; ++i ) {
 
103
    feList.append( FilterExpression( expr.at( i ), p ) );
 
104
  }
 
105
 
 
106
  QString resultName = expr.last();
 
107
 
 
108
  return new I18ncVarNode( sourceText, contextText, feList, resultName );
 
109
}
 
110
 
 
111
 
 
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 )
 
114
{
 
115
 
 
116
}
 
117
 
 
118
void I18ncNode::render( OutputStream* stream, Context* c )
 
119
{
 
120
  QVariantList args;
 
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 );
 
124
 
 
125
  streamValueInContext( stream, resultString, c );
 
126
}
 
127
 
 
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 )
 
130
{
 
131
 
 
132
}
 
133
 
 
134
void I18ncVarNode::render( OutputStream* stream, Context* c )
 
135
{
 
136
  Q_UNUSED( stream )
 
137
  QVariantList args;
 
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 );
 
141
 
 
142
  c->insert( m_resultName, resultString );
 
143
}