Reputation: 617
I have a Joomla multi-language site (FR/EN), all menus and contents work with these two languages. However, in my PHP scripts en-GB is always selected:
$lang = JFactory::getLanguage();
$language_tag = $lang->getTag();
echo($language_tag);
return always en-GB ?
Although the menus and breadcrumbs follow correctly the selected language in language filter module.
Upvotes: 3
Views: 1902
Reputation: 38584
$lang = JFactory::getLanguage();
echo 'Current language is: ' . $lang->getName();
Once you have the language, you can also retrieve the locale/language code (e.g. en-US
). Joomla! languages can have multiple locales, so you'll get an array.
$lang = JFactory::getLanguage();
foreach($lang->getLocale() as $locale) {
echo 'This language supports the locale: ' . $locale;
}
If for some reason, you are only interested in the first locale, you can simply grab the first element. You will probably need an array, like this:
$lang = JFactory::getLanguage();
$locales = $lang->getLocale();
echo 'This language\'s first locale is: ' . $locales[0];
If you just want to get the selected language tag (e.g. pt-PT) you can use getTag()
$lang = JFactory::getLanguage();
echo 'Current language is: ' . $lang->getTag()
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.
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
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
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.
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.
Upvotes: 3
Reputation: 131
I think that if you try to access the language class outside the application, it will not work (it will always return the default language). You might have to instantiate the application. See this question for more infos : https://joomla.stackexchange.com/questions/5036/function-getlanguage-does-not-return-current-language
Upvotes: 1