Top Banner
Drupal Site Translation and Translation Testing By James Andres For VANDUG, May 2011
16

Drupal site translation and translation testing

Jun 19, 2015

Download

Technology

james_andres

An in-depth look at managing translations for complex Drupal projects using .po files.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Drupal site translation and translation testing

Drupal Site Translation and Translation Testing

By James AndresFor VANDUG, May 2011

Page 2: Drupal site translation and translation testing

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

Page 3: Drupal site translation and translation testing

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”);

Page 4: Drupal site translation and translation testing

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*)

Page 5: Drupal site translation and translation testing

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)

Page 6: Drupal site translation and translation testing

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

Page 7: Drupal site translation and translation testing

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()

Page 8: Drupal site translation and translation testing

Solution, make a visually scannable test language.  The "." language..

Page 9: Drupal site translation and translation testing

Addingalanguage

Page 10: Drupal site translation and translation testing

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

Page 11: Drupal site translation and translation testing

After import, 3 issues .. not bad!

Page 12: Drupal site translation and translation testing

Using i18nmenu, i18ntaxonomy, etc.

Page 13: Drupal site translation and translation testing

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.

Page 14: Drupal site translation and translation testing

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 .. />');

Page 15: Drupal site translation and translation testing

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:

Page 16: Drupal site translation and translation testing

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.