[glom] Export to po file: Write a po file header.
- From: Murray Cumming <murrayc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glom] Export to po file: Write a po file header.
- Date: Sun, 8 Jan 2012 21:48:35 +0000 (UTC)
commit b610f2ef8945193ddb3a74c6b3003906ca52cd16
Author: Murray Cumming <murrayc murrayc com>
Date: Sun Jan 8 22:48:28 2012 +0100
Export to po file: Write a po file header.
* glom/libglom/translations_po.[h|cc]: write_translations_to_po_file():
Add an optional locale_name parameter. We cannot discover this inside
the function because we only use iso-codes in glom, not libglom, and
I would prefer not to move that static data into libglom.
* glom/mode_design/translation/window_translations.cc:
on_button_export():Pass the extra locale_name parameter.
* tests/translations_po/data/test.po: Resave.
This makes the written .po file pass validation by msgfmt -c on the
command line.
ChangeLog | 15 +++++++
glom/libglom/translations_po.cc | 41 +++++++++++++++++--
glom/libglom/translations_po.h | 13 ++++++-
.../mode_design/translation/window_translations.cc | 3 +-
tests/translations_po/data/test.po | 8 ++--
5 files changed, 69 insertions(+), 11 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 32d7ac1..b4ad77b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,20 @@
2012-01-08 Murray Cumming <murrayc murrayc com>
+ Export to po file: Write a po file header.
+
+ * glom/libglom/translations_po.[h|cc]: write_translations_to_po_file():
+ Add an optional locale_name parameter. We cannot discover this inside
+ the function because we only use iso-codes in glom, not libglom, and
+ I would prefer not to move that static data into libglom.
+ * glom/mode_design/translation/window_translations.cc:
+ on_button_export():Pass the extra locale_name parameter.
+ * tests/translations_po/data/test.po: Resave.
+
+ This makes the written .po file pass validation by msgfmt -c on the
+ command line.
+
+2012-01-08 Murray Cumming <murrayc murrayc com>
+
Import of po files: Handle empty gettext strings.
* glom/libglom/translations_po.cc: Handle empty msgid, msgstr and
diff --git a/glom/libglom/translations_po.cc b/glom/libglom/translations_po.cc
index db08c4d..f8b4b3a 100644
--- a/glom/libglom/translations_po.cc
+++ b/glom/libglom/translations_po.cc
@@ -25,6 +25,8 @@
#include "config.h" //For HAVE_GETTEXTPO_XERROR
#include <glibmm/convert.h>
+#include <glibmm/fileutils.h>
+#include <glibmm/datetime.h>
#include <glibmm/i18n.h>
#include <iostream>
@@ -32,6 +34,18 @@
/* For really ugly hacks! */
#include <setjmp.h>
+#define GLOM_PO_HEADER \
+"msgid \"\"\n" \
+"msgstr \"\"\n" \
+"\"Project-Id-Version: %1\\n\"\n" \
+"\"product=glom&keywords=I18N+L10N&component=general\\n\"\n" \
+"\"PO-Revision-Date: %2\\n\"\n" \
+"\"Last-Translator: Someone <someone someone com>\\n\"\n" \
+"\"Language-Team: %3 <someone someone com>\\n\"\n" \
+"\"MIME-Version: 1.0\\n\"\n" \
+"\"Content-Type: text/plain; charset=UTF-8\\n\"\n" \
+"\"Content-Transfer-Encoding: 8bit\\n\""
+
namespace Glom
{
@@ -128,7 +142,7 @@ Glib::ustring get_po_context_for_item(const sharedptr<const TranslatableItem>& i
return result;
}
-bool write_translations_to_po_file(Document* document, const Glib::ustring& po_file_uri, const Glib::ustring& translation_locale)
+bool write_translations_to_po_file(Document* document, const Glib::ustring& po_file_uri, const Glib::ustring& translation_locale, const Glib::ustring& locale_name)
{
std::string filename;
@@ -186,10 +200,27 @@ bool write_translations_to_po_file(Document* document, const Glib::ustring& po_f
error_handler.error = &on_gettextpo_error;
#endif //HAVE_GETTEXTPO_XERROR
- if(po_file_write(po_file, filename.c_str(), &error_handler))
- {
- po_file_free(po_file);
- }
+ 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.
+ 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);
return true;
}
diff --git a/glom/libglom/translations_po.h b/glom/libglom/translations_po.h
index b5ab6ee..4926697 100644
--- a/glom/libglom/translations_po.h
+++ b/glom/libglom/translations_po.h
@@ -26,8 +26,19 @@
namespace Glom
{
-bool write_translations_to_po_file(Document* document, const Glib::ustring& po_file_uri, const Glib::ustring& translation_locale);
+/** Create a po file containing the translations from the Glom document.
+ * @param document The document whose translations should be written to a .po file.
+ * @param po_file The filepath at which to create a .po file.
+ * @param translation_locale For instance, de_DE.
+ * @param locale_name For instance, Deutsch, to identify the translation team.
+ */
+bool write_translations_to_po_file(Document* document, const Glib::ustring& po_file_uri, const Glib::ustring& translation_locale, const Glib::ustring& locale_name = Glib::ustring());
+/** Parse a po file, storing its translations in the Glom document.
+ * @param document The document into which the translations should be stored.
+ * @param po_file The filepath at which to find a .po file.
+ * @param translation_locale For instance, de_DE.
+ */
bool import_translations_from_po_file(Document* document, const Glib::ustring& po_file_uri, const Glib::ustring& translation_locale);
/** Get a hint about what the text is for.
diff --git a/glom/mode_design/translation/window_translations.cc b/glom/mode_design/translation/window_translations.cc
index 87af366..f33bf66 100644
--- a/glom/mode_design/translation/window_translations.cc
+++ b/glom/mode_design/translation/window_translations.cc
@@ -358,7 +358,8 @@ void Window_Translations::on_button_export()
if(add_extension)
uri += extension;
- Glom::write_translations_to_po_file(get_document(), uri, m_translation_locale);
+ Glom::write_translations_to_po_file(get_document(), uri, m_translation_locale,
+ IsoCodes::get_locale_name(m_translation_locale));
}
void Window_Translations::on_button_import()
diff --git a/tests/translations_po/data/test.po b/tests/translations_po/data/test.po
index dd49907..2817442 100644
--- a/tests/translations_po/data/test.po
+++ b/tests/translations_po/data/test.po
@@ -1,10 +1,10 @@
msgid ""
msgstr ""
-"Project-Id-Version: example_film_manager.glom\n"
+"Project-Id-Version: Openismus Film Manager\n"
"product=glom&keywords=I18N+L10N&component=general\n"
-"PO-Revision-Date: 2011-12-28 11:32+0100\n"
-"Last-Translator: Murray Cumming <murrayc murrayc com>\n"
-"Language-Team: Deutsch <gnome-de gnome org>\n"
+"PO-Revision-Date: 2012-01-08 22:38+0100\n"
+"Last-Translator: Someone <someone someone com>\n"
+"Language-Team: German <someone someone com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]