[glom] Add glom_import_po_all command-line utility.



commit ddb7ebe4a770293dcd7a304bab274cca56e6284f
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Jan 9 12:22:47 2012 +0100

    Add glom_import_po_all command-line utility.
    
    	* glom/libglom/translations_po.cc: write_translations_to_po_file():
      Mark the document as modified so the changes will (or can be) saved.
    	* Makefile_libglom.am:
    	* glom/glom_import_po_all.cc: Add this command-line utility to read all
    	.po files in a directory back into the .glom file.

 ChangeLog                       |   10 +++++++++
 Makefile_libglom.am             |    7 +++++-
 glom/glom_import_po_all.cc      |   40 +++++++++++++++++++++++++++++++-------
 glom/libglom/translations_po.cc |    3 ++
 po/POTFILES.in                  |    1 +
 5 files changed, 52 insertions(+), 9 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index df82c73..75761f2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2012-01-09  Murray Cumming  <murrayc murrayc com>
 
+	Add glom_import_po_all command-line utility.
+
+	* glom/libglom/translations_po.cc: write_translations_to_po_file():
+  Mark the document as modified so the changes will (or can be) saved.
+	* Makefile_libglom.am:
+	* glom/glom_import_po_all.cc: Add this command-line utility to read all 
+	.po files in a directory back into the .glom file.
+	
+2012-01-09  Murray Cumming  <murrayc murrayc com>
+
 	Command-line utilities: Initialize gettext and the locale.
 
 	* glom/glom_create_from_example.cc:
diff --git a/Makefile_libglom.am b/Makefile_libglom.am
index eea463a..940d873 100644
--- a/Makefile_libglom.am
+++ b/Makefile_libglom.am
@@ -88,7 +88,8 @@ glom_commandline_cppflags = $(glom_includes) $(LIBGLOM_CFLAGS) $(PYTHON_CPPFLAGS
 bin_PROGRAMS = glom/glom_create_from_example \
   glom/glom_test_connection \
   glom/glom_export_po \
-  glom/glom_export_po_all
+  glom/glom_export_po_all \
+  glom/glom_import_po_all
 
 glom_glom_create_from_example_SOURCES = glom/glom_create_from_example.cc
 glom_glom_create_from_example_LDADD = $(glom_commandline_ldadd)
@@ -105,3 +106,7 @@ glom_glom_export_po_CPPFLAGS = $(glom_commandline_cppflags)
 glom_glom_export_po_all_SOURCES = glom/glom_export_po_all.cc
 glom_glom_export_po_all_LDADD = $(glom_commandline_ldadd)
 glom_glom_export_po_all_CPPFLAGS = $(glom_commandline_cppflags)
+
+glom_glom_import_po_all_SOURCES = glom/glom_import_po_all.cc
+glom_glom_import_po_all_LDADD = $(glom_commandline_ldadd)
+glom_glom_import_po_all_CPPFLAGS = $(glom_commandline_cppflags)
diff --git a/glom/glom_import_po_all.cc b/glom/glom_import_po_all.cc
index e29915f..a5b7a36 100644
--- a/glom/glom_import_po_all.cc
+++ b/glom/glom_import_po_all.cc
@@ -31,6 +31,7 @@
 #include <glibmm/convert.h>
 #include <glibmm/miscutils.h>
 #include <iostream>
+#include <stdexcept>
 
 #include <glibmm/i18n.h>
 
@@ -42,7 +43,6 @@ public:
   //These instances should live as long as the OptionGroup to which they are added,
   //and as long as the OptionContext to which those OptionGroups are added.
   std::string m_arg_filepath_po_input;
-  Glib::ustring m_arg_locale_id;
   bool m_arg_version;
 };
 
@@ -64,6 +64,28 @@ GlomCreateOptionGroup::GlomCreateOptionGroup()
 
 int main(int argc, char* argv[])
 {
+  bindtextdomain(GETTEXT_PACKAGE, GLOM_LOCALEDIR);
+  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
+  textdomain(GETTEXT_PACKAGE);
+
+  // Set the locale for any streams to the user's current locale,
+  // We should not rely on the default locale of
+  // any streams (we should always do an explicit imbue()),
+  // but this is maybe a good default in case we forget.
+  try
+  {
+    std::locale::global(std::locale(""));
+  }
+  catch(const std::runtime_error& ex)
+  {
+    //This has been known to throw an exception at least once:
+    //https://bugzilla.gnome.org/show_bug.cgi?id=619445
+    //This should tell us what the problem is:
+    std::cerr << G_STRFUNC << ": exception from std::locale::global(std::locale(\"\")): " << ex.what() << std::endl;
+    std::cerr << "  This can happen if the locale is not properly installed or configured." << std::endl;
+  }
+
+  
   Glom::libglom_init();
   
   Glib::OptionContext context;
@@ -106,7 +128,7 @@ int main(int argc, char* argv[])
 
   if(input_uri.empty())
   {
-    std::cerr << "Please specify a glom file." << std::endl;
+    std::cerr << _("Please specify a glom file.") << std::endl;
     std::cerr << std::endl << context.get_help() << std::endl;
     return EXIT_FAILURE;
   }
@@ -169,8 +191,8 @@ int main(int argc, char* argv[])
 
   //Import all .po files from the directory:
   Glib::RefPtr<Gio::FileEnumerator> enumerator = file_output->enumerate_children();
-  Glib::RefPtr<Gio::FileInfo> info = enumerator->next_file();
-  while(info)
+  Glib::RefPtr<Gio::FileInfo> info;
+  while(info = enumerator->next_file())
   {
     Glib::RefPtr<Gio::File> child = file_output->get_child(info->get_name());
     if(child->query_file_type() == Gio::FILE_TYPE_DIRECTORY)
@@ -182,16 +204,18 @@ int main(int argc, char* argv[])
       Glom::Utils::string_remove_suffix(basename, ".po");
     if(locale_id == basename)
       continue;
-
+    
+    document.set_allow_autosave(false); //Prevent saving while we modify the document.
     const bool succeeded = 
       Glom::import_translations_from_po_file(&document, child->get_uri(), locale_id);
     if(!succeeded)
     {
-      std::cerr << "Po file imported for locale: " << locale_id << std::endl;
+      std::cerr << Glib::ustring::compose(_("Po file import failed for locale: %1"), locale_id) << std::endl;
       return EXIT_FAILURE;
     }
- 
-    info = enumerator->next_file();
+    document.save();
+
+    std::cout << Glib::ustring::compose(_("Po file imported for locale: %1"), locale_id) << std::endl;
   }
 
   Glom::libglom_deinit();
diff --git a/glom/libglom/translations_po.cc b/glom/libglom/translations_po.cc
index f8b4b3a..e04c9f2 100644
--- a/glom/libglom/translations_po.cc
+++ b/glom/libglom/translations_po.cc
@@ -200,6 +200,7 @@ bool write_translations_to_po_file(Document* document, const Glib::ustring& po_f
   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);
   
@@ -303,6 +304,8 @@ bool import_translations_from_po_file(Document* document, const Glib::ustring& p
 
   po_file_free(po_file);
 
+  document->set_modified();
+
   return true;
 }
 
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 8ff5027..a957f25 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -60,6 +60,7 @@ glom/libglom/xsl_utils.cc
 glom/glom_create_from_example.cc
 glom/glom_export_po.cc
 glom/glom_export_po_all.cc
+glom/glom_import_po_all.cc
 glom/glom_test_connection.cc
 glom/main.cc
 glom/mode_data/box_data_calendar_related.cc



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