Changeset 2814
- Timestamp:
- 07/01/08 00:33:32 (6 months ago)
- Files:
-
- 1 modified
-
vidalia/trunk/src/tools/ts2po/ts2po.cpp (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
vidalia/trunk/src/tools/ts2po/ts2po.cpp
r2791 r2814 12 12 #include <QDomDocument> 13 13 #include <QTextStream> 14 #include <QTextCodec> 14 15 #include <QDateTime> 15 16 … … 32 33 } 33 34 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. */ 35 37 QString 36 create_po_header( )38 create_po_header(const QString &encoding) 37 39 { 38 40 QString header; … … 49 51 header.append("\"Language-Team: "TS2PO_LANGUAGE_TEAM"\\n\"\n"); 50 52 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)); 52 55 header.append("\"Content-Transfer-Encoding: 8bit\\n\"\n"); 53 56 header.append("\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n"); … … 112 115 113 116 /** 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 115 119 * converted on success, or -1 on error and <b>errorMessage</b> will be set. */ 116 120 int 117 ts2po(const QDomDocument *ts, QString *po, QString *errorMessage) 121 ts2po(const QDomDocument *ts, QString *po, const QString &encoding, 122 QString *errorMessage) 118 123 { 119 124 int n_strings = 0; … … 130 135 131 136 /* Start with the PO header */ 132 *po = create_po_header( );137 *po = create_po_header(encoding); 133 138 134 139 /* Iterate through all of the translation contexts and build up the PO file … … 158 163 { 159 164 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"; 161 167 error << " -q (optional) Quiet mode (errors are still displayed)\n"; 162 168 error << " -i <infile.ts> Input .ts file\n"; 163 169 error << " -o <outfile.po> Output .po file\n"; 170 error << " -c <encoding> Text encoding (default: utf-8)\n"; 164 171 error.flush(); 165 172 exit(1); … … 172 179 QString errorMessage; 173 180 char *infile, *outfile; 181 QTextCodec *codec = QTextCodec::codecForName("utf-8"); 174 182 bool quiet = false; 175 183 176 184 /* Check for the correct number of input parameters. */ 177 if (argc < 5 || argc > 6)185 if (argc < 5 || argc > 8) 178 186 print_usage_and_exit(); 179 187 for (int i = 1; i < argc; i++) { … … 185 193 else if (!arg.compare("-o", Qt::CaseInsensitive) && ++i < argc) 186 194 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 188 202 print_usage_and_exit(); 189 203 } 190 204 191 205 /* Read and parse the input .ts file. */ 192 206 QDomDocument ts; … … 209 223 /* Convert the input .ts file to a .po formatted file. */ 210 224 QString po; 211 int n_strings = ts2po(&ts, &po, &errorMessage);225 int n_strings = ts2po(&ts, &po, QString(codec->name()), &errorMessage); 212 226 if (n_strings < 0) { 213 227 error << QString("Unable to convert '%1' to '%2': %3\n").arg(infile) … … 219 233 /* Write the .po output. */ 220 234 QTextStream out(&poFile); 221 out.setCodec( "UTF-8");235 out.setCodec(codec); 222 236 out << po; 223 237 poFile.close();
