Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good way to provide Rails-way i18n support in Django

There's one thing in (new) Rails I envy: internationalization support (Django has one too, but I prefer Rails' flavour).

The key difference between Rails' and Django's approaches is what kind of string behaves like keys in key-value translation mapping, i.e.

Django version (keys - strings in "main" language, for example english):

msgid "Save and quit"
msgstr "Zapisz i wyjdź"

Rails version equivalent (keys - abstract strings; standalone unusable - one need to provide at least 1 "translation") - actually, Rails uses YAML format, but following example present the idea:

// english translation file

msgid "SAVE_QUIT_MESSAGE"
msgstr "Save and quit"

and

// polish translation file

msgid "SAVE_QUIT_MESSAGE"
msgstr "Zapisz i wyjdź"

Rails' way of supporting i18n is IMHO much better (think of key immutability - resistant to grammar/spelling corrections; language agnosticism etc).

One way to utilize this schema in Django would be to use some abstract language for the sole purpose of being translated (strings in that language would make immutable keys), but Django support only fixed set of languages. Another solution - sacrifice one of the supported (unused) languages to play this role - but this is just bad :P

Any ideas/third-party apps/techniques to solve this issue?


Sidenote: extending i18n support for artibrary languages would give funny opportunities:

// slang translation file

msgid "SAVE_QUIT_MESSAGE"
msgstr "Save shit 'n' quit, bro"
like image 804
gorsky Avatar asked Mar 08 '11 22:03

gorsky


Video Answer


2 Answers

Step back for a minute or two. Your doing triple work here. First you have to come up with a UNIQUE_ID and then you force people to look up either the context from the code or another language file to figure out what would the proper message for AMBIGUOUS_ARGUMENT_PROVIDED would be until you get down to providing the actual translation. And who ever said that creating IDs that can meaningfully convey the context and provide good message hints was ever easy?

What your trying to do is some preposterous shit bro! Jokes aside, the reason gettext is the most prevalent and widely used i18n and l10n API is because each message gets a unique message catalog ID assigned from it's contents and because it's proven you'll have a way better time translating messages than providing translations for IDs, reminiscent of when everyone tried making their own key->value i18n framework because it was the most straightforward to design.

You'll eventually conclude that it was a bad idea to use gettext the way it wasn't meant to and you can save yourself right now by forgetting about the whole idea.

like image 156
Filip Dupanović Avatar answered Sep 22 '22 16:09

Filip Dupanović


If you insist on doing it this way, then it can be done by generating a .po file that will contain the English translations of the source strings.

like image 35
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 16:09

Ignacio Vazquez-Abrams