[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r2813: Fix po2ts so that it uses the same encoding to read in a .po (vidalia/trunk/src/tools/po2ts)



Author: edmanm
Date: 2008-07-01 00:23:09 -0400 (Tue, 01 Jul 2008)
New Revision: 2813

Modified:
   vidalia/trunk/src/tools/po2ts/po2ts.cpp
Log:
Fix po2ts so that it uses the same encoding to read in a .po file that it uses
to write out the .ts file. Also make the encoding used an optional parameter
that defaults to 'utf-8'.


Modified: vidalia/trunk/src/tools/po2ts/po2ts.cpp
===================================================================
--- vidalia/trunk/src/tools/po2ts/po2ts.cpp	2008-06-28 21:50:43 UTC (rev 2812)
+++ vidalia/trunk/src/tools/po2ts/po2ts.cpp	2008-07-01 04:23:09 UTC (rev 2813)
@@ -12,8 +12,8 @@
 #include <QFile>
 #include <QDomDocument>
 #include <QTextStream>
+#include <QTextCodec>
 
-
 #define TS_DOCTYPE                    "TS"
 #define TS_ELEMENT_ROOT               "TS"
 #define TS_ELEMENT_CONTEXT            "context"
@@ -194,10 +194,12 @@
 print_usage_and_exit()
 {
   QTextStream error(stderr);
-  error << "usage: po2ts [-q] -i <infile.po> -o <outfile.ts>\n";
+  error << "usage: po2ts [-q] -i <infile.po> -o <outfile.ts> "
+           "[-c <encoding>]\n";
   error << "  -q (optional)   Quiet mode (errors are still displayed)\n";
   error << "  -i <infile.po>  Input .po file\n";
   error << "  -o <outfile.ts> Output .ts file\n";
+  error << "  -c <encoding>   Text encoding (default: utf-8)\n";
   error.flush();
   exit(1);
 }
@@ -208,10 +210,11 @@
   QTextStream error(stderr);
   QString errorMessage;
   char *infile, *outfile;
+  QTextCodec *codec = QTextCodec::codecForName("utf-8");
   bool quiet = false;
 
   /* Check for the correct number of input parameters. */
-  if (argc < 5 || argc > 6)
+  if (argc < 5 || argc > 8)
     print_usage_and_exit();
   for (int i = 1; i < argc; i++) {
     QString arg(argv[i]);
@@ -221,7 +224,13 @@
       infile = argv[i];
     else if (!arg.compare("-o", Qt::CaseInsensitive) && ++i < argc)
       outfile = argv[i];
-    else
+    else if (!arg.compare("-c", Qt::CaseInsensitive) && ++i < argc) {
+      codec = QTextCodec::codecForName(argv[i]);
+      if (!codec) {
+        error << "Invalid text encoding specified\n";
+        return 1;
+      }
+    } else
       print_usage_and_exit(); 
   }
 
@@ -235,6 +244,7 @@
 
   QDomDocument ts;
   QTextStream po(&poFile);
+  po.setCodec(codec);
   int n_strings = po2ts(&po, &ts, &errorMessage);
   if (n_strings < 0) {
     error << QString("Unable to convert '%1': %2\n").arg(infile)
@@ -252,7 +262,9 @@
 
   /* Write the .ts output. */
   QTextStream out(&tsFile);
-  out.setCodec("UTF-8");
+  out.setCodec(codec);
+  out << QString("<?xml version=\"1.0\" encoding=\"%1\"?>\n")
+                                                  .arg(QString(codec->name()));
   out << ts.toString(4);
 
   if (!quiet) {