Yii::t()

I have a doubt about Yii::t.

I know the standard way to declare a translation is for example for the file messages/pt-PT/main.php




return 

[

"Login here" => "Entrar"

];



If the app language is en-US it will display “Login here” if it’s pt-PT it will display “Entrar”.

But suppose I want the same phrase to have different translations in portuguese:


"Login" => "Entrar",

"Login" => "Iniciar sessão"

So what I thought was to create a en-US.php file with for example:


"login" => "Login",

"login2" => "Alternative login"

But it doesn’t work, it never reads from the en-US.php file and always outputs “login” and “login2”.

Why if the default language is en-US and I have a folder messages/en-US the files there are not used?

Thanks

I’m not quite sure what are you talking about. Mine works.

Default language (that is, when no language is set in config) seems to be just ‘en’.

PS.

Probably you meant ‘/en-US/main.php’ file?

I mean if you have a file "messages/en-US/main.php with:


"login" => "Login"

and "language" => "en-US" in main.php

Yii::t("main", "login")

will print

‘login’ when it should print ‘Login’

Hmm.

Maybe something’s wrong in your installation or config.

Mine looks like this:


'i18n' => [

    'translations' => [

        'app' => [

            'class' => 'yii\i18n\PhpMessageSource',

            'basePath' => '@app/messages',

        ],

        'label' => [

            'class' => 'yii\i18n\PhpMessageSource',

            'basePath' => '@app/messages',

        ],

    ],

],

So if I create messages/en-US/app.php and set language to en-US, all Yii::t(‘app’, ‘my_message’) are translated correctly.

Found it




     'i18n' => array(

            'translations' => array(

                'app*' => array(

                    'class' => 'yii\i18n\PhpMessageSource',

                    'basePath' => "@app/messages",

                    'sourceLanguage' => 'en-US',

                    'fileMap' => array(

                        'app' => 'app.php',

                        'app/common' => 'common.php',

                        'app/countries' => 'countries.php',

                        'app/menu' => 'menu.php'

                    )

                ),

            )

        ),

The problem was with "sourceLanguage", once I commented it, everything worked as expected.

Anyway, thanks for your help.