Yii Framework Forum: Not loading correct language - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Not loading correct language

#1 User is offline   prchakal 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 20-January 10

Posted 23 November 2017 - 08:53 PM

Hi,

Im developing a new project to community that is a all-in-one project with admin already configure with docker and more things. I have stopped in a part that i debug but dont understand why it happens on Yii2.

I have a folder with all my translations in "pt-BR" and "en-US". And have a configuration to translate all messages from frontend to the frontend/messages folder. When i set language to "pt-BR" is get all messages from "pt-BR" folder. If i set to "en-US" it dont get the messages from "en-US" folder. If i set to "en", it get the message from "en-US" folder.

1 - message configuration:

'i18n' => [
    'translations' => [
        'frontend*' => [
            'class' => 'yii\i18n\PhpMessageSource',
            'basePath' => '@frontend/messages',
        ],
    ],
],


2 - My folders structure is attached.

3 - My bootstrap code:

'bootstrap' => [
        'log',
        [
            'class' => 'backend\components\LanguageSelector',
            'supportedLanguages' => ['pt-BR', 'en-US'],
        ],
]


4 - My language loader class:

<?php

namespace backend\components;

use yii\base\BootstrapInterface;

class LanguageSelector implements BootstrapInterface
{

    public $supportedLanguages = [];

    /**
     * @inheritdoc
     */
    public function bootstrap($app)
    {
        $app->language = 'en-US';   // en = works, en-US = dont works, pt-BR = work
    }

}


Can anyone help me with it?
0

#2 User is offline   prchakal 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 20-January 10

Posted 23 November 2017 - 08:59 PM

I see that have a line of code inside MessageSource.php that is:

if ($this->forceTranslation || $language !== $this->sourceLanguage) {
    // translate ...
}


But i dont know why the framework dont let get the message from en-US folder or my folder need have a different name? My translations are not phrases, but a key, like "Model.CreatedAt".

Can anyone help me with it?
0

#3 User is offline   Fabrizio Caldarelli 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,065
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 24 November 2017 - 02:07 AM

Exactly. In your case, sourceLanguage and targetLanguage are the same, so you need to set forceTranslation flag to true.

http://www.yiiframew...nslation-detail

It could be useful to allow language recognition by browser configuration:

<?php

namespace frontend\components;

use yii\base\BootstrapInterface;

class LanguageSelector implements BootstrapInterface
{
    public function bootstrap($app)
    {
        $supportedLanguages = array_keys(\Yii::$app->params['languages']);
        
        if(\Yii::$app->session->has('language') && (in_array(\Yii::$app->session->get('language'), $supportedLanguages)))
        {
            $app->language = \Yii::$app->session->get('language');
        }
        else
        {
            $preferredLanguage = $app->request->getPreferredLanguage($supportedLanguages);
            $app->language = $preferredLanguage;
        }
    }
}
?>


where supportedLanguages are defined in frontend/common/params.php:

<?php
return [
    // Languages
    'languages' => [
        'en' => [ 'code' => 'en', 'label' => 'English' ],
        'it' => [ 'code' => 'it', 'label' => 'Italiano' ],
        'tr' => [ 'code' => 'tr', 'label' => 'Türkçe' ],
        'ro' => [ 'code' => 'ro', 'label' => 'Română' ],
        'zh' => [ 'code' => 'zh', 'label' => '中文' ],
    ],
];


Finally, let me to suggest my extension to configure languages translations dinamically:

https://github.com/F...lations-manager
Yii2 by example: my book about Yii2
https://www.packtpub...nt/yii2-example
0

#4 User is offline   prchakal 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 100
  • Joined: 20-January 10

Posted 24 November 2017 - 09:44 PM

Thanks. It is working now:

'i18n' => [
            'translations' => [
                'frontend*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'forceTranslation' => true,
                    'basePath' => '@frontend/messages',
                ],
            ],
],

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users