Reputation: 143
Could you tell me please how i can get current language name or somthing like this.
I want customize Locale Switcher on web site based on October CMS.
It will be greate to recevie something like
...
{{ set var = ****.getLocale();}}
...
then use it for switch($var){}
...
Upvotes: 4
Views: 9957
Reputation: 1497
i tried with activeLocale
and also with locales
array but one can not stop the loop and thus it was impossible for me to render some content conditionally. this is how i solved it, after reading the logic of plugin that how it is working, I did this in one of my partials.
==
use RainLab\Translate\Classes\Translator;
protected $translator;
function onStart()
{
$this->translator = Translator::instance();
$this['SelectedLanguage'] = $this->activeLocale = $this->translator->getLocale();
}
==
{% set CurrentLanguage = SelectedLanguage %}
now {{CurrentLanguage}}
will give me code for current language so now using twig i can do some conditional rendering like this
{% if CurrentLanguage is same as('en') %}{% endif %}
{% if CurrentLanguage is same as('tr') %}{% endif %}
{% if CurrentLanguage is same as('gr') %}{% endif %}
Maybe there could be a another solution. but this one worked like charm.
Update:
Although, In case of components or elsewhere one can use session to retrieve the current language this way,
Session::get('rainlab.translate.locale')
Upvotes: 6
Reputation: 857
Assuming that you use RainLab.Translate plugin?
Create a partial that uses the localePicker component and then use your custom code:
<div>{{ activeLocale }} - {{ activeLocaleName }}</div>
{% for code, name in localePicker.locales %}
<div>{{ code }} - {{ name }}</div>
{% endfor %}
And just call that partial from where you want to use it.
Upvotes: 2
Reputation: 550
In twig you can access the current language with {{ activeLocale }}
, the full language name with {{ activeLocaleName }}
and an array with all locales available with {{ locales }}
.
You could use {{ dump() }}
to see all variables available on a page. If you try it you will find the locale variables right there as well.
Upvotes: 7