Control modules based on JoomFish or front-end language

(9 votes, average 3.89 out of 5)

Joomla is capable of being set up in various languages. These recipes demonstrate how to display different modules depending on the “front-end language” as chosen by the administrator, or by the user via a language selector (e.g. via JoomFish). They also demonstrate how to display different modules based on the language set up in the user’s browser, so as to target your users with modules in their own language.

Using MetaMod to control modules by language is a useful addition to JoomFish’s translation capabilities. Instead of just translating each item in a menu into each language, just set up an entire “duplicate menu” for each alternative language, and use MetaMod to switch the entire menu to a different one, depending on the language the user has selected.

This has the additional advantage that you can have some completely different menu items in each language which is often useful, as the audiences in each language might be quite different.

Of course, this depends on the menus being controlled in Module Positions – note that some templates out there (e.g. RocketTheme) embed the main menus into the template itself, so cannot be controlled with MetaMod.

  • JoomFish / Front End Language mode.
    • Set the “$language handling” parameter in MetaMod to “Joomla front-end language”
    • This sets the variable $language to whichever language is installed, and chosen by the user (or default if they haven’t selected a language manually)
if ( $language == "en-gb" ) return 101; /* British English only */
if ( $language_code == "en" ) return 102; /* any variant of English */
/* or, any language in Switzerland: de-ch, fr-ch, it-ch: */
if ( $language_region == "ch" ) return 103;
  • Browser Language Detection mode (simple)
    • Set the “$language handling” parameter in MetaMod to “Browser’s preferred language”
    • This sets the variable $language (and $language_code and $language_region) to the “top” language specified in the user’s browser, disregarding any secondary languages.
    • You can then use rules like the ones above.
  • Browser Language Detection mode (advanced)
    • Set the “$language handling” parameter in MetaMod to “Most preferred language from supplied list”
    • Enter the set of languages that you have modules for, into the box marked “Preferred list of languages”.
    • $language will contain the language from this list that appears highest in the browser’s preferred language settings.
    • E.g. a browser may be set up for “ar,fr”. If you only test for the first language in the list, but have no content to display for Arabic (ar), you might miss the opportunity to present your French (fr) content. So by specifying a list of the languages you do cater for here, MetaMod will return the one that ranks highest for a particular visitor. If “Strict language comparison” is turned off, then specifying a major language here (e.g. fr) will match any variants of that in the user’s browser, such as fr-fr, fr-ch, fr-be or plain fr.
    • If you entered "ar,fr" because you have modules for Arabic and French, then you could use the following rules:
if ( $language == "ar" ) return 101; /* Arabic */
if ( $language == "fr" ) return 102; /* French */