Roby Sottini
Roby Sottini

Reputation: 2265

Yii2 framework: How to change English default language to Spanish

I am using the Yii2 Framework and I am translating all texts of buttons, labels, messages, etc.

Then I read this article http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html that shows how to do it automatically but I don't understand it.

I want to translate to Spanish from Argentina: es-AR or at least to any Spanish.

So I think I need to change from en-US to es-AR but I would like to know which files should I change.

Also I am using the great Gii code generator where I can see a checkbox called Enable I18N.

I watched these files but I am not sure if I am looking the right files:

Upvotes: 2

Views: 6570

Answers (2)

Anton Rybalko
Anton Rybalko

Reputation: 1314

Add language propery and i18n component in application config. For advanced application template in common/config/main.php

return [
    'language' => 'es-AR',
    ...
    'components' => [
        ...
        'i18n' => [
            'translations' => [
                'app*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@app/messages',
                ],
            ],
        ],
        ...
    ],
]

Use Yii::t() for all user messages (model labels, views, error messages etc).

echo \Yii::t('app', 'Friend');

Create directory messages/es-AR. Create file app.php in this directory and add translations

return [
    'Friend' => 'Amigo',
    'Girl' => 'Сhica',
    ...
];

Upvotes: 4

Dmitry
Dmitry

Reputation: 271

Try to look into the official documentation, it is best tutorial for you. http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html

Also, look at this answer yii2 basic multiple language

You can change default language by changing 'language' parameter of your main configuration file. Like this: return

[
    // set target language to be English
    'language' => 'en-US',
]

Where instead 'en-US' you must to set needed locale code, e.g. 'es-AR'

Upvotes: 1

Related Questions