Meta Matters

Comments and discussion of (mostly) Joomla-related topics by the author of MetaMod and Chameleon.

May 13
2012

Automatically switch language in Joomla 2.5

Posted by: metamodguy

Tagged in: Untagged 

cham-160

I've often been asked if it's possible to automatically switch languages in Joomla 2.5.

Why might you want to do this?

  • to correlate languages and templates, so that forcing a certain template also forces a certain language
  • to correlate languages and domains, so that going to a certain domain (e.g. mydomain.de) forces the site into German
  • to correlate languages and domains, so that if the user switches to a certain language, this will redirect to a certain domain (e.g. clicking on a the French flag in the language switcher will also redirect the browser to mydomain.fr)

As of Chameleon 2.26, you can now force a language switch as a result of any rule. Furthermore, you can specify an alternative domain name so that the language switch will redirect to a different domain entirely, instead of remaining on the same domain.

While this is good news, and very useful, there are a few "gotchas" that I want to talk about.

1 - This feature emulates the "language switcher" module (with the addition of switching domains).

The down-side of this is that in Joomla 2.5, the language switcher module has a very strict idea of what it means to switch language for any given page. That is, you have to set up Menu Item Associations from every menu item in a particular language, to its corresponding menu item in any other language(s).

If you don't set these up for each and every menu item, the language switcher will simply jump back to the default menu item for the language you clicked on, for any pages you didn't assign. If you think about it this does make sense, but it's just very different from JoomFish on Joomla 1.5.

An associated issue is that if you are on a page "inside" one of the menu items, then forcing a language change doesn't simply replace the language code in the URL with the new language, but redirects to the corresponding menu item in the new language. e.g. if the menu item is an article category view and you have clicked on an article in the list, then switching the language from there will take you back to the category listing view in the new language. There is no built-in correspondence in Joomla 2.5 between an article and a translation of that article.

2 - You can't remove the language prefix from all languages

When you set up Joomla 2.5 multi-lingually, you can make some settings in the "System - Language Filter" plugin. One of these (Remove URL Language Code) is whether the default language on the site will receive the language prefix in its URLs.

e.g. with removal turned on and a default language of en-GB, the English URLs will be of the form mysite.com/about-us, while the French URLs will be of the form mysite.com/fr/aboutus.

If you're wanting to use 1 domain for 1 language, and another domain for another language (which is now possible with Chameleon), this becomes a problem. It means that your primary language domain can have no prefixes, but any other domains are forced to use prefixes. e.g. mysite.com/about-us but mysite.fr/fr/aboutus.

Sorry, there's nothing that Chameleon can do about this, apart from making a new version of the Language Filter plugin's router. That may be the best bet in the long run.

3 - It probably has issues with sh404SEF and other 3rd-party SEF extensions

Sorry, it's currently untested on other SEF extensions.

HOWTO: Force all pages on a certain domain to use a particular site language

In this example we're going to set it up so that if someone goes to mysite.de, the site will be in German; mysite.com in English and mysite.fr in French.

1 - Set up cPanel (or similar) so that your site responds to all 3 domain names: mysite.de, mysite.com and mysite.fr

This is usually done with "parked domains" in cPanel.

2 - Set up your site and content in Joomla 2.5 such that the multi-lingual elements work properly.

This involves turning on the "System - Language Filter" plugin and configuring it, setting up new menu structures for each language, giving each language a default menu item, and setting up corresponding menu items between each of the new menu structures. This is all out of the scope of this example.

3 - Add a rule in Chameleon that detects mysite.de as the domain name.

This part is reasonably straight forward. You're just going to set up a rule that says "is the user on mysite.de? If so, force the language to German". To do this, enter mysite.de as the domain name in the Environment section of a Chameleon rule. This will trigger the Succeed action in this rule any time someone views a page on that domain name.

In the Succeed action, use the Language selector to choose "German", and save the rule. There's no need to set a new protocol or domain name as the user is already on that domain.

4 - Add new Chameleon rules for mysite.fr and mysite.com

As you did for mysite.de and German, set up 2 new rules to handle French and English. In the French one you'll detect mysite.fr, then in its Succeed action you'll force the French language. In the English one you'll detect mysite.com, and in its Succeed action you'll force the English language.

Discussion

In this example you can't use the language switcher module. Why? Because the language switcher will attempt to stay on the same domain but just switch the language. If that happens, then the rules you have set up will detect the domain and force the language back to the default you set for that domain.

There's nothing to stop you putting some links into your content or onto your pages to link to your other sites, however.

In general however, this setup is all about presenting separate sites and domains for each language. If people jump from site to site, the language will follow them without any problem.

Don't forget that using Chameleon you can also select a different template or template style for each language/domain – just use the dropdown at the top of the Succeed action. You can also select some menu items to disable for each domain/language, to customise the look & feel even more precisely.

HOWTO: Force the Language Switcher module to send different languages to different domains

In this example we're going to retain the use of the language switcher. But this time, when someone chooses a new language, Chameleon will redirect the browser to a different domain.

We'll use mysite.com for English, mysite.de for German, and mysite.fr for French.

1 - (as above) Set up cPanel (or similar) so that your site responds to all 3 domain names: mysite.de, mysite.com and mysite.fr

This is usually done with "parked domains" in cPanel.

2 - (as above) Set up your site and content in Joomla 2.5 such that the multi-lingual elements work properly.

This involves turning on the "System - Language Filter" plugin and configuring it, setting up new menu structures for each language, giving each language a default menu item, and setting up corresponding menu items between each of the new menu structures. This is all out of the scope of this example.

3 - Add a rule in Chameleon that detects English as the language

What we're doing here is to set up a rule that says "is the user viewing something in English? If so, redirect the page to mysite.com". To do this, enter "en" as the language in the Environment section of a Chameleon rule. This will trigger the Succeed action in this rule any time someone views a page set up in English.

In the Config section of the Chameleon rule, ensure that "$language handling" is set to "Joomla Front-end Language".

In the Succeed action, set the Language to English (yes, you need to force this even though it was already in English), and enter "mysite.com" in the "Language redirect domain" box.

4 - Add new Chameleon rules for French and German

As you did for "en" (English) and mysite.com, set up 2 new rules, one for French (fr) and one for German (de). In the Succeed action for each of these, ensure that you force the language, and set the corresponding domain in the "Language redirect domain" box (mysite.fr for French, and mysite.de for German).

Discussion

Let's say you are on the page mysite.com/contact-us, and click on the French flag in the language switcher. The language switcher will redirect you to mysite.com/fr/contactus which may appear to be wrong... however then Chameleon picks up the URL and sees that the Menu Item is a French one. It then redirects the user to mysite.fr/fr/contactus which is the right URL.

Without rewriting the Language Switcher module and/or writing a new router inside the Language Filter plugin, this double redirect can't be helped. The redirects are fast, however, and get you to the right domain and URL, so most people won't even notice.

HOWTO: Detect people from a certain country and force a certain language for them

In this example we will use the previous example as a basis (allowing the language switcher), but add some rules at the beginning to detect the country and preset the language.

1 - Download the GeoIP databases from the Config page in Chameleon.

2 - Set up a rule to detect people from France, and redirect to mysite.fr

In this rule we will detect 2 things: (1) is the person from France, and (2) have we already done this detection? This 2nd question will allow Joomla to only redirect people once. Then if they choose to return to the original site, a cookie or session variable will alert Joomla not to redirect them back again.

a - in the Config section of a Chameleon rule, select Enable GeoIP/GeoCity = GeoIP Country.

b - in the Quick Rules section of the Chameleon rule, enter "fr" into the "Only these countries" box. If there are any other countries that you want to force to use French, then enter all the country codes separated by commas, e.g. fr,be

c - in the PHP box of the Chameleon rule, enter the following:

if ( isset($_SESSION['FRANCE_REDIRECT']) ) return false;

d - in the PHP box of the Succeed action of the Chameleon rule, enter the following: 

$_SESSION['FRANCE_REDIRECT'] = true;

e - in the Language box in the Succeed action of the Chameleon rule, choose French, and enter "mysite.fr" into the "Language redirect domain" box.

3 - Repeat previous step for English and German.

In the PHP boxes, change the text FRANCE_REDIRECT in the 2 PHP boxes to ENGLAND_REDIRECT and GERMANY_REDIRECT as appropriate for each language and country/countries. Ensure that you are detecting the right set of countries in each step, and that you're redirecting to the right language and/or domain in the Succeed action.

4 - Add all the steps from "Howto: Force the Language Selector module to send different languages to different domains"

The order of the steps is important. Please ensure that the steps are in the order presented here (GeoIP-based steps first, followed by the language detection steps).


blog comments powered by Disqus