Home Blog Testing Internationalization with potpie

Testing Internationalization with potpie

I am working on a project that is required to be internationalized. This means that every string must be wrapped in a call to gettext. It's very easy to miss one, so I've been using a tool called potpie to check that all strings go through gettext. potpie processes a .po file and 'fakes' each of the translations, by turning ascii characters into ƈřȧzẏ ƈħȧřȧƈŧḗřş ŀīķḗ ŧħīş. If you see any text that's not crazy, it means you've missed a call to gettext. This way we can check our .po for completeness before sending it to the translators.

Settings

To set up Django for using translation files, set the LOCALE_PATHS setting. This is where the .po and .mo files will be stored.

1 2 3 LOCALE_PATHS = [ os.path.join(BASE_DIR, 'locale'), ]

Faking the translations with potpie

Ask Django to generate the .po file for our language

1 ./manage.py makemessages -l es

You should now have a file locale/es/LC_MESSAGES/django.po containing all the strings to be translated. It will have content that looks something like this:

1 2 3 #: templates/index.html:6 msgid "Home" msgstr ""

Now run install and run potpie to translate all the strings:

1 2 pip install https://github.com/fusionbox/potpie/archive/master.zip potpie locale/es/LC_MESSAGES/django.po

The messages in django.po will now look like:

1 2 3 #: templates/index.html:6 msgid "Home" msgstr "[Ħǿḿḗ 靐ſϖı]"

Before Django can use the translations, they have to be compiled into the binary .mo format:

1 ./manage.py compilemessages -l es

You will see a file called locale/es/LC_MESSAGES/django.mo now.

Once you tell Django to use the es language:

1 LANGUAGE_CODE = 'es'

You should see all translated strings using the crazy fake language. If any string appears not crazy, you'll have to find it and add the call to gettext.

Once you've checked that all strings are translated, remove the faked .po file and generate a new one to send to the translators.

Javascript catalog

Django also supports doing translations in JavaScript. To generate the po files for JavaScript:

1 2 ./manage.py makemessages -d djangojs -l es -e html,js potpie locale/es/LC_MESSAGES/djangojs.po

Once you've set up the translation catalog according to https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#internationalization-in-javascript-code, you will be able to use the gettext() function in JavaScript to access your translations.