Drupal Site Translation and Translation Testing By James Andres For VANDUG, May 2011
Overview <ul><ul><li>Background:  locale  module,  GetText ,  .po ,  .pot </li></ul></ul><ul><ul><li>Choosing a translatio...
Drupal translation ... is a bit limited <ul><li>//   These are valid GetText translatable strings  $string = t(“Hello worl...
Module's that pick up the slack <ul><ul><li>i18n : Provides an API to translate dynamic strings </li></ul></ul><ul><ul><ul...
Translating your Drupal project <ul><ul><li>There are several different approaches.  They can each be categorised by where...
Why use .po files? <ul><ul><li>Version control of translations (is good) </li></ul></ul><ul><ul><li>Ability to alter (fix)...
Translation &quot;unit testing&quot;, plus potx crash course <ul><li>When translating large Drupal sites I often had the s...
Solution, make a visually scannable test language.  The &quot;.&quot; language..
Adding a language
Making &quot;test.po&quot;, for  (most of)  the whole site <ul><li>#   (1) cd into the root of a Drupal site  </li></ul><u...
After import, 3 issues .. not bad!
Using i18nmenu, i18ntaxonomy, etc.
The translation &quot;unit test&quot; process in full <ul><ul><li>Create a custom language called 'Test' (langcode 'test')...
Translation gotchya's // Having $this = t(‘1 tomato’); // and $that = fomat_plural($num, ‘1 tomato’, ‘@count tomatoes’); /...
Tips <ul><li>Some modules don't support translation, it's getting better but check issue queues first.  Even big modules l...
More tips <ul><li>&quot;Translatables&quot; arrays are a handy trick to get potx to generate a translation without affecti...
Upcoming SlideShare
Loading in...5
×

Drupal site translation and translation testing

3,099

Published on

An in-depth look at managing translations for complex Drupal projects using .po files.

Published in: Technology, Business
1 Comment
1 Like
Statistics
Notes
  • For those involved in translating Drupal websites, I suggest the online localization tool https://poeditor.com/
    It's very handy and easy to use.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
3,099
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Drupal site translation and translation testing

  1. 1. Drupal Site Translation and Translation Testing By James Andres For VANDUG, May 2011
  2. 2. Overview <ul><ul><li>Background: locale module, GetText , .po , .pot </li></ul></ul><ul><ul><li>Choosing a translation approach, database versus files </li></ul></ul><ul><ul><li>*Idea*! Translation &quot;unit testing&quot;, and general .po file abuse </li></ul></ul><ul><ul><li>Adding a language in the Drupal UI </li></ul></ul><ul><ul><li>Using potx via CLI </li></ul></ul><ul><ul><li>Exporting dynamic translations, using i18n </li></ul></ul><ul><ul><li>Gotchya's, tips and tricks </li></ul></ul>
  3. 3. Drupal translation ... is a bit limited <ul><li>//  These are valid GetText translatable strings  $string = t(“Hello world.”); $string = t(“Hi @name. How are you?”, array(‘@name’ => $user->name)); </li></ul><ul><li>//  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”); </li></ul>
  4. 4. Module's that pick up the slack <ul><ul><li>i18n : Provides an API to translate dynamic strings </li></ul></ul><ul><ul><ul><li>i18nblocks : Implements the i18n API, allowing for multilingual Drupal blocks </li></ul></ul></ul><ul><ul><ul><li>i18ntaxonomy : Implements the i18n API, allowing for multilingual vocabularies and taxonomy terms. </li></ul></ul></ul><ul><ul><ul><li>i18nviews : ... you're getting the idea. </li></ul></ul></ul><ul><ul><li>potx : Simplifies the creation of .po and .pot files for your modules and themes. </li></ul></ul><ul><ul><li>Other goodies:  languageicons , i18n_media (*shameless plug here*) </li></ul></ul>
  5. 5. Translating your Drupal project <ul><ul><li>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: </li></ul></ul><ul><ul><ul><li>DB: i18n_client and i18n_server </li></ul></ul></ul><ul><ul><ul><li>DB: i18n's Search interface (painful..) </li></ul></ul></ul><ul><ul><ul><li>FILES: i18n export and import, via .po files </li></ul></ul></ul><ul><ul><ul><li>FILES: potx export to .po and import via i18n </li></ul></ul></ul><ul><ul><ul><li>FILES: potx export to .po and import with custom script (my, current, preference) </li></ul></ul></ul>
  6. 6. Why use .po files? <ul><ul><li>Version control of translations (is good) </li></ul></ul><ul><ul><li>Ability to alter (fix) translations via script </li></ul></ul><ul><ul><li>Keeps deployment reproducible and sane, helps me sleep at night </li></ul></ul><ul><ul><li>Simplifies contributing translations back to localize.drupal.org </li></ul></ul>
  7. 7. Translation &quot;unit testing&quot;, plus potx crash course <ul><li>When 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? </li></ul>drupal_set_title(&quot;Homepage&quot;); // Hint, missing t()
  8. 8. Solution, make a visually scannable test language.  The &quot;.&quot; language..
  9. 9. Adding a language
  10. 10. Making &quot;test.po&quot;, for (most of)  the whole site <ul><li>#  (1) cd into the root of a Drupal site  </li></ul><ul><li>$> cd /var/www/mysite </li></ul><ul><li>#   (2) create general.pot .. if potx-cli.php in $PATH  $> potx-cli.php </li></ul><ul><li>#   (3) create test.po from general.pot  </li></ul><ul><li>$> msginit --no-translator            --locale= test            --input=general.pot </li></ul><ul><li>#   (4) Translate each string in test.po to &quot;.&quot;  </li></ul><ul><li>$> FIRST=$(grep -n 'msgstr &quot;&quot;' test.po </li></ul><ul><li>         | head -n 1 </li></ul><ul><li>         | awk -F ':' '{ print $1 }') </li></ul><ul><li>$> FIRST=$((FIRST+1)) </li></ul><ul><li>$> sed -i $FIRST',$ s/msgstr &quot;&quot;/msgstr &quot;.&quot;/g ' test.po </li></ul><ul><li>$> sed -i $FIRST',$ s/msgstr[([0-9])] &quot;&quot;/msgstr[1] &quot;.&quot;/g ' </li></ul><ul><li>           test.po </li></ul>
  11. 11. After import, 3 issues .. not bad!
  12. 12. Using i18nmenu, i18ntaxonomy, etc.
  13. 13. The translation &quot;unit test&quot; process in full <ul><ul><li>Create a custom language called 'Test' (langcode 'test') </li></ul></ul><ul><ul><li>Extract .po files for the site using potx </li></ul></ul><ul><ul><li>Extract .po files for dynamic content using i18n </li></ul></ul><ul><ul><li>Replace all  msgstr &quot;&quot;  with  msgstr &quot;.&quot;  </li></ul></ul><ul><ul><li>Import the .po files </li></ul></ul><ul><ul><li>Test. </li></ul></ul><ul><ul><li>and repeat.. </li></ul></ul><ul><li>The beauty is, it's exactly the same for real translation.  Replace step (4) with a translation team. </li></ul>
  14. 14. 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 .. />');
  15. 15. Tips <ul><li>Some 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. </li></ul><ul><li>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 &quot;mymodule/translation&quot; directory. </li></ul>For panels and views, it can be helpful to wrap the t() function around some of the strings inside &quot;in-code&quot; exports.  Example:
  16. 16. More tips <ul><li>&quot;Translatables&quot; arrays are a handy trick to get potx to generate a translation without affecting your code / execution. Example: </li></ul><ul><li>GetText has many other useful utilities, check it out! Start with msgmerge . </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×