- Home
- Blogs
- George Boobyer's blog
- Drupal Translation Magic
Drupal Translation Magic
When you create a multilingual site with Drupal there are lot of things to consider; should I use content translation, entity translation, what about views, what about node titles.
There are lot of great resources out there and with a little lot of effort you can get it all working.
However one thing that is easily overlooked is all the strings you use in your jQuery files.
You might think that after all of the grind of configuring and translating your interface and site strings that this would be 'La goutte d'eau qui a fait déborder le vase'
Well not so! There is a massively under sung hero in the Drupal translation system. Drupal.t().
You will be familiar with t() for many reasons and especially so for all your strings that require translation and you can use this in your JQuery to present all of your script strings to the translation system.
A quick example: (see what this is for here)
<?phpDrupal.behaviors.FeatureList = { attach : function(context) { var strMore, strLess, strLessLink, strMoreLink2 ; var n = $(".feature-list li").length; if (n > 4) { strMore = Drupal.t('See More') ; var str1= "<a class='seeMoreLi listTog' href='javascript:void(0)' title='"; var str2= "'><i class='icon-plus'></i>"; strMoreLink = str1.concat(strMore,str2,strMore,'</a>'); $('.feature-list',context).once('dww',function() { $(strMoreLink).insertAfter(".feature-list"); }) ; } } }?>
Once the JQuery runs it presents the strings in t() to the translation system and they are available to translate in the translation interface. Once they have been translated they are added to a set of strings available to jQuery
<?phpDrupal.locale = { ... 'strings': {"See More":"Voir plus","See Less":"Voir moins"}} };?>
This magic is done by http://api.drupal.org/api/drupal/modules!locale!locale.module/function/locale_js_alter/7 Which scans all attached JS files (added by drupal_add_js) for t() wrapped strings and if they have translations it adds them to the Drupal.locale string set within a cached javascript file in sites/default/languages - These are then available to Drupal.t() - magic!
Contact Details
Blue-Bag Ltd
- info [at] blue-bag.com
- Telephone: 0843 2894522
- Blue-Bag HQ:
The Garage, Manor Farm
Chilcompton, Radstock
Somerset, BA3 4HP, United Kingdom - Telephone: (+44) 01761 411542
- Blue-Bag Brighton:
Unit 35 Level 6 North, New England House
New England Street, Brighton
BN1 4GH United Kingdom - Telephone: (+44) 07944 938204
- VAT GB 748125034
- UK Company Reg: 3932829