noname9/yii2-multilanguages

为Yii2框架提供的可翻译行为

安装: 30

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

1.1.2 2020-01-27 21:01 UTC

This package is auto-updated.

Last update: 2024-08-28 07:13:47 UTC


README

为Yii2框架提供可翻译的行为。

安装

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

运行以下命令

$ composer require noname9/yii2-multilanguages

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

"noname9/yii2-multilanguages": "~1.0"

迁移

运行以下命令

$ yii migrate/create create_post_table

打开/path/to/migrations/m_xxxxxx_xxxxxx_create_post_table.php文件,在up()方法中添加以下内容

$this->createTable('{{%post}}', [
    'id' => $this->primaryKey(),
]);

运行以下命令

$ yii migrate/create create_post_translation_table

打开/path/to/migrations/m_xxxxxx_xxxxxx_create_post_translation_table.php文件,在up()方法中添加以下内容

$this->createTable('{{%post_translation}}', [
    'id' => $this->primaryKey(),
    'post_id' => $this->integer()->notNull(),
    'language' => $this->string(8)->notNull(),
    'title' => $this->string(1024)->notNull(),
    'body' => $this->text(),
]);

配置

按照以下方式配置模型

use creocoder\translateable\TranslateableBehavior;

/**
 * ...
 * @property string $title
 * @property string $body
 * ...
 */
class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'translateable' => [
                'class' => TranslateableBehavior::className(),
                'translationAttributes' => ['title', 'body'],
                // translationRelation => 'translations',
                // translationLanguageAttribute => 'language',
            ],
        ];
    }

    public function transactions()
    {
        return [
            self::SCENARIO_DEFAULT => self::OP_INSERT | self::OP_UPDATE,
        ];
    }

    public function getTranslations()
    {
        return $this->hasMany(PostTranslation::className(), ['post_id' => 'id']);
    }
}

模型PostTranslation可以使用Gii生成。

用法

设置实体的翻译

为了设置实体的翻译

$post = new Post();

// title attribute translation for default application language
$post->title = 'Post title';

// body attribute translation for default application language
$post->body = 'Post body';

// title attribute translation for German
$post->translate('de-DE')->title = 'Post titel';

// body attribute translation for German
$post->translate('de-DE')->body = 'Post inhalt';

// title attribute translation for Russian
$post->translate('ru-RU')->title = 'Заголовок поста';

// body attribute translation for Russian
$post->translate('ru-RU')->body = 'Тело поста';

// save post and its translations
$post->save();

从实体获取翻译

为了从实体获取翻译

$posts = Post::find()->with('translations')->all();

foreach ($posts as $post) {
    // title attribute translation for default application language
    $title = $post->title;

    // body attribute translation for default application language
    $body = $post->body;

    // title attribute translation for German
    $germanTitle = $post->translate('de-DE')->title;

    // body attribute translation for German
    $germanBody = $post->translate('de-DE')->body;

    // title attribute translation for Russian
    $russianTitle = $post->translate('ru-RU')->title;

    // body attribute translation for Russian
    $russianBody = $post->translate('ru-RU')->body;
}

检查实体中的翻译

为了检查实体中的翻译

$post = Post::findOne(1);

// checking for default application language translation
$result = $post->hasTranslation();

// checking for German translation
$result = $post->hasTranslation('de-DE');

// checking for Russian translation
$result = $post->hasTranslation('ru-RU');

高级用法

收集表格输入

视图表单的示例

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin();

foreach (['en-US', 'de-DE', 'ru-RU'] as $language) {
    echo $form->field($model->translate($language), "[$language]title")->textInput();
    echo $form->field($model->translate($language), "[$language]body")->textarea();
}

//...

ActiveForm::end();

特定语言的翻译属性标签

模型属性标签的示例

class PostTranslation extends \yii\db\ActiveRecord
{
    public function attributeLabels()
    {
        switch ($this->language) {
            case 'de-DE':
                return [
                    'title' => 'Titel',
                    'body' => 'Inhalt',
                ];
            case 'ru-RU':
                return [
                    'title' => 'Заголовок',
                    'body' => 'Тело',
                ];
            default:
                return [
                    'title' => 'Title',
                    'body' => 'Body',
                ];
        }
    }
}