Back

Multi Language Module in Drupal 7

We live in a world of hundreds of languages. A world that’s seeking interconnectedness and communication across these barriers. Technology has allowed us to have colleagues, friends and family members throughout the world and has given businesses the ability to have employees, customers and partners who speak different native languages.

With this in mind, we often have clients who need a website in multiple languages! So, developers need to use a multi-language solution. Within Drupal 7, and we use Internalization module to set up this solution. You can convert a site with the “Multi language installation” module to be represented in multiple languages.

In doing so, it’s important to remember how to add new language to the site via the administration panel. I’m not going to reinvent the wheel here and describe how to setup multi-language site, so instead I’ll attach helpful links here, here and here.

But let’s be honest, you’re not going to read those articles. You’re busy.

So let’s look at the steps we need to take to use a multi-language solution in Drupal 7:

  1. Install necessary modules
  2.  Add new language to the site at admin/config/regional/language
  3. Enable option “Determine the language from the URL (Path prefix or domain)” at admin/config/regional/language/configure.
  4. Activate and place in a region the Language switcher block which will allow users to switch between languages – admin/structure/block.
  5. Enable Multilingual support, with translation for each of the content type that you require.

    Our goal is to do all these steps programmatically to save your client’s time now and your company’s time in future. You don’t need to waste time to set up the languages.

Previously, putting together a multi-language solution involved multiple steps within the admin panel. With this module, all the basic multilingual tools are used, making the process much easier.

Let me explain how it works. You can use this module in two ways. The first is when you integrate the module to your installation profile.

/profile/sf/ – in /profile directory we have our custom profile – sf. There is a basic profile that includes most useful modules.

/profile/sf/sf.info – needs to include dependency for based core modules and common contrib modules (i18n of course) and we add new dependency from our custom modules:

dependencies[] = i18n
; Drupal Custom Modules
dependencies[] = sf_multi_language

In this case after you have installed your profile you will have a multi-language site.

The second way is when you need to add multi-language capabilities to the site. Just download the module and enable it!

So go to our sf_multi_language module and see inside it.

sf_multi_language.info – no needs to present.
sf_multi_language.features.language.inccontains two languages that will add to the site, there are English and French.
sf_multi_language.module – is the most interesting.

1. In the hook install I enable translation for each content type:

variable_set('language_content_type_page',TRANSLATION_ENABLED);


In this example I do enable translation for basic content type page, for the article it will be

'language_content_type_article'

and so on.

2. I use hook_language_negotiation_info_alter() set URL language detection as default. It gives the opportunity to change site content by switching site URL from /en to /fr.

3. And in the end in hook block_info() I add language switcher block to the header region for bartik theme, in other cases it will be in the content region. But you can put it in any enabled regions. In other cases, block putting in first available region.

Try the module yourself at https://simplytest.me/ just enter in ‘project name’ field – sf_ml (the short name of module), choose the last version and launch the box.

After that the installation process starts…

In the module I added dependencies, so all required modules will downloaded, installed and enabled when installation process will finish.

We can see in the right top corner block with languages, it means that languages have been added successfully. Go inside and check other options.

Detection method is URL.

We can see that basic content types are translatable:

All items are checked and working. That’s great!

With this module, we’ve simplified a common request from clients. We’re saving time and money on these projects with this simple solution. And as you can see, you can programmatically set up any languages that you want. So take a look at our module and let us know how you like it!

Read more