Translator hints with gettext

In our current project we use gettext not only for translating, but also for localizing date and time formats.

One day I’ve found out, that the user registration date looks strange if I switch the language to italian. The cause was, that the translator translated %Y-%m-%d to %A-%m-%g. It is logical, because “year” is “anno” in italian, so he translated %Y to %A. The result was

"Friday-02-09"

neither italian nor a correct full date.

My first idea was to embed the translation hints into the msgid itself. But the problem is, if there is no translation for particular language, the msgid is used as date format, so we can not do that.

I’ve found out that the GNU gettext provides a special feature for such comments given by the programmer and directed at the translator; these comments are called extracted comments and inserted by xgettext (for C programs) into the po file and appear as follows:

#. extracted-comments
msgid untranslated-string
msgstr translated-string

So the prefix is a hash sign followed by a dot. There is a bigger example on the GNU web site.

Unfortunatly this feature is not implemented in ruby-gettext yet. I’ll probably implement it some day. Then the following code will generate appropriate comments in the po file.

def format_date(user, t)
  # TRANSLATORS: please adjust the order of the date parts and the separator
  # %Y - Year with century
  # %m - Month of the year (01..12)
  # %d - Day of the month (01..31)
  # Example: %d.%m.%Y generates 23.02.1976
  user.timezone.utc_to_local(t.utc).strftime(_('%Y-%m-%d'))
end

Comments

blog comments powered by Disqus