Post on 19-Jun-2015
description
transcript
Drupal Site Translation and Translation Testing
By James AndresFor VANDUG, May 2011
Overview
• Background: locale module, GetText, .po, .pot• Choosing a translation approach, database versus
files• *Idea*! Translation "unit testing", and general .po file
abuse• Adding a language in the Drupal UI• Using potx via CLI• Exporting dynamic translations, using i18n• Gotchya's, tips and tricks
Drupal translation ... is a bit limited// These are valid GetText translatable strings $string = t(“Hello world.”);$string = t(“Hi @name. How are you?”, array(‘@name’ => $user->name));
// These are NOT valid // Only static strings allowed. No variables!$message = ($user->uid == 1) ? “Hello admin.” : “Hello user.”;$string = t($message);// Only whole continuous strings allowed. No concatenation!$string = t(“Hello: ” . “admin”);// Only static strings allowed. No variables!$string = t(“Hello: $user->name”);
Module's that pick up the slack
• i18n: Provides an API to translate dynamic stringso i18nblocks: Implements the i18n API, allowing for
multilingual Drupal blockso i18ntaxonomy: Implements the i18n API,
allowing for multilingual vocabularies and taxonomy terms.
o i18nviews: ... you're getting the idea.• potx: Simplifies the creation of .po and .pot files for
your modules and themes.• Other goodies: languageicons, i18n_media
(*shameless plug here*)
Translating your Drupal project
• There are several different approaches. They can each be categorised by where the data is stored (DB vs. FILES). Here are a few I've tried:o DB: i18n_client and i18n_servero DB: i18n's Search interface (painful..)o FILES: i18n export and import, via .po fileso FILES: potx export to .po and import via i18no FILES: potx export to .po and import with custom
script (my, current, preference)
Why use .po files?
• Version control of translations (is good)• Ability to alter (fix) translations via script• Keeps deployment reproducible and sane, helps me
sleep at night• Simplifies contributing translations back to
localize.drupal.org
Translation "unit testing", plus potx crash courseWhen translating large Drupal sites I often had the same problem: how can I easily tell if the site is fully translated?
That is, the locale module says the site is 100% translated, but what about bugs / poor code?
drupal_set_title("Homepage");// Hint, missing t()
Solution, make a visually scannable test language. The "." language..
Addingalanguage
Making "test.po", for (most of) the whole site# (1) cd into the root of a Drupal site $> cd /var/www/mysite# (2) create general.pot .. if potx-cli.php in $PATH $> potx-cli.php
# (3) create test.po from general.pot $> msginit --no-translator \ --locale=test \ --input=general.pot
# (4) Translate each string in test.po to "." $> FIRST=$(grep -n 'msgstr ""' test.po \ | head -n 1 \ | awk -F ':' '{ print $1 }')$> FIRST=$((FIRST+1))$> sed -i $FIRST',$ s/msgstr ""/msgstr "."/g' test.po$> sed -i $FIRST',$ s/msgstr\[\([0-9]\)\] ""/msgstr[\1] "."/g'\ test.po
After import, 3 issues .. not bad!
Using i18nmenu, i18ntaxonomy, etc.
The translation "unit test" process in full1.Create a custom language called 'Test' (langcode
'test')2.Extract .po files for the site using potx3.Extract .po files for dynamic content using i18n4.Replace all msgstr "" with msgstr "." 5.Import the .po files6.Test.7.and repeat..
The beauty is, it's exactly the same for real translation. Replace step (4) with a translation team.
Translation gotchya's
// Having$this = t(‘1 tomato’);// and$that = fomat_plural($num, ‘1 tomato’, ‘@count tomatoes’);// Can cause problems...
// Drupal will refuse to import some HTML, like:t('<div></div>') t('<br/>') t('<img .. />');
TipsSome modules don't support translation, it's getting better but check issue queues first. Even big modules like apachesolr, panels and views still have a few translation weak points.
Drupal.org separated the translation effort out of standard version control with the switch to Git. For management of custom module translations, however, I still recommend using a "mymodule/translation" directory.For panels and views, it can be helpful to wrap the t() function around some of the strings inside "in-code" exports. Example:
More tips
"Translatables" arrays are a handy trick to get potx to generate a translation without affecting your code / execution. Example:
GetText has many other useful utilities, check it out! Start with msgmerge.