开发者

How to detect the current language of a Joomla! website?

开发者 https://www.devze.com 2023-01-09 18:11 出处:网络
I just want to generate a code that will detect the current language of my websiti开发者_高级运维n joomla+ phpSee getLanguage in JFactory:

I just want to generate a code that will detect the current language of my websit i开发者_高级运维n joomla + php


See getLanguage in JFactory:

$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();


In Joomla 3.4+, the answer by @MvanGeest still works. Here's a list of useful functions that exist on the language object:

  • Get a handle on the current language through an object of type JLanguage

    $currentLanguage = JFactory::getLanguage();
    
  • Get the current language name:

    $currentLanguageName = $currentLanguage->get('name');
    
    //OR
    
    $currentLanguageName = $currentLanguage->getName();
    
  • Check if RTL (which is the case of the Arabic language and some other languages)

    $isRTL = $currentLanguage->get('rtl');
    
    //OR
    
    $isRTL = $currentLanguage->isRtl();
    
  • Get the current language tag:

    $currentTag = $currentLanguage->get('tag');
    
    //OR
    
    $currentTag = $currentLanguage->getTag();
    
  • Get a list of all the known languages:

    $arrLanguages = $currentLanguage->getKnownLanguages();
    
0

精彩评论

暂无评论...
验证码 换一张
取 消