simsol/yii2-multilang
Composer 安装命令:
composer require simsol/yii2-multilang
包简介
Extensions for multi languages in objects
README 文档
README
This is a Guide for using MultiLang Module
1. Install
composer require simsol/yii2-multilang
2. Init
Module declaration and config for languages to be stored:
'modules' => [
'multilang' => [
'class' => 'simsol\multilang\Module',
//config for languages, default will be ["en"=>"ENG"]."en" is language code and "ENG" is the label to be displayed
'languages' => [
'en' => 'ENG',
'vi' => 'VN',
],
],
],
3. Run Migrations
./yii migrate --migrationPath=@simsol/multilang/migrations
4. Usage
Please follow below steps to set up and use this extension.
a. In model
-
Add property
translationto your model class.public $translations; -
Add rule for
translationproperty./** * {@inheritdoc} */ public function rules() { return [ ... [['translations'],'safe'], ]; } -
Modify
afterSave()and add this line:Translate::saveTranslation($this->translations,$this->id_model,Model::class);This will save translation for current model.
b. In form
-
Add this widget for attributes that you want to provide translation to your create or update form.
<?=$form->field($model,'attribute')->widget(MultiLangFieldsWidget::class)->label(false)?> -
Available config:
attributeLabel: Optional. In case you want to use another name for model attribute, default is model's attribute name.attributeToSave: Optional. In case you want to save with a different attribute, default is model's attribute. Useful when working with custom model.attributeToLoad: Optional. In case you want to load saved translation value on edit with a different attribute, default is model's attribute. Useful when working with custom model.inputType: Optional. Default is text input, available options:- Text Input: MultiLangFieldsWidget::TYPE_TEXT_INPUT
- Textarea: MultiLangFieldsWidget::TYPE_TEXTAREA
<?=$form->field($model,'attribute')->widget(MultiLangFieldsWidget::class,[ 'inputType' => MultiLangFieldsWidget::TYPE_TEXTAREA, 'attributeLabel' => 'Another name', ])->label(false)?>
c. Query translations
Translate::loadTranslation($model,$attribute = null,$modelId = null;$modelClass = null,$language = null)
-
$model: Required. Model to load translations. -
$attribute: Optional. Translations for specified attribute will be queried if provided. -
$modelId: Optional. In case the model id is different from defined $model, useful when working with custom model. -
$modelClass: Optional. In case model class name is different from provided model. For example:app\models\form\CustomerFormandapp\models\Customer -
$language: Optional. If passed, value will be used to query for specified language. -
Query results: Result might be varied depends on provided parameters. See examples below:
Translate::loadTranslation($model);
Result:
[
'description' => [
'en' => 'Description of data.',
'vi' => 'Mô tả của dữ liệu',
],
'title' => [
'en' => 'Title of data',
'vi' => 'Tiêu đề của dữ liệu',
],
]
Translate::loadTranslation($model,'title');
Result:
[
'en' => 'Title of data',
'vi' => 'Tiêu đề của dữ liệu',
]
Translate::loadTranslation($model,'title',null,null,'en');
Result:
'Title of data'
Translate::loadTranslation($model,null,null,null,'en');
Result:
[
'description' => 'Description of data.',
'title' => 'Title of data',
]
simsol/yii2-multilang 适用场景与选型建议
simsol/yii2-multilang 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 simsol/yii2-multilang 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simsol/yii2-multilang 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2021-08-18