承接 2amigos/yii2-translateable-behavior 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

2amigos/yii2-translateable-behavior

最新稳定版本:1.1.1

Composer 安装命令:

composer require 2amigos/yii2-translateable-behavior

包简介

ActiveRecord attributes translations for Yii2.

README 文档

README

This behavior has been inspired by the great work of Mikehaertl's Translatable Behavior for Yii 1.*.

It eases the translation of ActiveRecord's attributes as it maps theme from a translation table into the main record. It also automatically loads application language by default.

Sample of use:

<?php // create a record $tour = new Tour; $tour->title = "English title"; // save both the new Tour and a related translation record with the title $tour->save(); // change language $tour->language = 'fr'; $tour->title = "French title"; // save translation only $tour->saveTranslation();

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require "2amigos/yii2-translateable-behavior" 

or add

"2amigos/yii2-translateable-behavior" : "~1.1"

to the require section of your application's composer.json file.

Usage

Preparation

First you need to move all the attributes that require to be translated into a separated table. For example, imagine we wish to keep translations of title and description from our tour entity. Our schema should result on the following:

 +--------------+ +--------------+ +-------------------+ | tour | | tour | | tour_lang | +--------------+ +--------------+ +-------------------+ | id | | id | | id | | title | ---> | created_at | + | tour_id | | description | | updated_at | | language | | created_at | +--------------+ | title | | updated_at | | description | +--------------+ +-------------------+ 

After we have modified our schema, now we need to define a relation in our ActiveRecord object. The following example assumes that we have already created a TourLang model (see the schema above):

/** * @return \yii\db\ActiveQuery */ public function getTranslations() { return $this->hasMany(TourLang::className(), ['tour_id' => 'id']); }

Finally, we need to attach our behavior.

use dosamigos\translateable\TranslateableBehavior; \\ ... public function behaviors() { return [ 'trans' => [ // name it the way you want 'class' => TranslateableBehavior::className(), // in case you named your relation differently, you can setup its relation name attribute // 'relation' => 'translations', // in case you named the language column differently on your translation schema // 'languageField' => 'language', 'translationAttributes' => [ 'title', 'description' ] ], ]; } 

Basic Usage

// create a record $tour = new Tour; $tour->title = "English title"; // save both the new Tour and a related translation record with the title $tour->save(); // change language $tour->language = 'fr'; $tour->title = "French title"; // save fr translation only $tour->saveTranslation();

You may also set multiple translations directly:

$tour = new Tour; $tour->title = [ 'translations' => [ 'en' => "English title", 'de' => "Deutscher Titel", ], ]; // save both the new Tour and a related translation record with the title $tour->save();

Fallback language

In case no translation is available for a specific language the behavior allows to specify a fallback translation to load instead. By default the fallback will use the application source language. It can be configured by setting the fallbackLanguage property of the behavior.

Fallback language can be configured to be a single language or per language:

// use english as fallback for all languages when no translation is available 'fallbackLanguage' => 'en', // alternatively: 'fallbackLanguage' => [ 'de' => 'en', // fall back to English if German translation is missing 'uk' => 'ru', // fall back to Russian if no Ukrainian translation is available ],

Additionally to the configurable fallback a fallback to non-localized language is applied automatically. E.g. if no translation exists for de-AT (German in Austria) the translation will fall back to de. The fallback goes further if de is not found using the fallbackLanguage configuration, so from the example above it will then try en.

When the fallback is defined in array format and no fallback can be found for a language, the first fallback is returned.

You may disable the fallback mechanism by setting fallbackLanguage to false.

If you want to configure fallback languages globally, you can do so by configuring the TranslateableBehavior class in Yii DI container:

Yii::$container->set('dosamigos\translateable\TranslateableBehavior', ['fallbackLanguage' => 'de']);

Deleting translations

By default, when an active record is deleted, translation records are deleted in the afterSave event. However some database scenarios require different configuration, in case foreign keys restrict the deletion of records.

You may configure 'deleteEvent' to be either ActiveRecord::EVENT_BEFORE_DELETE or ActiveRecord::EVENT_AFTER_DELETE to control on which event the deletion of records should be performed. You may set 'deleteEvent' to false to disable deletion and rely on DB foreign key cascade or implement your own method.

When using the Translateablebehavior in an ActiveRecord you should enable transactions() for the delete operation.

2amigOS!
Web development has never been so fun!
www.2amigos.us

2amigos/yii2-translateable-behavior 适用场景与选型建议

2amigos/yii2-translateable-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120.75k 次下载、GitHub Stars 达 66, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「translation」 「extension」 「Behavior」 「yii」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 2amigos/yii2-translateable-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 2amigos/yii2-translateable-behavior 我们能提供哪些服务?
定制开发 / 二次开发

基于 2amigos/yii2-translateable-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 120.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 66
  • 点击次数: 30
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 66
  • Watchers: 23
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-01-04