Sunday, January 9, 2022

[FIXED] CakePHP 3: How to get current language value in views?

Issue

From CakePHP 3 Book

// Before (CakePHP 2)
Configure::write('Config.language', 'fr_FR');

// Now
I18n::locale('en_US');

Before in our CakePHP 2 app

<?php if(Configure::read('Config.language') != 'hrv') {  ?>
    <li>
        <a href="<?php echo $this->Html->url('/hrv'); ?>" class="flag flag-hr">
        <?php echo __('Hrv', true); ?>
        </a>
    </li>
<?php } ?>

How now in CakePHP 3?

in bootstrap:

ini_set('intl.default_locale', 'hr_HR');

in View?


Solution

Quote from the I18n::getLocale() docs:

[...] Will return the currently configure locale as stored in the intl.default_locale PHP setting. [...]

https://api.cakephp.org/3.5/class-Cake.I18n.I18n.html#_getLocale

So

use Cake\I18n\I18n;

if (I18n::getLocale() !== 'hr_HR') // ...


Answered By - ndm

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.