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

后端 未结 2 484
慢半拍i
慢半拍i 2020-12-13 18:18

I just want to generate a code that will detect the current language of my websit in joomla + php

相关标签:
2条回答
  • 2020-12-13 18:36

    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 讨论(0)
  • 2020-12-13 18:44

    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();
    
    0 讨论(0)
提交回复
热议问题