Skip to contentSkip to conversation

Correct management of .po files for translation

PATCH Daniel P. Berrange 2 participants 4 messages Apr 4, 2007 – Apr 5, 2007

lore
Share & tools
lore
#0 lore raw

In updating Fedora rawhide to work against xen-unstable.hg (3.0.5) I noticed that there is now gettext support for translating the error messages returned from XenAPI in xm. Unfortauntely the way the .po files have been created is completely wrong & useless for translators to work kwith.

First there is no master .pot file - merely an english 'translation'. Since the convention is for the untranslated strings to be in english to start with, providing an english .po file is redundant unless you specialize it to a variant like en_GB.po

The core problem though is that the catalog file ..

tools/python/xen/xm/messages/en/xen-xm.po

..does not contain untranslated strings at all - it is instead full of symbolic constants.

msgid "INTERNAL_ERROR"
msgstr "Internal error: %(1)s."

msgid "MAP_DUPLICATE_KEY"
msgstr "This map already contains %(1)s -> %(2)s."

msgid "MESSAGE_METHOD_UNKNOWN"
msgstr "The method %(1)s is unsupported."

msgid "MESSAGE_PARAMETER_COUNT_MISMATCH"
msgstr "The method %(1)s takes %(2)s argument(s) (%(3)s given)."

There are two critical reasons why msgid should always contain the master english untranslated string:

- Translators need to be able to compare the original english alongside the translated text to ensure accurate translations. Referring to external files to find the english is not practical

- The msgmerge tool used to periodically update the catelogs looks at the msgid entries to identify added / removed / editted strings. If the msgid is a symbolic constant it'll never be able to identify strings which have changed & thus mark them as needing re-translation

Basically the code is abusing the gettext translation service to do both the constant -> string conversion & the translation in one go. The normal way to do the constant -> string conversion is to have a statically declared hash table in the application itself. This contains the master english strings annotated with N_(...string..). The xgettext program will then generate the .pot files automatically in the correct format.

I'm attaching an example to show how this ought to work in the Xen case. The makefiles could be hooked up to automatically run

  xgettext --keyword=N_ -o xen-xm.pot tools/python/xen/xm/XenAPI.py

Which generates a sensible catalog file looking like

#: tools/python/xen/xm/XenAPI.py:58
#, python-format
msgid "Internal error: %(1)s."
msgstr ""

#: tools/python/xen/xm/XenAPI.py:59
#, python-format
msgid "This map already contains %(1)s -> %(2)s." msgstr ""

#: tools/python/xen/xm/XenAPI.py:60
#, python-format
msgid "The method %(1)s is unsupported."
msgstr ""

For each new language the translators simply do xen-xm.pot to fr_FR.po, and fill in the msgstr field. When xen-xm.pot is later updated, the translations can in turn be updated using msgmerge, etc, etc

Regards,
Dan.

Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
tools/python/xen/xm/XenAPI.py +17 -1
diff -r 2d25db73ddc1 tools/python/xen/xm/XenAPI.py
@@ -49,6 +49,22 @@ import xmlrpclib
 
 import xen.util.xmlrpcclient as xmlrpcclient
 
+def gettext_noop(str):
+    return str
+
+N_ = gettext_noop
+
+errormap = {
+    "INTERNAL_ERROR": N_("Internal error: %(1)s."),
+    "MAP_DUPLICATE_KEY": N_("This map already contains %(1)s -> %(2)s."),
+    "MESSAGE_METHOD_UNKNOWN": N_("The method %(1)s is unsupported."),
+    "MESSAGE_PARAMETER_COUNT_MISMATCH": N_("The method %(1)s takes %(2)s argument(s) (%(3)s given)."),
+    "SESSION_AUTHENTICATION_FAILED": N_("Permission denied."),
+    "VALUE_NOT_SUPPORTED": N_("Value \"%(2)s\" for %(1)s is not supported by this server.  The server said \"%(3)s\"."),
+    "HANDLE_INVALID": N_("The %(1)s handle %(2)s is invalid."),
+    "OPERATION_NOT_ALLOWED": N_("You attempted an operation that was not allowed."),
+    "NETWORK_ALREADY_CONNECTED": N_("The network you specified already has a PIF attached to it, and so another one may not be attached."),
+    }
 
 translation = gettext.translation('xen-xm', fallback = True)
 
@@ -68,7 +84,7 @@ class Failure(Exception):
 
     def __str__(self):
         try:
-            return translation.ugettext(self.details[0]) % self._details_map()
+            return translation.ugettext(errormap[self.details[0]]) % self._details_map()
         except TypeError, exn:
             return "Message database broken: %s.\nXen-API failure: %s" % \
                    (exn, str(self.details))

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 48 lines · Daniel P. Berrange

In updating Fedora rawhide to work against xen-unstable.hg (3.0.5) I noticed that there is now gettext support for translating the error messages returned from XenAPI in xm. Unfortauntely the way the .po files have been created is completely wrong & useless for translators to work kwith.

First there is no master .pot file - merely an english 'translation'. Since the convention is for the untranslated strings to be in english to start with, providing an english .po file is redundant unless you specialize it to a variant like en_GB.po

The core problem though is that the catalog file ..

tools/python/xen/xm/messages/en/xen-xm.po

..does not contain untranslated strings at all - it is instead full of symbolic constants.

msgid "INTERNAL_ERROR"
msgstr "Internal error: %(1)s."

msgid "MAP_DUPLICATE_KEY"
msgstr "This map already contains %(1)s -> %(2)s."

msgid "MESSAGE_METHOD_UNKNOWN"
msgstr "The method %(1)s is unsupported."

msgid "MESSAGE_PARAMETER_COUNT_MISMATCH"
msgstr "The method %(1)s takes %(2)s argument(s) (%(3)s given)."

There are two critical reasons why msgid should always contain the master english untranslated string:

- Translators need to be able to compare the original english alongside the translated text to ensure accurate translations. Referring to external files to find the english is not practical

- The msgmerge tool used to periodically update the catelogs looks at the msgid entries to identify added / removed / editted strings. If the msgid is a symbolic constant it'll never be able to identify strings which have changed & thus mark them as needing re-translation

Basically the code is abusing the gettext translation service to do both the constant -> string conversion & the translation in one go. The normal way to do the constant -> string conversion is to have a statically declared hash table in the application itself. This contains the master english strings annotated with N_(...string..). The xgettext program will then generate the .pot files automatically in the correct format.

Quoted 4 lines

I'm attaching an example to show how this ought to work in the Xen case. The makefiles could be hooked up to automatically run

  xgettext --keyword=N_ -o xen-xm.pot tools/python/xen/xm/XenAPI.py

Sounds good to me, Daniel, thanks. Could you write a patch to hook up the Makefiles as well? I reckon we'll be able to sneak this one in for 3.0.5 if you did.

Ewan.

#2 lore raw
Quoted 9 lines · Ewan Mellor
Quoted 4 lines · Daniel P. Berrange

I'm attaching an example to show how this ought to work in the Xen case. The makefiles could be hooked up to automatically run

  xgettext --keyword=N_ -o xen-xm.pot tools/python/xen/xm/XenAPI.py

Sounds good to me, Daniel, thanks. Could you write a patch to hook up the Makefiles as well? I reckon we'll be able to sneak this one in for 3.0.5 if you did.

Haha - I walked right into that trap, didn't I :-) I'll knock up a suitable patch for the Makefile & try and post it tomorrow.

Regards,
Dan.

Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
#3 lore raw
In reply to Ewan Mellor
Quoted 9 lines · Ewan Mellor
Quoted 7 lines · Daniel P. Berrange

Basically the code is abusing the gettext translation service to do both the constant -> string conversion & the translation in one go. The normal way to do the constant -> string conversion is to have a statically declared hash table in the application itself. This contains the master english strings annotated with N_(...string..). The xgettext program will then generate the .pot files automatically in the correct format.

Quoted 8 lines
Quoted 4 lines

I'm attaching an example to show how this ought to work in the Xen case. The makefiles could be hooked up to automatically run

  xgettext --keyword=N_ -o xen-xm.pot tools/python/xen/xm/XenAPI.py

Sounds good to me, Daniel, thanks. Could you write a patch to hook up the Makefiles as well? I reckon we'll be able to sneak this one in for 3.0.5 if you did.

I'm attaching a patch which hooks up the tools/python/Makefile to update the master xen-xm.pot file with new translatable text on every biuld, and to merge these into any translations. The install rule was tweaked to deal with the different build process.

There is no actual language translation in the patch - when adding new translations simply

cd xen/xm/messages
cp xen-xm.pot fr_FR.po
vi fr_FR.po

    ....

Also edit tools/python/Makefile to add the language code 'fr_FR' to the LINGUAS makefile variable.

NB, since I don't have a current xen-unstable.hg kernel build operational on my dev hardware currently I've not yet been able to test the correct operational of the XenAPI.py changes in this patch. Hopefully I'll be able to get a working kernel on this machine soon

 a/tools/python/xen/xm/messages/en/xen-xm.po |   69 ----------------------------
 b/tools/python/remove-potcdate.sed          |   19 +++++++
 b/tools/python/xen/xm/messages/xen-xm.pot   |   63 +++++++++++++++++++++++++
 config/StdGNU.mk                            |    1 
 tools/python/Makefile                       |   68 ++++++++++++++++++++++-----
 tools/python/xen/xm/XenAPI.py               |   18 ++++++-
 7 files changed, 218 insertions(+), 83 deletions(-)

Regards,
Dan.

Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
config/StdGNU.mk +1 -0
diff -r 2d25db73ddc1 config/StdGNU.mk
@@ -10,6 +10,7 @@ OBJDUMP    = $(CROSS_COMPILE)objdump
 OBJDUMP    = $(CROSS_COMPILE)objdump
 
 MSGFMT     = msgfmt
+MSGMERGE   = msgmerge
 
 INSTALL      = install
 INSTALL_DIR  = $(INSTALL) -d -m0755 -p
tools/python/Makefile +55 -13

84 lines not on this page · raw message

tools/python/remove-potcdate.sed new file +19 -0
diff -r 2d25db73ddc1 tools/python/remove-potcdate.sed
@@ -0,0 +1,19 @@
+# Sed script that remove the POT-Creation-Date line in the header entry
+# from a POT file.
+#
+# The distinction between the first and the following occurrences of the
+# pattern is achieved by looking at the hold space.
+/^"POT-Creation-Date: .*"$/{
+x
+# Test if the hold space is empty.
+s/P/P/
+ta
+# Yes it was empty. First occurrence. Remove the line.
+g
+d
+bb
+:a
+# The hold space was nonempty. Following occurrences. Do nothing.
+x
+:b
+}
tools/python/xen/xm/XenAPI.py +17 -1
diff -r 2d25db73ddc1 tools/python/xen/xm/XenAPI.py
@@ -49,6 +49,22 @@ import xmlrpclib
 
 import xen.util.xmlrpcclient as xmlrpcclient
 
+def gettext_noop(str):
+    return str
+
+N_ = gettext_noop
+
+errormap = {
+    "INTERNAL_ERROR": N_("Internal error: %(1)s."),
+    "MAP_DUPLICATE_KEY": N_("This map already contains %(1)s -> %(2)s."),
+    "MESSAGE_METHOD_UNKNOWN": N_("The method %(1)s is unsupported."),
+    "MESSAGE_PARAMETER_COUNT_MISMATCH": N_("The method %(1)s takes %(2)s argument(s) (%(3)s given)."),
+    "SESSION_AUTHENTICATION_FAILED": N_("Permission denied."),
+    "VALUE_NOT_SUPPORTED": N_("Value \"%(2)s\" for %(1)s is not supported by this server.  The server said \"%(3)s\"."),
+    "HANDLE_INVALID": N_("The %(1)s handle %(2)s is invalid."),
+    "OPERATION_NOT_ALLOWED": N_("You attempted an operation that was not allowed."),
+    "NETWORK_ALREADY_CONNECTED": N_("The network you specified already has a PIF attached to it, and so another one may not be attached."),
+    }
 
 translation = gettext.translation('xen-xm', fallback = True)
 
@@ -68,7 +84,7 @@ class Failure(Exception):
 
     def __str__(self):
         try:
-            return translation.ugettext(self.details[0]) % self._details_map()
+            return translation.ugettext(errormap[self.details[0]]) % self._details_map()
         except TypeError, exn:
             return "Message database broken: %s.\nXen-API failure: %s" % \
                    (exn, str(self.details))
tools/python/xen/xm/messages/en/xen-xm.po deleted +0 -69

69 lines not on this page · raw message

tools/python/xen/xm/messages/xen-xm.pot new file +63 -0

63 lines not on this page · raw message

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel