yidas/yii2-language 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yidas/yii2-language

Composer 安装命令:

composer require yidas/yii2-language

包简介

Yii 2 Framework Language extension with Status Keep and Mapping

README 文档

README

Yii 2 Language Extension


Yii 2 Framework Language extension with Status Keep and Mapping

Latest Stable Version Latest Unstable Version License

FEATURES

  • language Mapping integrated with Yii2 Language

  • Session & Cookie storage support

  • Yii2 i18n support

OUTLINE

REQUIREMENTS

This library requires the following:

  • PHP 5.4.0+
  • Yii 2.0.0+

INSTALLATION

Install via Composer in your Yii2 project:

composer require yidas/yii2-language

CONFIGURATION

Add a component using yidas\components\Language with configurations:

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.

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

USAGE

get()

Get Current Language

public string get($map=null)

Example:

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

You could get from map by giving map key as first argument:

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

set()

Set Current Language synchronised to \Yii::$app->language

public boolean set($language)

Example:

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

getByMap()

Get customized language value from $map

public string getByMap($mapKey)

Example:

If you have to echo HTML language value by current language:

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

setByMap()

Set by using customized language value from $map

public boolean setByMap($mapKey, $mapValue)

Example:

If you have to set current language by inputting a HTML language value:

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

isFirstCome()

First time coming check, which has no StorageRecord

Inverse alias with hasStorageRecord()

public boolean isFirstCome()

Example:

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:

<?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.

For example, get GET parameter to set language in on beforeAction function:

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.

统计信息

  • 总下载量: 69
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固