2017 © Pedro Peláez
 

yii2-extension yii2-language

Language extension component for Yii 2 framework

image

yidas/yii2-language

Language extension component for Yii 2 framework

  • Tuesday, July 17, 2018
  • by yidas
  • Repository
  • 1 Watchers
  • 1 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 117 % Grown

The README.md

, (*1)

Yii 2 Language Extension


Yii 2 Framework Language extension with Status Keep and Mapping, (*2)

Latest Stable Version Latest Unstable Version License, (*3)

FEATURES

  • language Mapping integrated with Yii2 Language, (*4)

  • Session & Cookie storage support, (*5)

  • Yii2 i18n support, (*6)


OUTLINE


REQUIREMENTS

This library requires the following:, (*7)

  • PHP 5.4.0+
  • Yii 2.0.0+

INSTALLATION

Install via Composer in your Yii2 project:, (*8)

composer require yidas/yii2-language

CONFIGURATION

Add a component using yidas\components\Language with configurations:, (*9)

return [
    'bootstrap' => ['log', 'lang'],
    'language' => 'en-US',
    'components' => [
        'lang' => [
            'class' => 'yidas\components\Language',
            'languages' => [
                0 => 'en-US',
                1 => 'zh-TW',
                2 => 'zh-CN',
            ],
            'maps' => [
                'html' => [
                    0 => 'en',
                    1 => 'zh-Hant',
                    2 => 'zh-Hans',
                ],
            ],
            // 'storage' => 'session',
            // 'storageKey' => 'language',
        ],
        ...
property Type Default Description
languages array As example Supported language list
maps array As example Customized language map
storage string 'session' Storage carrier: 'session' or 'cookie'
storageKey string 'language' Storage carrier Key

Bootstrap

You could add the language component into bootstrap for keeping the language storage work such as Seesion and Cookie., (*10)

// `lang` component for example
return [
    'bootstrap' => ['lang'], 
    ...

USAGE

get()

Get Current Language, (*11)

public string get($map=null)

Example:, (*12)

echo \Yii::$app->lang->get();  // en-US

You could get from map by giving map key as first argument:, (*13)

echo \Yii::$app->lang->get('html');  // en

set()

Set Current Language synchronised to \Yii::$app->language, (*14)

public boolean set($language)

Example:, (*15)

\Yii::$app->lang->set('zh-TW');

getByMap()

Get customized language value from $map, (*16)

public string getByMap($mapKey)

Example:, (*17)

If you have to echo HTML language value by current language:, (*18)

echo \Yii::$app->lang->getByMap('html');  // en

setByMap()

Set by using customized language value from $map, (*19)

public boolean setByMap($mapKey, $mapValue)

Example:, (*20)

If you have to set current language by inputting a HTML language value:, (*21)

$this->setByMap('html', 'zh-Hant');

isFirstCome()

First time coming check, which has no StorageRecord, (*22)

Inverse alias with hasStorageRecord(), (*23)

public boolean isFirstCome()

Example:, (*24)

if (Yii::$app->lang->isFirstCome()) {
    // Detetmine user ip to set current language
} 
else if (Yii::$app->lang->hasStorageRecord()) {
    // Means !(Yii::$app->lang->isFirstCome())
}

IMPLEMENTATION

Controller for Changing Language

You could add a controller or action for changing language like /language?language=zh-TW:, (*25)

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;

/**
 * The Controller for Language converting
 */
class LanguageController extends Controller
{
    public function actionIndex($language='')
    {
        $result = Yii::$app->lang->set($language);

        return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
    }
}

BeforeAction for globally changing language

You could globally set language by handling language setting in the bootstrap of application., (*26)

For example, get GET parameter to set language in on beforeAction function:, (*27)

return [
    'on beforeAction' => function ($event) {
        // Always fetch language from get-parameter
        $lang = \Yii::$app->request->get('lang');
        // Set to given language with get-parameter
        if ($lang) {
            $result = \Yii::$app->lang->set($lang);
        }
    },
    ...
]

After that, by giving lang param from any url like /post/my-article?lang=zh-TW would change language., (*28)

The Versions

17/07 2018

dev-master

9999999-dev

Language extension component for Yii 2 framework

  Sources   Download

MIT

The Requires

 

framework language yii2 i18n language-map

16/07 2018

1.0.1

1.0.1.0

Language extension component for Yii 2 framework

  Sources   Download

MIT

The Requires

 

framework language yii2 i18n language-map

24/04 2018

1.0.0

1.0.0.0

Language extension component for Yii 2 framework

  Sources   Download

MIT

The Requires

 

framework language yii2 i18n language-map