Specify different site language for specific page with a plugin

I had a customer who wanted to specify a different site language for their events page made with Matukio. His main language was German and he had a single page, which had to be in English. I've wasted more hours than I'm comfortable to admit on this issue and that is why I thought that I'll share the solution with you, in case you need it.

If we were dealing with a standard multilingual site, the solution to this issue would be pretty straight forward. Define a new content language, enable the language filter plugin, create a menu item for our page and set the language to English. If we go this way we run into the problem that once a user lands on the English version of the page, all links on the site will get /en appended to them. But we don't want this. We want that the links stay as they are.

First I thought okay, I could do:

$lang = JFactory::getLanguage();
$lang->load('extension', JPATH_SITE, 'en-GB', true);

I would call those lines after I load the extension files for this page and everything would be fine. Basically the load function would load the language files for en-GB and since the override parameter is set to true, it will override the previously loaded German files. This kind of worked, but it had the unpleasant side effect -> I had to define each file for override and I had to make sure that my override is loaded right after the original language load call. In our Compojoom library we have a language helper that triggers a special event allowing me to override the language file. However when we deal with plugins or modules, there we don't necessary load the language files though that helper. We leave it to up to Joomla to load the files as it sees fit. And that is why this solution was not good.

Fortunately, there is a simple way. Instead of trying to override every loaded language file on the page, why not just set the default language for this page to English? And this is how it is done:

public function onAfterRoute() {
        $appl = JFactory::getApplication();
        if ($appl->input->get('option') == 'com_matukio')
        {
            JFactory::getLanguage()->setLanguage('en');
        }
    }

If the page we are on is part of matukio, we set the default language to English! Then joomla will load only the English language files on the page! Voila!

If you know a better way to do this, then please let us know in the comments below!

 

Rate this blog entry:
0
14.03.2015 - A German Pizza, Bugs and Fun event - ...
Keep your Joomla menus in order!