php 如何检测 Joomla 的当前语言!网站?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3352241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:26:15  来源:igfitidea点击:

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

phpjoomla

提问by leonyx

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

我只想生成一个代码,该代码将在 joomla + php 中检测我网站的当前语言

回答by MvanGeest

See getLanguage in JFactory:

请参阅JFactory 中的 getLanguage

$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.

获得语言后,您还可以检索区域设置/语言代码(例如en-US)。乔姆拉!语言可以有多个语言环境,所以你会得到一个数组。

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

如果您只想获取选定的语言标签(例如 pt-PT),您可以使用 getTag()

$lang = JFactory::getLanguage();
echo 'Current language is: ' . $lang->getTag();

回答by itoctopus

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

在 Joomla 3.4+ 中,@MvanGeest 的答案仍然有效。以下是语言对象上存在的有用函数的列表:

  • 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();
    
  • 通过类型对象获取当前语言的句柄 JLanguage

    $currentLanguage = JFactory::getLanguage();
    
  • 获取当前语言名称:

    $currentLanguageName = $currentLanguage->get('name');
    
    //OR
    
    $currentLanguageName = $currentLanguage->getName();
    
  • 检查 RTL(这是阿拉伯语和其他一些语言的情况)

    $isRTL = $currentLanguage->get('rtl');
    
    //OR
    
    $isRTL = $currentLanguage->isRtl();
    
  • 获取当前语言标签:

    $currentTag = $currentLanguage->get('tag');
    
    //OR
    
    $currentTag = $currentLanguage->getTag();
    
  • 获取所有已知语言的列表:

    $arrLanguages = $currentLanguage->getKnownLanguages();