[glom] Exporting of .po files: Do not lose non-ASCII characters.



commit 090c44ef63ca28ed11e5ff5aabf325e3da3abe55
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Jan 9 13:22:40 2012 +0100

    Exporting of .po files: Do not lose non-ASCII characters.
    
    * glom/libglom/translations_po.cc: write_translations_to_po_file():
      Write the file manually instead of using gettext-po.h and its po_file_write(),
      because that loses non-ASCII characters (see previous commit).
      make check now works.

 ChangeLog                       |    9 ++++++
 glom/glom_export_po.cc          |    2 +-
 glom/glom_export_po_all.cc      |    2 +-
 glom/glom_import_po_all.cc      |    2 +-
 glom/libglom/translations_po.cc |   58 ++++++++++-----------------------------
 5 files changed, 27 insertions(+), 46 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 68d6b6f..7513b19 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2012-01-09  Murray Cumming  <murrayc murrayc com>
 
+	Exporting of .po files: Do not lose non-ASCII characters.
+
+	* glom/libglom/translations_po.cc: write_translations_to_po_file():
+  Write the file manually instead of using gettext-po.h and its po_file_write(), 
+  because that loses non-ASCII characters (see previous commit).
+  make check now works.
+
+2012-01-09  Murray Cumming  <murrayc murrayc com>
+
 	test_document_export_po: Test a special character.
 
 	* tests/translations_po/test_document_export_po.cc:
diff --git a/glom/glom_export_po.cc b/glom/glom_export_po.cc
index b25b375..87ac8cc 100644
--- a/glom/glom_export_po.cc
+++ b/glom/glom_export_po.cc
@@ -19,7 +19,7 @@
  */
 
 // For instance:
-// glom_export_po /opt/gnome30/share/doc/glom/examples/example_music_collection.glom --output="/home/someone/something.po"
+// glom_export_po /opt/gnome30/share/doc/glom/examples/example_music_collection.glom --output-path="/home/someone/something.po"
 
 #include "config.h"
 
diff --git a/glom/glom_export_po_all.cc b/glom/glom_export_po_all.cc
index a27cc08..e97a498 100644
--- a/glom/glom_export_po_all.cc
+++ b/glom/glom_export_po_all.cc
@@ -19,7 +19,7 @@
  */
 
 // For instance:
-// glom_export_po /opt/gnome30/share/doc/glom/examples/example_music_collection.glom --output="/home/someone/something.po"
+// glom_export_po_all /opt/gnome30/share/doc/glom/examples/example_music_collection.glom --output-path="/home/someone/po_files/"
 
 #include "config.h"
 
diff --git a/glom/glom_import_po_all.cc b/glom/glom_import_po_all.cc
index a5b7a36..30dfba0 100644
--- a/glom/glom_import_po_all.cc
+++ b/glom/glom_import_po_all.cc
@@ -19,7 +19,7 @@
  */
 
 // For instance:
-// glom_export_po /opt/gnome30/share/doc/glom/examples/example_music_collection.glom --output="/home/someone/something.po"
+// glom_import_po_all /opt/gnome30/share/doc/glom/examples/example_music_collection.glom --input-path="/home/someone/po_Files"
 
 #include "config.h"
 
diff --git a/glom/libglom/translations_po.cc b/glom/libglom/translations_po.cc
index e04c9f2..239e821 100644
--- a/glom/libglom/translations_po.cc
+++ b/glom/libglom/translations_po.cc
@@ -156,11 +156,12 @@ bool write_translations_to_po_file(Document* document, const Glib::ustring& po_f
     return false;
   }
 
-  if(setjmp(jump) != 0)
-    return false;  
-
-  po_file_t po_file = po_file_create();
-  po_message_iterator_t msg_iter = po_message_iterator(po_file, 0);
+  //We do not use gettext-po.h and its po_file_write() function for this,
+  //because that does not allow us to specify UTF-8, so it drops non-ASCII 
+  //characters such as U with umlaut.
+  //It also has no obvious API for setting the header, so we would have to 
+  //do that manually anyway.
+  Glib::ustring data;
 
   Document::type_list_translatables list_layout_items = document->get_translatable_items();
   for(Document::type_list_translatables::iterator iter = list_layout_items.begin(); iter != list_layout_items.end(); ++iter)
@@ -174,52 +175,23 @@ bool write_translations_to_po_file(Document* document, const Glib::ustring& po_f
 
     const Glib::ustring hint = iter->second;
 
-    po_message_t msg = po_message_create();
-    po_message_set_msgid(msg, item->get_title_original().c_str());
-    po_message_set_msgstr(msg, item->get_title_translation(translation_locale, false).c_str());
-
     // Add "context" comments, to uniquely identify similar strings, used in different places,
     // and to provide a hint for translators.
-    const Glib::ustring msgtxt = get_po_context_for_item(item, hint);
-    //std::cout << "debug: msgtxt=" << msgtxt << std::endl;
-    po_message_set_msgctxt(msg, msgtxt.c_str());
-
-    po_message_insert(msg_iter, msg);
+    Glib::ustring msg = "msgctxt \"" + get_po_context_for_item(item, hint) + "\"\n";
+    
+    //The original and its translation:
+    msg += "msgid \"" + item->get_title_original() + "\"\n";
+    msg += "msgstr \"" + item->get_title_translation(translation_locale, false) + "\"";
+    
+    data += msg + "\n\n";
   }
 
-  po_message_iterator_free(msg_iter);
-
-  #ifdef HAVE_GETTEXTPO_XERROR
-  po_xerror_handler error_handler;
-  memset(&error_handler, 0, sizeof(error_handler));
-  error_handler.xerror = &on_gettextpo_xerror;
-  error_handler.xerror2 = &on_gettextpo_xerror2;
-  #else
-  po_error_handler error_handler;
-  memset(&error_handler, 0, sizeof(error_handler));
-  error_handler.error = &on_gettextpo_error;
-  #endif //HAVE_GETTEXTPO_XERROR
-
-  output_format_po.requires_utf8 = true;
-  const po_file_t written = po_file_write(po_file, filename.c_str(), &error_handler);
-  po_file_free(po_file);
-  
-  if(!written)
-    return false;
-
-  //Prepend the po header, by reading in the data written by po_file_write(),
-  //and then writing it all out again. The (generally awkward) gettext-po API 
-  //does not offer an easier way to do this.
-  //
-  //Actually. maybe we could use po_header_set_field(), but that is not 
-  //clear and that returns allocated strings that we would need to free instead 
-  //of just ignoring, so this is probably still the easiest way.
+  //The header:
   const Glib::DateTime revision_date = Glib::DateTime::create_now_local();
   const Glib::ustring revision_date_str = revision_date.format("%F %R%z");
-
-  const Glib::ustring data = Glib::file_get_contents(filename);
   const Glib::ustring header = Glib::ustring::compose(GLOM_PO_HEADER,
     document->get_database_title(), revision_date_str, locale_name);
+  
   const Glib::ustring full = header + "\n\n" + data;
   Glib::file_set_contents(filename, full);
 



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