pkorniev/yii2-language

Simple language extension for Yii2.

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

v1.1 2021-09-17 14:44 UTC

This package is auto-updated.

Last update: 2024-04-17 20:19:24 UTC


README

Language extension for yii2 application.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require  pkorniev/language-yii2 

or add

"pkorniev/language-yii2": ">=1.0"

to the require section of your composer.json file.

Usage

Once the extension is installed, you need to override the application components "request" and "urlManager" and add the component "translator" (it will store translation parameters) in the main configuration file :

...
'components' => [
        'request' => [
            'class' => 'pkorniev\language\PKRequest',
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => '<<YOUR_COOKIE_VALIDATION_KEY>>',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'class' => 'pkorniev\language\PKUrlManager',
        ],
        'translator' => [
                    'class' => 'pkorniev\language\PKTranslator',
                    'supportedLanguages' => [
                        'ua' => 'uk-UA',
                        'en' => 'en-US',
                    ],
                    'defaultLanguage' =>'ua',
                    //'cookieRemember' => false,
                    //'expiredLangCookie' => 60*60*24*30,
                ],
...                

The "supportedLanguages" property specifies a list of supported locales. The keys contain user keys that need to be passed in the query string to go to the corresponding locale.

For example, to switch to English, you can use any key to your liking: "en", "eng" or any other. The link to your site with a transition to any language will look like this:

https://site.com/en/ or https://site.com/eng/

The "defaultLanguage" property specifies which locale to set as the default. Important! The default locale must be declared in the list of available ones.

The "cookieRemember" property specifies whether or not to store the previously set locale in the Cookies. If it is set to true (the default value), then the locale will be set to the one that was the last time it was changed and does not change even if the user key is not passed in the query string.

Well, the "expiredLangCookie" property sets the expiration date of the last locale in seconds. It can be omitted, by default it is equal to 30 days.