Translation in Drupal 7 : How it works?
As we all know that, we were all getting projects and clients across the globe. But doing a multilingual website with each having different set of requirements make the website handling tougher and tougher. Also, building a multilingual web site involves much more than translating content from one language to another. Translation must be done with an awareness of a language's idioms as spoken within a geographic region. But in Drupal we’ve been provided with rich set of core and contrib modules, which supports translation for our website(s). When we go deeper in the concept of Drupal Translation, there is a lot more can be learnt. Well, here I explain you a few concepts that i’ve come across.
The Translation Methods:
In Drupal 7, we have two different types of translation methods, which we can call as two main modules to implement translation of content in order to make a site multilingual. One is Entity Translation (contrib) and the other one is Content Translation (core).
- a core Drupal module that allows you to translate nodes.
- Translated nodes are mapped together, but if they are distinct nodes that can be treated separately.
- Translation module lets you translate particular fields, while only a single node or entity is created.
- Entity Translation works for all kinds of entities that have fields, not just nodes. It provides a way to translate taxonomy terms, users, and comments as well.
Synchronizing Translations :
Synchronize Translations is a submodule of the Internationalization module (i18n). This module allows you to synchronize certain fields across languages when translating nodes with the Content Translation module. While it provides some of the same results, it works very differently from the entity translation module.
Here is the process I went through to set up a basic multilingual site:
Setting up the core modules
- Locale (core module) : Adds language handling functionality and enables the translation of the user interface to languages other than English.
- Once it’s enabled, Language and Translate interface links will be added under Configuration > Regional and Language
Adding new languages
- Under Configuration > Regional and language > Languages click on Add language
- Select a language from the select list and click Add language
- After adding a language, it's now time for us to define which language will be used by Drupal to display page elements. Click on Detection and Selection tab.
Now, you will be presented with different options, as each of them are explained below:
URL: Determine the language from the URL (Path prefix or domain).
- Path prefix: language code as first argument of the URL mysite.com/en/contact or mysite.com/it/contatti.
- Domain: language code as if you want like en.mysite.com or it.mysite.com. [You will need to create subdomains with this option]
- Session: Determine the language from a request/session parameter, like mysite.com?language=en or mysite.com?language=it.
- User: Follow the user's language preference. The user will be able set his language from his/her edit profile page.
- Browser: Determine the language from the browser's language settings.
- Default: Use the default site language (English).
- URL: Determine the language from the URL (Path prefix or domain).
- Now let us configure the URL option, then we proceed further:
- Choose Path prefix and save configuration. Get back to the Detection and Selection page, enable URL and Save settings.
- Now the website will define the language by checking the detection method. Lets move forward to do translation via Translate Interface.
Translate Interface (Here I chose Italian as my language)
- Go to Configure > Regional and languages > Translate interface.
- From the translate interface page, click on Translate
- Here you can look for any text string that is on the site. For new string you create in code, remember to wrap it in a t() function.
- Let us look for a sample string like “No front page content has been created yet.” and click filter.
- Now you have found the string and how you can see by the stroke on the Italian prefix that the string has not been translated. Click on Edit and translate it.
- Provide the Italian translation and click on Save translations.
- Now if I visit mysite.com/it I will see the translated text and If I go to the mysite.com/en I will see it in English.
Importing a language file
- You are also able to Import translated strings to the system from Import page. The file format will be .po. A list of ready translation may be found here https://localize.drupal.org/
- Choose the language(s) you are interested in and download the files related to your Drupal Version.
- Now, go to Configuration > Regional and language > Translate interface > Import, add the file in the Language file field input, import to the targeted language, choose the Text group and choose the mode and click import.
- After the import, Drupal can now translate your new language! In this case, Italian.Also if we check the homepage, we can see that most of the text has been translated too and the message I’ve translated previously, has been replaced with the one provided by the .po file.
Exporting the language Files
- If you want to import strings from an environment to another one go to Go to Configuration > Regional and language > Translate interface > Export and under Export translation, select the language you want to export, text group and click export. This operation will create a .po file with all the translated strings.
- You can also export a clean template for translation, this will give you a file with all strings from the Drupal locale database.
We will see the effective usage of translation in custom module in the next part.