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

[vidalia-svn] r2744: Create a Qt-based ts2po helper tool to convert .ts files to (in vidalia/trunk/src: . tools tools/ts2po vidalia/i18n)



Author: edmanm
Date: 2008-06-16 22:08:44 -0400 (Mon, 16 Jun 2008)
New Revision: 2744

Added:
   vidalia/trunk/src/tools/
   vidalia/trunk/src/tools/CMakeLists.txt
   vidalia/trunk/src/tools/ts2po/
   vidalia/trunk/src/tools/ts2po/CMakeLists.txt
   vidalia/trunk/src/tools/ts2po/ts2po.cpp
   vidalia/trunk/src/tools/ts2po/ts2po_config.h.in
Modified:
   vidalia/trunk/src/CMakeLists.txt
   vidalia/trunk/src/vidalia/i18n/CMakeLists.txt
Log:
Create a Qt-based ts2po helper tool to convert .ts files to .po format, without
having to depend on translate-toolkit.


Modified: vidalia/trunk/src/CMakeLists.txt
===================================================================
--- vidalia/trunk/src/CMakeLists.txt	2008-06-16 23:30:33 UTC (rev 2743)
+++ vidalia/trunk/src/CMakeLists.txt	2008-06-17 02:08:44 UTC (rev 2744)
@@ -40,6 +40,7 @@
   add_subdirectory(miniupnpc)
 endif(USE_MINIUPNPC)
 
+add_subdirectory(tools)
 add_subdirectory(torcontrol)
 add_subdirectory(common)
 add_subdirectory(vidalia)

Added: vidalia/trunk/src/tools/CMakeLists.txt
===================================================================
--- vidalia/trunk/src/tools/CMakeLists.txt	                        (rev 0)
+++ vidalia/trunk/src/tools/CMakeLists.txt	2008-06-17 02:08:44 UTC (rev 2744)
@@ -0,0 +1,14 @@
+##
+##  $Id$
+## 
+##  This file is part of Vidalia, and is subject to the license terms in the
+##  LICENSE file, found in the top level directory of this distribution. If 
+##  you did not receive the LICENSE file with this file, you may obtain it
+##  from the Vidalia source package distributed by the Vidalia Project at
+##  http://www.vidalia-project.net/. No part of Vidalia, including this file,
+##  may be copied, modified, propagated, or distributed except according to
+##  the terms described in the LICENSE file.
+##
+
+add_subdirectory(ts2po)
+


Property changes on: vidalia/trunk/src/tools/CMakeLists.txt
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: vidalia/trunk/src/tools/ts2po/CMakeLists.txt
===================================================================
--- vidalia/trunk/src/tools/ts2po/CMakeLists.txt	                        (rev 0)
+++ vidalia/trunk/src/tools/ts2po/CMakeLists.txt	2008-06-17 02:08:44 UTC (rev 2744)
@@ -0,0 +1,43 @@
+##
+##  $Id$
+## 
+##  This file is part of Vidalia, and is subject to the license terms in the
+##  LICENSE file, found in the top level directory of this distribution. If 
+##  you did not receive the LICENSE file with this file, you may obtain it
+##  from the Vidalia source package distributed by the Vidalia Project at
+##  http://www.vidalia-project.net/. No part of Vidalia, including this file,
+##  may be copied, modified, propagated, or distributed except according to
+##  the terms described in the LICENSE file.
+##
+
+## Define this version of ts2po
+set(VERSION "0.1")
+
+##
+include_directories(
+  ${CMAKE_CURRENT_SOURCE_DIR}
+  ${CMAKE_CURRENT_BINARY_DIR}
+)
+
+## ts2po source files
+set(ts2po_SRCS
+  ts2po.cpp
+)
+
+## Set some of the properties used in the .po header
+set(TS2PO_PROJECT_ID    "Vidalia")
+set(TS2PO_CONTACT_ADDR  "translations@xxxxxxxxxxxxxxxxxxx")
+set(TS2PO_LANGUAGE_TEAM "translations@xxxxxxxxxxxxxxxxxxx")
+
+## Create and populate config.h
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/ts2po_config.h.in
+  ${CMAKE_CURRENT_BINARY_DIR}/ts2po_config.h
+)
+
+## Create the ts2po executable
+add_executable(ts2po ${ts2po_SRCS})
+
+## Link the executable with the appropriate Qt libraries
+target_link_libraries(ts2po ${QT_LIBRARIES})
+


Property changes on: vidalia/trunk/src/tools/ts2po/CMakeLists.txt
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: vidalia/trunk/src/tools/ts2po/ts2po.cpp
===================================================================
--- vidalia/trunk/src/tools/ts2po/ts2po.cpp	                        (rev 0)
+++ vidalia/trunk/src/tools/ts2po/ts2po.cpp	2008-06-17 02:08:44 UTC (rev 2744)
@@ -0,0 +1,202 @@
+/*
+**  This file is part of Vidalia, and is subject to the license terms in the
+**  LICENSE file, found in the top level directory of this distribution. If you
+**  did not receive the LICENSE file with this file, you may obtain it from the
+**  Vidalia source package distributed by the Vidalia Project at
+**  http://www.vidalia-project.net/. No part of Vidalia, including this file,
+**  may be copied, modified, propagated, or distributed except according to the
+**  terms described in the LICENSE file.
+*/
+
+#include <QFile>
+#include <QDomDocument>
+#include <QTextStream>
+#include <QDateTime>
+
+#include "ts2po_config.h"
+
+#define TS_DOCTYPE                    "TS"
+#define TS_ELEMENT_CONTEXT            "context"
+#define TS_ELEMENT_NAME               "name"
+#define TS_ELEMENT_MESSAGE            "message"
+#define TS_ELEMENT_SOURCE             "source"
+#define TS_ELEMENT_TRANSLATION        "translation"
+
+
+/** Return the current time (in UTC) in the format YYYY-MM-DD HH:MM+0000. */
+QString
+create_po_timestamp()
+{
+  QDateTime now = QDateTime::currentDateTime().toUTC();
+  return now.toString("yyyy-MM-dd hh:mm+0000");
+}
+
+/** Return a header to be placed at the top of the .po file. */
+QString
+create_po_header()
+{
+  QString header;
+  QString tstamp = create_po_timestamp();
+
+  header.append("#,fuzzy\n");
+  header.append("msgid \"\"\n");
+  header.append("msgstr \"\"\n");
+  header.append("\"Project-Id-Version: "TS2PO_PROJECT_ID"\\n\"\n");
+  header.append("\"Report-Msgid-Bugs-To: "TS2PO_CONTACT_ADDR"\\n\"\n");
+  header.append(QString("\"POT-Creation-Date: %1\\n\"\n").arg(tstamp));
+  header.append("\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n");
+  header.append("\"Last-Translator: \\n\"\n");
+  header.append("\"Language-Team: "TS2PO_LANGUAGE_TEAM"\\n\"\n");
+  header.append("\"MIME-Version: 1.0\\n\"\n");
+  header.append("\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
+  header.append("\"Content-Transfer-Encoding: 8bit\\n\"\n");
+  header.append("\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n");
+  header.append("\"X-Generator: Vidalia ts2po "TS2PO_VERSION"\\n\"\n");
+  header.append("\n");
+
+  return header;
+}
+
+/** Convert the messages in <b>context</b> to PO format. The output will be
+ * appended to <b>po</b>. Returns the number of source messages converted on
+ * success, or -1 on error and <b>errorMessage</b> will be set. */
+int
+convert_context(const QDomElement &context, QString *po, QString *errorMessage)
+{
+  QString msgid, msgstr;
+  QDomElement location, source, translation;
+  int n = 0;
+
+  Q_ASSERT(po);
+  Q_ASSERT(errorMessage);
+
+  QDomElement name = context.firstChildElement(TS_ELEMENT_NAME);
+  if (name.isNull()) {
+    *errorMessage = QString("context element with no name (line %1)")
+                                                 .arg(context.lineNumber());
+    return -1;
+  }
+
+  QDomElement msg = context.firstChildElement(TS_ELEMENT_MESSAGE);
+  while (!msg.isNull()) {
+    /* Extract the <source> tags */
+    source = msg.firstChildElement(TS_ELEMENT_SOURCE);
+    if (source.isNull())
+      continue; /* Empty source string. Ignore. */
+    msgid = source.text();
+    msgid.replace("\r", "");
+    msgid.replace("\n", "\"\n\"");
+
+    /* Extract the <translation> tags */
+    translation = msg.firstChildElement(TS_ELEMENT_TRANSLATION);
+    msgstr = translation.text();
+    msgstr.replace("\r", "");
+    msgstr.replace("\n", "\"\n\"");
+  
+    /* Format the .po entry for this string */
+    (*po).append(QString("#: %1#%2\n").arg(name.text()).arg(++n));
+    (*po).append(QString("msgid \"%1\"\n").arg(msgid));
+    (*po).append(QString("msgstr \"%1\"\n").arg(msgstr));
+    (*po).append("\n");
+  
+    /* Find the next source message in the current context */
+    msg = msg.nextSiblingElement(TS_ELEMENT_MESSAGE);
+  }
+  return n;
+}
+
+/** Convert the TS-formatted document in <b>ts</b> to a PO-formatted document.
+ * The output will be written to <b>po</b>. Returns the number of strings
+ * converted on success, or -1 on error and <b>errorMessage</b> will be set. */
+int
+ts2po(const QDomDocument *ts, QString *po, QString *errorMessage)
+{
+  int n_strings = 0;
+  QString context;
+
+  Q_ASSERT(ts);
+  Q_ASSERT(po);
+  Q_ASSERT(errorMessage);
+
+  /* Get the document root and check that it's valid */
+  QDomElement root = ts->documentElement(); 
+  if (root.tagName() != TS_DOCTYPE)
+    return -1;
+
+  /* Start with the PO header */
+  *po = create_po_header();
+
+  /* Iterate through all of the translation contexts and build up the PO file
+   * output. */
+  QDomElement child = root.firstChildElement(TS_ELEMENT_CONTEXT);
+  while (!child.isNull()) {
+    QString context;
+  
+    /* Convert the current .ts context to .po */
+    int n = convert_context(child, &context, errorMessage);
+    if (n < 0)
+      return -1;
+    
+    /* Add it to the output file */
+    (*po).append(context);
+    n_strings += n;
+
+    /* Move to the next context */
+    child = child.nextSiblingElement(TS_ELEMENT_CONTEXT);
+  }
+  return n_strings;
+}
+
+int
+main(int argc, char *argv[])
+{
+  QTextStream error(stderr);
+  QString errorMessage;
+
+  /* Check for the correct number of input parameters. */
+  if (argc != 3) {
+    error << "usage: ts2po <infile.ts> <outfile.po>\n";
+    return 1;
+  }
+
+  /* Read and parse the input .ts file. */
+  QDomDocument ts;
+  QFile tsFile(argv[1]);
+  if (!ts.setContent(&tsFile, true, &errorMessage)) {
+    error << QString("Unable to parse '%1': %2\n").arg(argv[1])
+                                                  .arg(errorMessage);
+    return 1;
+  }
+  
+  /* Try to open the output .po file for writing. */
+  QFile poFile(argv[2]);
+  if (!poFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
+    error << QString("Unable to open '%1' for writing: %2\n")
+                                                   .arg(argv[2])
+                                                   .arg(tsFile.errorString());
+    return 2;
+  }
+ 
+  /* Convert the input .ts file to a .po formatted file. */
+  QString po;
+  int n_strings = ts2po(&ts, &po, &errorMessage);
+  if (n_strings < 0) {
+    error << QString("Unable to convert '%1' to '%2': %3\n").arg(argv[1])
+                                                            .arg(argv[2])
+                                                            .arg(errorMessage);
+    return 3;
+  }
+
+  /* Write the .po output. */
+  QTextStream out(&poFile);
+  out.setCodec("UTF-8");
+  out << po;
+  poFile.close();
+  
+  QTextStream(stdout) << QString("Converted %1 strings from %2 to %3.\n")
+                                                          .arg(n_strings)
+                                                          .arg(argv[1])
+                                                          .arg(argv[2]);
+  return 0;
+}
+


Property changes on: vidalia/trunk/src/tools/ts2po/ts2po.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: vidalia/trunk/src/tools/ts2po/ts2po_config.h.in
===================================================================
--- vidalia/trunk/src/tools/ts2po/ts2po_config.h.in	                        (rev 0)
+++ vidalia/trunk/src/tools/ts2po/ts2po_config.h.in	2008-06-17 02:08:44 UTC (rev 2744)
@@ -0,0 +1,23 @@
+/*
+**  This file is part of Vidalia, and is subject to the license terms in the
+**  LICENSE file, found in the top level directory of this distribution. If you
+**  did not receive the LICENSE file with this file, you may obtain it from the
+**  Vidalia source package distributed by the Vidalia Project at
+**  http://www.vidalia-project.net/. No part of Vidalia, including this file,
+**  may be copied, modified, propagated, or distributed except according to the
+**  terms described in the LICENSE file.
+*/
+
+#ifndef _CONFIG_H
+#define _CONFIG_H
+
+#define TS2PO_VERSION "@VERSION@"
+
+#define TS2PO_PROJECT_ID "@TS2PO_PROJECT_ID@"
+
+#define TS2PO_CONTACT_ADDR "@TS2PO_CONTACT_ADDR@"
+
+#define TS2PO_LANGUAGE_TEAM "@TS2PO_LANGUAGE_TEAM@"
+
+#endif
+


Property changes on: vidalia/trunk/src/tools/ts2po/ts2po_config.h.in
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: vidalia/trunk/src/vidalia/i18n/CMakeLists.txt
===================================================================
--- vidalia/trunk/src/vidalia/i18n/CMakeLists.txt	2008-06-16 23:30:33 UTC (rev 2743)
+++ vidalia/trunk/src/vidalia/i18n/CMakeLists.txt	2008-06-17 02:08:44 UTC (rev 2744)
@@ -49,6 +49,7 @@
 add_custom_target(lupdate 
   COMMAND ${QT_LUPDATE_EXECUTABLE} -noobsolete ${translate_SRCS} -ts ${vidalia_TS}
 )
+add_dependencies(lupdate ts2po)
 
 ## Create a target that runs lrelease for all the .ts files
 add_custom_target(translations ALL DEPENDS ${vidalia_QMS})