Changeset 2814 for vidalia/trunk/src

Show
Ignore:
Timestamp:
07/01/08 00:33:32 (5 months ago)
Author:
edmanm
Message:

Make the encoding used by ts2po configurable as well (defaults to utf-8
still), and set the PO header properly.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • vidalia/trunk/src/tools/ts2po/ts2po.cpp

    r2791 r2814  
    1212#include <QDomDocument> 
    1313#include <QTextStream> 
     14#include <QTextCodec> 
    1415#include <QDateTime> 
    1516 
     
    3233} 
    3334 
    34 /** Return a header to be placed at the top of the .po file. */ 
     35/** Return a header to be placed at the top of the .po file. The header will 
     36 * include <b>encoding</b> in the Content-Type header line. */ 
    3537QString 
    36 create_po_header() 
     38create_po_header(const QString &encoding) 
    3739{ 
    3840  QString header; 
     
    4951  header.append("\"Language-Team: "TS2PO_LANGUAGE_TEAM"\\n\"\n"); 
    5052  header.append("\"MIME-Version: 1.0\\n\"\n"); 
    51   header.append("\"Content-Type: text/plain; charset=UTF-8\\n\"\n"); 
     53  header.append("\"Content-Type: text/plain; "); 
     54  header.append(QString("charset=%1\\n\"\n").arg(encoding)); 
    5255  header.append("\"Content-Transfer-Encoding: 8bit\\n\"\n"); 
    5356  header.append("\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n"); 
     
    112115 
    113116/** Convert the TS-formatted document in <b>ts</b> to a PO-formatted document. 
    114  * The output will be written to <b>po</b>. Returns the number of strings 
     117 * The output will be written to <b>po</b>, including a file header that 
     118 * specifies <b>encoding</b> as the character set. Returns the number of strings 
    115119 * converted on success, or -1 on error and <b>errorMessage</b> will be set. */ 
    116120int 
    117 ts2po(const QDomDocument *ts, QString *po, QString *errorMessage) 
     121ts2po(const QDomDocument *ts, QString *po, const QString &encoding, 
     122      QString *errorMessage) 
    118123{ 
    119124  int n_strings = 0; 
     
    130135 
    131136  /* Start with the PO header */ 
    132   *po = create_po_header(); 
     137  *po = create_po_header(encoding); 
    133138 
    134139  /* Iterate through all of the translation contexts and build up the PO file 
     
    158163{ 
    159164  QTextStream error(stderr); 
    160   error << "usage: ts2po [-q] -i <infile.ts> -o <outfile.po>\n"; 
     165  error << "usage: ts2po [-q] -i <infile.ts> -o <outfile.po> " 
     166           "[-c <encoding>]\n"; 
    161167  error << "  -q (optional)   Quiet mode (errors are still displayed)\n"; 
    162168  error << "  -i <infile.ts>  Input .ts file\n"; 
    163169  error << "  -o <outfile.po> Output .po file\n"; 
     170  error << "  -c <encoding>   Text encoding (default: utf-8)\n"; 
    164171  error.flush(); 
    165172  exit(1); 
     
    172179  QString errorMessage; 
    173180  char *infile, *outfile; 
     181  QTextCodec *codec = QTextCodec::codecForName("utf-8"); 
    174182  bool quiet = false; 
    175183   
    176184  /* Check for the correct number of input parameters. */ 
    177   if (argc < 5 || argc > 6) 
     185  if (argc < 5 || argc > 8) 
    178186    print_usage_and_exit(); 
    179187  for (int i = 1; i < argc; i++) { 
     
    185193    else if (!arg.compare("-o", Qt::CaseInsensitive) && ++i < argc) 
    186194      outfile = argv[i]; 
    187     else 
     195    else if (!arg.compare("-c", Qt::CaseInsensitive) && ++i < argc) { 
     196      codec = QTextCodec::codecForName(argv[i]); 
     197      if (!codec) { 
     198        error << "Invalid text encoding specified.\n"; 
     199        return 1; 
     200      } 
     201    } else 
    188202      print_usage_and_exit();  
    189203  } 
    190  
     204  
    191205  /* Read and parse the input .ts file. */ 
    192206  QDomDocument ts; 
     
    209223  /* Convert the input .ts file to a .po formatted file. */ 
    210224  QString po; 
    211   int n_strings = ts2po(&ts, &po, &errorMessage); 
     225  int n_strings = ts2po(&ts, &po, QString(codec->name()), &errorMessage); 
    212226  if (n_strings < 0) { 
    213227    error << QString("Unable to convert '%1' to '%2': %3\n").arg(infile) 
     
    219233  /* Write the .po output. */ 
    220234  QTextStream out(&poFile); 
    221   out.setCodec("UTF-8"); 
     235  out.setCodec(codec); 
    222236  out << po; 
    223237  poFile.close();