PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/support/ezlupdate-qt3/main.cpp

https://github.com/aurelienRT1/ezpublish
C++ | 376 lines | 267 code | 30 blank | 79 comment | 73 complexity | a6ed5cb053d3fa8c114ecfe81c5f1cc4 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. //
  2. // main.cpp for ezlupdate
  3. //
  4. // This file is based on main.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-2010 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. #include <iostream>
  31. #include <qstringlist.h>
  32. #include <qregexp.h>
  33. #include <qdir.h>
  34. // #include <qfile.h>
  35. // #include <qtextstream.h>
  36. #include <errno.h>
  37. #include <metatranslator.h>
  38. // used in eZ Publish mode
  39. void traverse( const QDir &dir, MetaTranslator &fetchedTor, bool assumeUTF8 = false );
  40. // defined in fetchtr_php.cpp and fetchtr_tpl.cpp
  41. extern void fetchtr_php( QFileInfo *fi, MetaTranslator *tor, bool mustExist );
  42. extern void fetchtr_tpl( QFileInfo *fi, MetaTranslator *tor, bool mustExist, bool assumeUTF8 = false );
  43. // defined in merge.cpp
  44. extern void merge( MetaTranslator *tor, const MetaTranslator *virginTor, const QString &language, bool verbose );
  45. static int verbose = 0;
  46. static QString version = "4.4.0alpha5"; // eZ Publish version plus local version
  47. static QStringList dirs; // Additional scan directories
  48. static bool extension = false; // Extension mode
  49. static QDir extension_dir; // Extension directory
  50. static QRegExp localeRE( "^[a-z]{3}-[A-Z]{2}(@.*)?$" );
  51. static bool untranslated = false; // Untranslated translation is off by default
  52. static void printOut( const QString & out )
  53. {
  54. std::cout << out.utf8() << std::endl;
  55. }
  56. static void printUsage()
  57. {
  58. printOut( "Creates or updates eZ Publish translations.\n"
  59. "Usage: ezlupdate [OPTION]... LANGUAGE\n\n"
  60. "Options:\n"
  61. " -h, --help Display this information and exit\n"
  62. " -e, --extension EXT Extension mode. Scans extension EXT instead of\n"
  63. " kernel, lib and design\n"
  64. " -d, --dirs DIR [DIR]... Directories to scan in addition to kernel, lib\n"
  65. " and designs\n"
  66. " -u, --untranslated Create/update the untranslated file as well\n"
  67. " -no, --noobsolete Drop all obsolete strings\n"
  68. " --utf8 Assume UTF8 when the encoding is uncertain\n"
  69. " -v, --verbose Explain what is being done\n"
  70. " -vv Really explain what is being done\n"
  71. " --version Display the version of ezlupdate and exit\n" );
  72. }
  73. int main( int argc, char **argv )
  74. {
  75. // If no arguments, print help and exit
  76. if ( argc < 2 )
  77. {
  78. printUsage();
  79. return 1;
  80. }
  81. // Argument handling
  82. bool noObsolete = false;
  83. bool assumeUTF8 = false;
  84. QStringList languages;
  85. for ( int i = 1; i < argc; i++ )
  86. {
  87. if ( qstrcmp( argv[i], "--help" ) == 0 ||
  88. qstrcmp( argv[i], "-h" ) == 0 )
  89. {
  90. printUsage();
  91. return 0;
  92. }
  93. else if ( qstrcmp( argv[i], "--untranslated" ) == 0 ||
  94. qstrcmp( argv[i], "-u" ) == 0 )
  95. {
  96. untranslated = true;
  97. }
  98. else if ( qstrcmp( argv[i], "--extension" ) == 0 ||
  99. qstrcmp( argv[i], "-e" ) == 0 )
  100. {
  101. if ( i < argc - 1 )
  102. {
  103. i++;
  104. QString arg( argv[i] );
  105. extension_dir.setPath( arg );
  106. if ( !arg.startsWith( "-" ) && extension_dir.exists() )
  107. {
  108. printOut( "Extension mode, directory: " + arg );
  109. extension = true;
  110. }
  111. else
  112. {
  113. qFatal( "ERROR: Directory does not exist: " + arg );
  114. }
  115. }
  116. else
  117. {
  118. qFatal( "ERROR: Extension directory missing" );
  119. }
  120. }
  121. else if ( qstrcmp( argv[i], "--dirs" ) == 0 ||
  122. qstrcmp( argv[i], "-d" ) == 0 )
  123. {
  124. ++i;
  125. while ( i < argc )
  126. {
  127. QString arg( argv[i] );
  128. if ( arg.startsWith( "-" ) )
  129. {
  130. break;
  131. }
  132. QDir dir( arg );
  133. if ( dir.exists() )
  134. {
  135. printOut( "Added scan directory: " + arg );
  136. dirs.append( arg );
  137. }
  138. i++;
  139. }
  140. continue;
  141. }
  142. else if ( qstrcmp( argv[i], "--noobsolete" ) == 0 ||
  143. qstrcmp( argv[i], "-no" ) == 0 )
  144. {
  145. noObsolete = true;
  146. continue;
  147. }
  148. else if ( qstrcmp( argv[i], "--utf8" ) == 0 )
  149. {
  150. assumeUTF8 = true;
  151. continue;
  152. }
  153. else if ( qstrcmp( argv[i], "--verbose" ) == 0 ||
  154. qstrcmp( argv[i], "-v" ) == 0 )
  155. {
  156. verbose = 1;
  157. continue;
  158. }
  159. else if ( qstrcmp( argv[i], "-vv" ) == 0 )
  160. {
  161. verbose = 2;
  162. continue;
  163. }
  164. else if ( qstrcmp( argv[i], "--version" ) == 0 )
  165. {
  166. printOut( QString( "ezlupdate version %1-%2" ).arg( QT_VERSION_STR ).arg( version ) );
  167. return 0;
  168. }
  169. else
  170. {
  171. QString language = argv[i];
  172. if ( localeRE.match( language ) == -1 )
  173. {
  174. qFatal( "ERROR - Locale should be of the form aaa-AA or aaa-AA@variation. Examples: eng-GB, nor-NO, srp-RS@latin" );
  175. }
  176. else
  177. languages.append( language );
  178. }
  179. }
  180. if ( untranslated )
  181. {
  182. // Add the untranslated file to the list
  183. languages.append( "untranslated" );
  184. }
  185. if ( languages.count() == 0 )
  186. {
  187. qFatal( "ERROR - No languages defined, cannot continue." );
  188. return 1;
  189. }
  190. // Create/verify translation directory
  191. QDir tfdir( "share/translations" );
  192. if ( extension )
  193. tfdir.setPath( extension_dir.path() + QDir::separator() + "translations" );
  194. if ( !tfdir.exists() )
  195. {
  196. if ( QDir::current().mkdir( tfdir.path() ) )
  197. {
  198. printOut( "eZ Publish translations directory created: " + tfdir.path() );
  199. }
  200. else
  201. {
  202. qFatal( "ERROR - eZ Publish translations directory could not be created: " + tfdir.path() );
  203. }
  204. }
  205. for ( QStringList::ConstIterator it = languages.begin(); it != languages.end(); ++it )
  206. {
  207. const QString &language = *it;
  208. QDir languageDir;
  209. languageDir.setPath( tfdir.path() + QDir::separator() + language );
  210. if ( !languageDir.exists() )
  211. {
  212. if ( QDir::current().mkdir( languageDir.path() ) )
  213. {
  214. printOut( "eZ Publish translations directory created: " + languageDir.path() );
  215. }
  216. else
  217. {
  218. qFatal( "ERROR - eZ Publish translations directory could not be created: " + languageDir.path() );
  219. }
  220. }
  221. }
  222. // Start the real job
  223. QDir dir;
  224. QString currentPath = dir.absPath();
  225. MetaTranslator fetchedTor;
  226. if ( extension )
  227. {
  228. if ( verbose )
  229. printOut( QString( "Checking eZ Publish extension directory: '%1'" ).arg( extension_dir.absPath().latin1() ) );
  230. dir.setCurrent( extension_dir.absPath() );
  231. traverse( dir.currentDirPath(), fetchedTor, assumeUTF8 );
  232. }
  233. else
  234. {
  235. if ( verbose )
  236. printOut( QString( "Checking eZ Publish directory: '%1'" ).arg( dir.absPath().latin1() ) );
  237. // traverse( dir.path() + QDir::separator() + "kernel", fetchedTor, assumeUTF8 );
  238. // traverse( dir.path() + QDir::separator() + "lib", fetchedTor, assumeUTF8 );
  239. // traverse( dir.path() + QDir::separator() + "design", fetchedTor, assumeUTF8 );
  240. // Fix for bug in qt win free, only reads content of current directory
  241. dir.setCurrent( currentPath + "/kernel" );
  242. traverse( dir.currentDirPath(), fetchedTor, assumeUTF8 );
  243. dir.setCurrent( currentPath + "/lib" );
  244. traverse( dir.currentDirPath(), fetchedTor, assumeUTF8 );
  245. dir.setCurrent( currentPath + "/design" );
  246. traverse( dir.currentDirPath(), fetchedTor, assumeUTF8 );
  247. }
  248. // Additional directories
  249. dir.setCurrent( currentPath );
  250. for ( QStringList::Iterator it = dirs.begin(); it != dirs.end(); ++it )
  251. {
  252. dir.setCurrent( *it );
  253. traverse( dir.currentDirPath(), fetchedTor, assumeUTF8 );
  254. }
  255. // Cleanup
  256. dir.setCurrent( currentPath );
  257. // // Try to find codec from locale file
  258. // QString codec;
  259. // QFileInfo localefi( dir.absPath() + "/classes/locale/" + language + ".ini" );
  260. // if ( localefi.exists() )
  261. // {
  262. // QFile locale( localefi.filePath() );
  263. // if ( locale.open( IO_ReadOnly ) )
  264. // {
  265. // QTextStream localeStream( &locale );
  266. // QString line, liso = "LanguageISO";
  267. // while ( !localeStream.atEnd() )
  268. // {
  269. // line = localeStream.readLine();
  270. // if ( line.startsWith( liso ) )
  271. // {
  272. // codec = line.right( line.length() - liso.length() - 1 );
  273. // break;
  274. // }
  275. // }
  276. // locale.close();
  277. // }
  278. // }
  279. // if ( !codec.isNull() )
  280. // {
  281. // tor.setCodec( codec.latin1() );
  282. // if ( verbose )
  283. // printOut( "Setting codec for .ts file to: %s", codec.latin1() );
  284. // }
  285. // else
  286. // printOut( "Warning: No codec found, setting codec for .ts file to default: iso-8859-1" );
  287. for ( QStringList::ConstIterator it = languages.begin(); it != languages.end(); ++it )
  288. {
  289. const QString &language = *it;
  290. MetaTranslator tor;
  291. QFileInfo fi( tfdir.path() + QDir::separator() + language + QDir::separator() + "translation.ts" );
  292. tor.load( fi.filePath() );
  293. if ( verbose )
  294. printOut( QString( "Updating '%1'").arg( fi.filePath().latin1() ) );
  295. merge( &tor, &fetchedTor, language, verbose );
  296. if ( noObsolete )
  297. tor.stripObsoleteMessages();
  298. tor.stripEmptyContexts();
  299. if ( !tor.save( fi.filePath() ) )
  300. qWarning( "ezlupdate error: Cannot save '%s': %s", fi.filePath().latin1(), strerror( errno ) );
  301. }
  302. return 0;
  303. }
  304. /**!
  305. Recursively traverse an eZ Publish directory
  306. */
  307. void traverse( const QDir &dir, MetaTranslator &fetchedTor, bool assumeUTF8 )
  308. {
  309. if ( verbose )
  310. printOut( QString( " Checking subdirectory '%1'" ).arg( dir.path().latin1() ) );
  311. if ( !dir.exists() )
  312. return;
  313. const QFileInfoList *list = dir.entryInfoList();
  314. QFileInfoListIterator it( *list );
  315. QFileInfo *fi;
  316. while ( (fi = it.current()) )
  317. {
  318. ++it;
  319. if ( fi->fileName().startsWith( "." ) )
  320. {
  321. // Do nothing
  322. }
  323. else if ( fi->isDir() )
  324. {
  325. QDir subdir = dir;
  326. subdir.setCurrent( subdir.path() + QDir::separator() + fi->fileName() );
  327. traverse( subdir.currentDirPath(), fetchedTor, assumeUTF8 );
  328. subdir.setCurrent( dir.path() );
  329. }
  330. else
  331. {
  332. if ( fi->fileName().endsWith( ".php", false ) )
  333. {
  334. if ( verbose > 1 )
  335. printOut( QString( " Checking '%1'" ).arg( fi->fileName().latin1() ) );
  336. fetchtr_php( fi, &fetchedTor, true );
  337. }
  338. else if ( fi->fileName().endsWith( ".tpl", false ) )
  339. {
  340. if ( verbose > 1 )
  341. printOut( QString( " Checking '%1'").arg( fi->fileName().latin1() ) );
  342. fetchtr_tpl( fi, &fetchedTor, true, assumeUTF8 );
  343. }
  344. }
  345. }
  346. }