A fool- and SEF-proof link to homepage with core Joomla multilanguage

What? Is not that simple as place a <href="index.php">Home</a> and you're done?

Yes and no... The Joomla routing engine is smart enough, to redirect all such above links to the default menu item in most cases. But using the core multi-language engine in Joomla 1.7+ has created a little-bit different situation. Let me explain!

The problem is crated by the settings needed to make Joomla core Multilingual engine work. In nutshell, there are the crucial steps leading to the problems in detecting the homepage for the given language: First of all you need to create a menu item assigned to all available languages, and choose it as classic Joomla Homepage/default page. Then, after you set up the rest of multi-language related things, you need to choose a default menu item for EACH used language. These menu items will act as the Home menu items/home pages for the given language.

So, using the code from the first sentence to link your logo for example in your template to the homepage is far to be a foolproof solution. The situation getting worse, if you add to the mix a third party SEF engine, like sh404SEF. For this case you need to use a fool- and SEF proof PHP code!

Let's see how this can be done. First of all you need to determine the active language on any given page:

$lang = JFactory::getLanguage();

Then, you need to detect the default menu item for the active language:

$app = JFactory::getApplication();
$menu = $app->getMenu();
$home = $menu->getDefault($lang->getTag());

Now you have all necessary ingredients, you need only to build up the link to be used wherever you need it:

$homelink=JRoute::_($home->link."&Itemid=".$home->id);

And here you go! You have a solid code which works in a multilingual Joomla site, regardless if you have or not the core SEF enabled, and did you added to the mix a third party SEF component or not.