[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 // 2 // Finds i18n data from tpl files 3 // 4 // This file is based on fetchtr.cpp from lupdate/Qt Linguist, 5 // which is Copyright (C) 2000 Trolltech AS (www.trolltech.com). 6 // 7 // Gunnstein Lye <gl@ez.no> 8 // Created on: <10-Dec-2002 18:46:17 gl> 9 // 10 // Copyright (C) 1999-2006 eZ systems as. All rights reserved. 11 // 12 // This program is free software; you can redistribute it and/or 13 // modify it under the terms of the GNU General Public License 14 // as published by the Free Software Foundation; either version 2 15 // of the License, or (at your option) any later version. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of the GNU General Public License 23 // along with this program; if not, write to the Free Software 24 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 25 // 26 // The GNU General Public License is also available online at: 27 // 28 // http://www.gnu.org/copyleft/gpl.html 29 // 30 31 #include <qfile.h> 32 #include <qfileinfo.h> 33 #include <qtextstream.h> 34 #include <qregexp.h> 35 36 #include <metatranslator.h> 37 38 39 static QTextStream stream; 40 41 static QString getString( QString content, int pos, bool reverse, int &endpos ) 42 { 43 int tpos = pos; 44 int qpos = pos; 45 bool found = false; 46 QChar quote; 47 endpos = -1; 48 do 49 { 50 quote = content[tpos]; 51 if ( reverse ) 52 --tpos; 53 else 54 ++tpos; 55 } while ( ( quote == ' ' || 56 quote == '\t' || 57 quote == '\r' || 58 quote == '\n' ) && 59 tpos >= 0 && 60 tpos < (int)content.length() ); 61 if ( tpos < 0 || 62 tpos >= (int)content.length() ) 63 return QString::null; 64 if ( quote != '\'' && 65 quote != '"' ) 66 return QString::null; 67 qpos = tpos; 68 69 while ( !found ) 70 { 71 if ( reverse ) 72 tpos = content.findRev( quote, tpos ); 73 else 74 tpos = content.find( quote, tpos ); 75 76 if ( content[tpos-1] != QChar( '\\' ) ) 77 found = true; 78 79 if ( tpos == -1 ) 80 { 81 qWarning( "lupdate error: end quote not found" ); 82 return QString::null; 83 } 84 if ( !found ) 85 { 86 if ( reverse ) 87 --tpos; 88 else 89 ++tpos; 90 } 91 } 92 93 QString str; 94 if ( reverse ) 95 str = content.mid( tpos + 1, qpos - tpos ); 96 else 97 str = content.mid( qpos, tpos - qpos ); 98 str = str.replace( "\\'", "'" ); 99 str = str.replace( "\\\"", "\"" ); 100 endpos = tpos; 101 if ( reverse ) 102 --endpos; 103 else 104 ++endpos; 105 return str; 106 } 107 108 static void skipComma( const QString &content, int pos, int &endpos ) 109 { 110 QChar c; 111 endpos = -1; 112 do 113 { 114 c = content[pos]; 115 ++pos; 116 } while( pos < (int)content.length() && 117 ( c == ' ' || 118 c == '\t' || 119 c == '\r' || 120 c == '\n' ) ); 121 if ( c == ',' ) 122 endpos = pos; 123 return; 124 } 125 126 static void parse( MetaTranslator *tor, const QString &filename ) 127 { 128 QRegExp i18nRE( "\\|[ \t\r\n]*[xi]18n[ \t\r\n]*\\(" ); 129 QString content = stream.read(); 130 QString context, source, comment; 131 int startpos, pos = 0; 132 int endpos; 133 134 while ( pos >= 0 ) 135 { 136 source = QString::null; 137 context = QString::null; 138 comment = QString::null; 139 startpos = i18nRE.search( content, pos ); 140 pos = startpos + i18nRE.matchedLength(); 141 if ( pos < 0 ) 142 return; 143 144 source = getString( content, startpos - 1, true, endpos ); 145 if ( source.isNull() ) 146 { 147 qWarning( filename + ":error: Found non-quoted source, skipping translation" ); 148 exit( 1 ); 149 // continue; 150 } 151 152 if ( content[startpos+1] == 'x' ) 153 { 154 QString ext = getString( content, pos, false, endpos ); 155 if ( endpos < 0 ) 156 continue; 157 pos = endpos; 158 skipComma( content, pos, endpos ); 159 if ( endpos < 0 ) 160 continue; 161 pos = endpos; 162 } 163 context = getString( content, pos, false, endpos ); 164 if ( endpos < 0 ) 165 { 166 qWarning( filename + ":error: Found non-quoted context, skipping translation" ); 167 exit( 1 ); 168 // continue; 169 } 170 if ( context.find( '\n' ) != -1 ) 171 { 172 qWarning( filename + ":error: The context contains newlines, please correct the template.\nThe context was:\n" + context ); 173 exit( 1 ); 174 } 175 pos = endpos; 176 skipComma( content, pos, endpos ); 177 if ( endpos >= 0 ) 178 { 179 pos = endpos; 180 181 comment = getString( content, pos, false, endpos ); 182 if ( endpos >= 0 && 183 comment.length() == 0 ) 184 comment = QString::null; 185 } 186 187 if ( context.isNull() ) 188 continue; 189 190 tor->insert( MetaTranslatorMessage( context.latin1(), source.latin1(), comment, 191 QString::null, false ) ); 192 } 193 } 194 195 void fetchtr_tpl( QFileInfo *fi, MetaTranslator *tor, bool mustExist, bool assumeUTF8 ) 196 { 197 QFile file( fi->filePath() ); 198 if ( file.open( IO_ReadOnly ) ) 199 { 200 // Hack: Check if the template is utf8 201 QTextStream testStream; 202 testStream.setDevice( &file ); 203 QString testContent = testStream.read(); 204 file.close(); 205 if ( file.open( IO_ReadOnly ) ) 206 { 207 if ( ( testContent.startsWith( "{*?template charset=", false ) && 208 ( testContent.startsWith( "{*?template charset=utf8?*}", false ) || 209 testContent.startsWith( "{*?template charset=utf-8?*}", false ) ) ) || 210 assumeUTF8 ) 211 { 212 stream.setEncoding( QTextStream::UnicodeUTF8 ); 213 } 214 else 215 { 216 stream.setEncoding( QTextStream::Locale ); 217 } 218 } 219 stream.setDevice( &file ); 220 } 221 else if ( mustExist ) 222 { 223 qWarning( "lupdate error: cannot open translation file '%s'", 224 fi->filePath().latin1() ); 225 return; 226 } 227 228 parse( tor, file.name() ); 229 file.close(); 230 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |