百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Y

yii2-translateable

> 编程语言
开源

Yii 框架的可翻译行为。

99 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Yii 框架的可翻译行为。

Translateable Behavior for Yii 2

A modern translateable behavior for the Yii framework.

Installation

The preferred way to install this extension is through composer.

Either run

bash
$ composer require creocoder/yii2-translateable

or add

"creocoder/yii2-translateable": "~1.0"

to the require section of your composer.json file.

Migrations

Run the following command

bash
$ yii migrate/create create_post_table

Open the /path/to/migrations/m_xxxxxx_xxxxxx_create_post_table.php file, inside the up() method add the following

php
$this->createTable('{{%post}}', [
    'id' => Schema::TYPE_PK,
]);

Run the following command

bash
$ yii migrate/create create_post_translation_table

Open the /path/to/migrations/m_xxxxxx_xxxxxx_create_post_translation_table.php file, inside the up() method add the following

php
$this->createTable('{{%post_translation}}', [
    'post_id' => Schema::TYPE_INTEGER . ' NOT NULL',
    'language' => Schema::TYPE_STRING . '(16) NOT NULL',
    'title' => Schema::TYPE_STRING . ' NOT NULL',
    'body' => Schema::TYPE_TEXT . ' NOT NULL',
]);

$this->addPrimaryKey('', '{{%post_translation}}', ['post_id', 'language']);

Configuring

Configure model as follows

…

Model PostTranslation can be generated using Gii.

Usage

Setting translations to the entity

To set translations to the entity

…

Getting translations from the entity

To get translations from the entity

…

Checking for translations in the entity

To check translations in the entity

php
$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');

Advanced usage

Collecting tabular input

Example of controller actions

…

Example of view form

php
field($model->translate($language), "[$language]title")->textInput();
    echo $form->field($model->translate($language), "[$language]body")->textarea();
}

//...

ActiveForm::end();

Language specific translation attribute labels

Example of model attribute labels

…

Donating

Support this project and others by creocoder via gratipay.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言