blog-banner

Translation in Drupal 7 : How it works?

  • Drupal 7
  • Drupal Planet
  • multilingual
  • Translation

Drupal Translation

 

As we all know, we were all getting projects and clients across the globe. But doing a multilingual website with each having a different set of requirements makes the website handling tougher and tougher. Also, building a multilingual website involves much more than translating content from one language to another. The 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 a rich set of core and contrib modules, which supports translation for our website(s). When we go deeper into the concept of Drupal Translation, there is a lot more that can be learned. Well, here I explain to 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 two main modules to implement the translation of content in order to make a site multilingual. One is Entity Translation (contrib) and the other one is Content Translation (core).

  1. Content Translation

    1. A core Drupal module that allows you to translate nodes.

    2. Translated nodes are mapped together, but if they are distinct nodes they can be treated separately.

  1. Entity Translation

    1. The translation module lets you translate particular fields, while only a single node or entity is created.

    2. 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 Translation 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:

  1. Setting up the core modules

    1. Locale (core module): Adds language handling functionality and enables the translation of the user interface to languages other than English.

    2. Once it’s enabled, Language and Translate interface links will be added under Configuration  > Regional and Language

  1. Adding new languages

    1. Under Configuration -> Regional and language > Languages click on Add language

  1. Select a language from the select list and click Add language

  1. 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 the Detection and Selection tab.

  1. Now, you will be presented with different options, as each of them is explained below:

    1. URL: Determine the language from the URL (Path prefix or domain).

      1. Path prefix: language code as the first argument of the URL mysite.com/en/contact or mysite.com/it/contatti.

      2. Domain: language code as if you want like en.mysite.com or it.mysite.com. [You will need to create subdomains with this option]

    2. Session: Determine the language from a request/session parameter, like mysite.com?language=en or mysite.com?language=it.

    3. User: Follow the user's language preference. The user will be able to set his language from his/her edit profile page.

    4. Browser: Determine the language from the browser's language settings.

    5. Default: Use the default site language (English).

  2. Now let us configure the URL option, then we proceed further:

  1. Choose Path prefix and save configuration. Get back to the Detection and Selection page, enable URL  and Save settings.

  1. Now the website will define the language by checking the detection method. Let's move forward to do translation via Translate Interface.

  1. Translate Interface (Here I chose Italian as my language)

    1. Go to Configure > Regional and languages > Translate interface.

    2. From the translate interface page, click on Translate

    3. Here you can look for any text string that is on the site. For the new string you create in code, remember to wrap it in a t() function.

    4. Let us look for a sample string like “No front page content has been created yet.” and click filter.

  1. 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.

  1. Provide the Italian translation and click on Save translations.

  1. Now if I visit mysite.com/it I will see the translated text and If I go to mysite.com/en I will see it in English.

  1. Importing a language file

    1. You are also able to Import translated strings to the system from the Import page. The file format will be .po. A list of the ready translations may be found here https://localize.drupal.org/

    2. Choose the language(s) you are interested in and download the files related to your Drupal Version.

    3. 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.

  1. 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.

  1. Exporting the language Files

    1. If you want to import strings from one 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.

  1. 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 the custom module in the next part.

Get awesome tech content in your inbox