Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough
Feb 10 2015
Feb 10

Multi lingual sites is a desired feature for sites targeting audiences of multiple countries or of more than one linguistic groups for example my first experience with the module was translating the Wanderlust Festival website for audiences in Quebec, where French and English are national languages. For Drupal there are many features which streamline the process. We will go into some of the standard translation techniques. In a later post I will go into identifying and translating certain hard to target strings such as those in complex views.

The Translate Function:

When we are dealing with content in templates for pages/nodes/content-types, etc. some strings are hardcoded in these tpl.php files.

Drupal provides us a smart way to let its template engine know which strings to consider for translation and which strings to treat as constant literals – that don’t need translation.

There is a translate function, also known as t function or t(). Anything passed inside this function is processed and considered for translation before getting passed to html content. Example: t(“text for translation”).

String literals that are not enclosed in this function are considered as constant strings and are passed as is into the html content. Example: Anything that doesn’t require translation – like Names of places, persons, etc. (in case we are translating in the same script like English to French)

The prototype of the translate function is:

t($string, array $args = array(), array $options = array());

Here, $string is the variable containing the English string to translate.
$args is an associative array of replacements to make. Occurrences in $string of any key in $args are replaced with the corresponding value, after optional sanitization and formatting. The type of sanitization and formatting depends on the first character of the key:

  • @variable: Value is inserted as plain text.
  • %variable: This makes it display as emphasized text.
  • !variable: Inserted as is, with no sanitization or formatting. Only use this for text that has already been prepared for HTML display (for example, user-supplied text that has already been run through check_plain() previously, or is expected to contain some limited HTML tags and has already been run through filter_xss() previously).

This is an example to demonstrate above three keys:

t("This is my text", array( '@This' => 'yeh', '%is my' => 'hi', '!text' =>  'text'));

It will translate from “This is my text” to “yeh hi text”.
The simplest call to t() looks like this:
t(“This is my text”);

Where to call t() function ?

  • Any string that is a name, url, address passed in t() should specify the translated string also either from the admin panel or in the translate function call itself by passing through $args array.
  • In case you are making a Form with form API, it is a good practice to pass the title, placeholder and labels in t() function. Example:$form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#required' => TRUE,
  • In case your are using a php field in views, it is advisable to output any text in t(), except for dates or dynamic data.

Where not to call t() function ?

It is NOT advisable to use t() in following scenarios:

  • t($text);
    unless the text that the variable holds has been passed through t() elsewhere (e.g., $text is one of several translated literal strings in an array).
  • It is especially important never to call
    where $user_text is some text that a user entered – doing that can lead to cross-site scripting and other security problems.

However, you can use variable substitution in your string, to put variable text such as user names or link URLs into translated text. Variable substitution looks like this:

$text = t("@name's blog", array('@name' => format_username($account)));

Basically, you can put variables like @name into your string, and t() will substitute their sanitized values at translation time. Translators can then rearrange the string as necessary for the language (e.g., in Spanish, it might be “blog de @name”).

Jan 25 2015
Jan 25


Our fourth  multilingual site and significantly more of a challange.  When in the past I had dealt with latin alphabet only, this time I was dealing with Arabic and there were some major differences here. In the Arabic language everything reads right to left. So through simply checking a setting in i18n admin panel you can add the attribute dir="rtl" to the <html>  tag. This conveniently moves all your content to the right side of your page and changes text highlight from this direction as well. This caused quite a few issues mainly with CSS.   Sprites were off on the Arabic version by a few px, but luckily this was easy to fix, as Drupal adds the class i18n-ar to the body tag, so it was easy to target Arabic only views. One other major issue I had was a large left margin of > 1000px when language was set to Arabic, this was fixed by setting body tag to overflow:none;

Translating Fields and Taxonomy

First enable translation on the content type you want to translate.  You can find this  at the bottom  in the  ‘Publishing options’ tab.  There you need to select ‘Enabled with translation’ . After saving  you will see a new tab – ‘Multilingual settings’.  Afterwards select checkbox ‘Set current language as default for new content’ – This makes it easier for editors creating a post if they primarily post in a certain language, it will be set to that language by default without having to manually choose a language.

To translate fields in content type (labels, descriptions etc.) –  enable ‘Field translation’ module. After this we can go to the field in the ‘manage fields’ section.

and click translate tab –

and there you can translate needed title

Replace text

and click ‘Save translation’.

To translate taxonomy terms enable – ‘Taxonomy translation’ module. After this we can go to the vocabulary and edit it.

With this method we can translate terms via the admin panel. If we need different terms for each language – we must choose ‘Translate.  Different terms will be allowed for each language and they can be translated.’ – this method can also be used too in some cases. You must decide before development what method you want to use.
Now we can translate the term in admin section. Go to yoursite.com/admin/config/regional/translate/translate and search term –

now we can see the term in the translation interface.

and translate –

Jan 01 2015
Jan 01

In this tutorial we will

1. Configure Views to show nodes with only the current language.

2. Translate  strings in views that cant be translated via admin UI.

3. Translate menu items.

1. On each views (pages, blocks etc) we need to add filter criteria ‘Content: Language’ and choose ‘Current user’s language’ – https://screencast.com/t/hvRR4XmL8DQZ After this you the view will only show nodes that contain your current language on site.

2. Sometimes we have views with a difficult structure and fields with a lot of HTML tags, tokens and strings. These can’t be translated via Drupal admin UI. For example this global custom field – https://screencast.com/t/S0U6Zibb0p54 a method for translating something irregular like this is as follows.  Create a .tpl file for this field and wrap strings and links in t() and l() functions.  https://screencast.com/t/3Dj1gGjEe after this we Then these can easily found and translated in admin UI.

The Same method can be used for other views templates and nodes if there are links

3. To translate the menu we need to enable ‘Menu translation’ module (from i18n). Now we go to edit menu and enable translation mode – https://screencast.com/t/JRw0ngrBa8  Now we can translate any menu item – https://screencast.com/t/s6maX10qk

Then add (or edit) the Arabic variant  – https://screencast.com/t/LGoewMdCo

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web