davidpersson/li3_translate
最新稳定版本:v2.1.0
Composer 安装命令:
composer require davidpersson/li3_translate
包简介
A translate behavior for the Lithium PHP framework
README 文档
README
What this behavior does is enable you to have content of different locales/languages to be stored in your database via your lithium based model. You can also search and retrieve locale specific data simply.
At this moment the plugin is compatible with MongoDB and all relational databases supported by lithium.
Installation
Install the plugin via composer (this will also pull in any dependencies):
composer require davidpersson/li3_translate
Usage
In the model you wish to have translatable please add something to the tune of:
// ... class Artists extends \lithium\data\Model { use li3_behaviors\data\model\Behaviors; protected static $_actsAs = [ 'Translatable' => [ 'default' => 'ja', 'locales' => ['en', 'it', 'ja'], 'fields' => ['name'] ] ]; // ...
-
The default option is required, espececially if you are saving multiple languages in one create or save command. A base language of which to gather the content and validate against is needed. This ensures that your validations will still work.
-
The locales that you want to use is fairly self explanatory, it simply tells the plugin which languages you want support for.
-
So as not to double up on too much data. The fields array tells the behavior which fields will need localizations. Those that are not included here will be simple fields which will not be attached a locale.
Good example usage of the plugin can be seen in the unit tests, but here is a brief description.
Saving Data
When saving data with the default locale, you basically don't have to change anything. When saving translated data along with the original data use one of the following syntax (all are equivalent):
$user = Users::create([ 'profile' => 'Dreaded Rasta', 'name' => 'Richard', 'i18n.name.it' => 'Ricardo' ]); $user = Users::create([ 'name' => 'Richard', 'profile' => 'Dreaded Rasta', 'i18n' => [ 'name' => [ 'it' => 'Ricardo' ] ] ]); $user = Users::create([ 'profile' => 'Dreaded Rasta', 'name' => 'Richard' ]); $user->translate('name', 'it', 'Ricardo');
When saving just translated data i.e. when updating an already existing record use the following syntax. Please note that in this case original data (for the default locale must already be present).
$user = Users::find('first', ['conditions' => ['name' => 'Richard']]); $user->save([ 'i18n.name.it' => 'Ricardo' ]); // ... or ... $user->translate('name', 'it', 'Ricardo'); $user->save();
Retrieving translated Entities
$user = Users::find('first', [ 'conditions' => ['i18n.name.it' => 'Ricardo'] ]); $user = Users::find('all', [ 'order' => ['i18n.name.it' => 'ASC'] ]);
If you don't want to use the translate() method to translate single fields, but
want the record translated into a single locale use the following syntax. You can
then retrieve field data as normal.
$user = Users::find('first', [ 'conditions' => ['id' => 23], 'translate' => 'it' ]); $user->name; // returns 'Ricardo'.
This is good for display purposes. For saving data use the syntax described above.
If you do not know the translation you are searching for, the translation can be searched by the following:
$users = Users::all(['conditions' => ['i18n.name' => 'Ricardo']]);
On-the-fly Disabling of Translations
You can disable the automatic retrieval of translations for a record:
$user = Users::find('first', [ 'conditions' => ['name' => 'Richard'], 'translate' => false ]);
And disable running the behavior on save:
$user->save(null, ['translate' => false]);
Accessing Translations
$user = Users::find('first', ['conditions' => ['name' => 'Richard']]); $user->translate('name', 'it'); // returns 'Ricardo'; $user->translate('name'); // returns ['en' => 'Richard', 'it' => 'Ricardo']; $user->name; // returns 'Richard', as the default locale is `en`.
Validation
When translations are present in the to-be-saved data, all are validated against the base rule.
$user = Users::create([ 'profile' => 'Dreaded Rasta', 'name' => 'Richard' ]); $user->validate(['translate' => false]);
Data Model
Translation data is stored inline with the entity. For MongoDB a subdocument will used, for relational databases special field names are used.
-
<user>name => Richardprofile<i18n>nameit => Ricardo
-
<user>name => Richardprofilei18n_name_it => Ricardo
Gotchas
You should not change the locale when the model already has saved data. Otherwise manual migration will be required.
I have yet tested this plugin for white lists and other features. If you find a case that doesn't work then please log an issue.
davidpersson/li3_translate 适用场景与选型建议
davidpersson/li3_translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.78k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「Behavior」 「lithium」 「li3」 「g11n」 「translate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 davidpersson/li3_translate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 davidpersson/li3_translate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 davidpersson/li3_translate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A CakePHP plugin to provide easy access to zumba/swivel
This is a set of Yii2 behaviors extensions
A Usenet Indexer
A Lithium plugin adding support for querying Unicode's CLDR.
A Lithium plugin with additional g11n resources constituting the Lithium Locale Data Repository.
Model behaviors for the li₃ PHP framework
统计信息
- 总下载量: 1.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 27
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-02-23