[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/support/lupdate-ezpublish3/ -> main.cpp (source)

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


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7