Yel*_*bay 1 translation localization octobercms
你能告诉我如何获得当前的语言名称或类似的东西。
我想在基于October CMS的网站上自定义Locale Switcher。
收到类似的东西会很棒
...
{{ set var = ****.getLocale();}}
...
then use it for switch($var){}
...
Run Code Online (Sandbox Code Playgroud)
在Twig中,您可以使用访问当前语言,使用可以访问{{ activeLocale }}完整的语言名称,使用可以访问{{ activeLocaleName }}具有所有语言环境的数组{{ locales }}。
您可以{{ dump() }}用来查看页面上所有可用的变量。如果您尝试使用它,也可以在附近找到语言环境变量。
我尝试过使用activeLocale数组locales,但无法停止循环,因此我不可能有条件地渲染某些内容。这就是我解决这个问题的方法,在阅读了插件的工作原理之后,我在我的一个部分中做到了这一点。
==
use RainLab\Translate\Classes\Translator;
protected $translator;
function onStart()
{
$this->translator = Translator::instance();
$this['SelectedLanguage'] = $this->activeLocale = $this->translator->getLocale();
}
==
{% set CurrentLanguage = SelectedLanguage %}
Run Code Online (Sandbox Code Playgroud)
现在{{CurrentLanguage}}会给我当前语言的代码,所以现在使用 twig 我可以做一些像这样的条件渲染
{% if CurrentLanguage is same as('en') %}{% endif %}
{% if CurrentLanguage is same as('tr') %}{% endif %}
{% if CurrentLanguage is same as('gr') %}{% endif %}
Run Code Online (Sandbox Code Playgroud)
也许还有另一种解决方案。但这很有魅力。
更新:
虽然,在组件或其他地方,可以使用会话以这种方式检索当前语言,
Session::get('rainlab.translate.locale')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4550 次 |
| 最近记录: |