Load a custom language-file in your Joomla! template
When working with multi-lingual Joomla! sites, it is not uncommon that translations contain small mistakes, which you might want to change. But changing the original language-pack is actually the same as making a core hack - and that is dead wrong. But the Joomla! Framework offers you a way to include a custom language-file with just a few lines of code. Ideal to place in your template.
How Joomla! language files work
First of all, let's explain how the language files work in Joomla!. If you're not interested in this, just scroll down for the PHP-code. The basics are that for every language a separate subfolder in the directory language is used. For example:
JOOMLA/language/en-GB
JOOMLA/language/de-DE
JOOMLA/language/fr-FR
JOOMLA/language/nl-NL
Within this folder, the Joomla! core itself can be translated but third party extensions (components, modules, plugins and templates) as well. By default, Joomla! ships with a core file which is loaded first.
JOOMLA/language/en-GB/en-GB.ini
After this, the language files belonging to other extensions are loaded. So if you want to make changes to certain translations, editing the core-file makes no sense, because it might be overwritten by other extensions. Also, editing the core-file is a core hack, with which all your changes will be overwritten with the next Joomla! upgrade.
Using the template to override language strings
Now the trick is that of all extensions (components, modules, plugins, templates) actually the template-code is loaded at last. So if you create a language-file for a specific template, that language-file will have the chance to override any language-strings in previously loaded extensions.
JOOMLA/language/en-GB/en-GB.tpl_mytheme.ini
Overloading a language-file
Still, the language/en-GB folder is part of the core, and messing around in that folder is not what we want. Instead, we use our template to create template override of MVC-based components or modules. So why not create a template-override of a language file?
This is possible by loading a language-file with the following code. This code could be placed somewhere in the index.php file of your template - in this case, a template called mytheme.
$language =& JFactory::getLanguage();
$extension = 'mod_login';
$base_dir = JPATH_SITE;
$language_tag = 'en-GB';
$language->load($extension, $base_dir, $language_tag, true);
Or a short-cut version of this is:
JFactory::getLanguage()->load('mod_login', JPATH_SITE, 'en-GB', true);
This example reloads the English language-file for the Joomla!-login module. But it's not really useful to reload the same language-file twice. But the code shows you that the second argument of the load() function is the base-directory where Joomla! will look for the language file.
Now, if we create a file with the following location and change the $base_dir to our own template-directory, this will effectively override the previous language-files:
$language =& JFactory::getLanguage();
$extension = 'mod_login';
$base_dir = dirname(__FILE__);
$language_tag = $language->getTag(); // loads the current language-tag
$language->load($extension, $base_dir, $language_tag, true);
The file will be:
JOOMLA/templates/mytheme/language/en-GB/en-GB.mod_login.ini
Adding your own custom language-file
But the fun doesn't stop here. If you manually load a language-file, it is able to override any language-string in the system. So why not create our own custom language-file instead?
$language =& JFactory::getLanguage();
$language->load('custom' , dirname(__FILE__), $language->getTag(), true);
This code will look in the current directory (your template-directory?) for a folder with the following name, depending on the current language. If the file doesn't exist, Joomla! will just skip that file.
JOOMLA/templates/mytheme/language/en-GB/en-GB.custom.ini
JOOMLA/templates/mytheme/language/fr-FR/fr-FR.custom.ini
JOOMLA/templates/mytheme/language/de-DE/de-DE.custom.ini
This makes it possible to make any changes to language-strings, without making any core hack. Very nice.
Understanding the bootstrap
When overloading language-files, it is important to understand the Joomla! bootstrap: The language-loader will load any language-file that is not yet loaded. So if the core language-file is loaded after your own version, it will actually overwrite your own changes back to default. For instance, the template-code is executed before the actual modules are called. This means that you have to manually call the original language-file before calling your own.
For instance, if we want to override the language-file for mod_poll, the code to use is the following:
$language =& JFactory::getLanguage();
$language->load('mod_poll'); // this loads the original
$language->load('mod_poll', dirname(__FILE__), $language->getTag(), true); // this loads our own version
Hope this helps.
Created on Wednesday, 23 June 2010Modified on Friday, 25 March 2011
More tutorials in this section
- Load a custom language-file in your Joomla! template
- Starting with Nooku development on Linux
- MVC workflow with JPagination
