赵文林/yii2-dot-translation

Yii2:用于翻译短文本的组件

安装: 555

依赖: 2

建议者: 0

安全性: 0

星标: 3

关注者: 2

分支: 1

开放问题: 0

类型:yii2-extension

v2.1.0 2018-02-28 13:32 UTC

README

Screen Shot

安装

安装此扩展的首选方式是通过Composer

运行以下命令之一

php composer.phar require --prefer-dist pavlinter/yii2-dot-translation "*"

或添加以下内容到您的composer.json文件的require部分。

"pavlinter/yii2-dot-translation": "*"

配置

  • 运行迁移文件

    yii migrate --migrationPath=@vendor/pavlinter/yii2-dot-translation/migrations
  • 更新配置文件

'bootstrap' => [
   'i18n',
   ...
],
'components' => [
    'i18n' => [
        'class'=>'pavlinter\translation\I18N',
        //'translations' => [
            //'myCategory' => [
                //'class' => 'pavlinter\translation\DbMessageSource',
                //'forceTranslation' => true,
                //'autoInsert' => true, //if message key doesn't exist in the database, message key will be created automatically
                //'dotMode' => null, //default state: show or hide dot
            //],
        //],
        //default settings

        //'dialog' => 'bs', //Bootstrap Modal Or jQuery Dialog (bs or jq)
        //'access' => 'dots-control',  //user permissions or function(){ return true || false; }
        //'nl2br' => true,
        //'dotClass' => 'dot-translation',
        //'dotSymbol' => '•',
        //'nl2br' => true //nl2br filter text
        //languages table
        //'langTable' => '{{%languages}}', //string || null if table not exist
        //'langColCode' => 'code',
        //'langColLabel' => 'name',
        //'langColUpdatedAt' => 'updated_at', //string || null
        //'langWhere' => ['active' => 1], //$query->where(['active' => 1]);
        //'langOrder' => 'weight', //$query->orderBy('weight');

        //'enableCaching' => true, //for langTable cache
        //'durationCaching' => 0, //langTable cache
        //'router' => '/site/dot-translation', //'site' your controller
        //'langParam' => 'lang', // $_GET KEY
    ],
    ...
],
  • 更新控制器
//SiteController.php
public function actions()
{
    return [
        'dot-translation' => [
            'class' => 'pavlinter\translation\TranslationAction',
            //'adminLink' => null;  //array|string|function
            //'htmlEncode' => true, //encode new message
            //'access' => null, //default Yii::$app->i18n->access
        ],
        ...
    ];
}

用法

更改语言

/index.php?r=site/index&lang=ru

示例

echo Yii::t('app', 'Hello world.'); used global settings

//individual adjustment
echo Yii::t('app', 'Hi {username}.', ['username' => 'Bob', 'dot' => true]); //enable dot

echo Yii::t('app', 'Hello world.', ['dot' => false , 'nl2br' => false]); //or 'br' => false //disable dot and disable nl2br filter

echo Html::submitInput(Yii::t('app', 'Submit', ['dot' => false])); //disable dot

echo Yii::$app->i18n->getPrevDot(); // show previous dot

// Or

echo Yii::t('app', 'Submit', ['dot' => '.']); //show only dot
Yii::$app->i18n->disableDot(); //force disable all dots

echo Yii::t('app', 'Submit'); //hidden dot
echo Yii::t('app', 'Submit', ['dot' => true]); //force enable dot

Yii::$app->i18n->resetDot(); //reset settings
Yii::$app->i18n->enableDot();//OR enable dot
//Add description variables to popup.
echo Yii::t('app', 'Count: ', ['dot' => true, 'var' => ['count' => 'Count email', 'owner']]);
//render languages
echo \yii\widgets\Menu::widget([
    'items' => Yii::$app->i18n->menuItems(),
    'encodeLabels' => false,
]);
//get array with current url
Yii::$app->i18n->getLanguages(true); //true|callable
/*
Array
(
    [1] => Array
    (
        [id] => 1
        [code] => en
        [name] => English
        [image] => /files/en.png
        [weight] => 130
        [active] => 1
        [updated_at] => 1424504802
        [url] => /en/site/index
    )
    ...
)
*/