tabaoman/laravel-translation
Composer 安装命令:
composer require tabaoman/laravel-translation
包简介
An i18n solution with Laravel
README 文档
README
A i18n solution with Laravel
This is a Laravel package for i18n. You just need to create just ONE more table to manage ALL multi-language texts for your other Eloquent entities.
Installation
composer require tabaoman/laravel-translation
Basic Use
Change the new config file 'translation.php'
return [ /* * The table that restores the multi-language texts * NOTES: Avoid to have any timestamp field. * If you need to record the created/updated time, use 'model' as following. */ 'table' => 't_language_translate', /* * An example helper model. * It has a higher priority than 'table' if it is not commented out. */ //'model' => \Tabaoman\Translation\Models\LanguageTranslate::class ];
Create the table
NOTE: Do NOT create with timestamp fields ('create_time', 'update_time' here) if using 'table' in translation config.
CREATE TABLE `t_language_translate` ( `id` BIGINT(18) NOT NULL AUTO_INCREMENT, `entity_id` VARCHAR(32) NOT NULL COMMENT 'model id', `lang_code` VARCHAR(18) NOT NULL COMMENT 'language code', `text_code` VARCHAR(18) NOT NULL COMMENT 'text code', `content` LONGTEXT NOT NULL COMMENT 'text', `create_time` DATETIME NOT NULL COMMENT 'created time', `update_time` DATETIME NULL DEFAULT NULL COMMENT 'updated time', PRIMARY KEY (`id`), UNIQUE KEY unique_trans (`entity_id`, `lang_code`, `text_code`) ) COMMENT='i18n table' COLLATE='utf8mb4_general_ci' ENGINE=InnoDB;
Add trait and a new member variable '$translations' in model class
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Tabaoman\Translation\Translation; class Store extends Model { use Translation; protected $casts = ['id' => 'string']; // Sometimes you need this cast protected $translations = [ 'name' => 'STORE_NAME' // attribute => text code // 'name' => ['code' => 'STORE_NAME'], // Or associate like this // 'name' => ['code' => 'STORE_NAME', 'my_key' => 'my_value'] // You can add custom key-value pair for other uses. ]; // ... }
Getting translated attributes
$store = Store::first(); echo $store->name; // Get with default language setting (App::getLocale()) echo $store->getTranslation('name', 'en'); // Get its English name App::setLocale('en'); echo $store->name; // Or get its English name after explicitly set app locale
Saving translated attributes
$store = Store::first(); echo $store->name; // 苹果商店 $store->setTranslation('name', 'Apple store', 'en'); // Directly save the English name App::setLocale('en'); $store->name = 'Apple store'; // Or set its English name after explicitly set app locale $store->save(); // Optional if there is no mutator for 'name' $store = Store::first(); echo $store->name; // Apple store App::setLocale('zh_CN'); echo $store->name; // 苹果商店
Flatten the i18n atrributes
In model class
protected $translations = [ 'name' => 'STORE_NAME' // attribute => text code 'name_english' => ['code' => 'STORE_NAME', 'locale' => 'en'], // You are free to define your own language code. ];
$store = Store::first(); echo $store->name; // 苹果商店 echo $store->name_english; // Apple store
Versions
| Package | Laravel | PHP |
|---|---|---|
| v0.* | 5.8.* / 6.* |
>=7.2 |
tabaoman/laravel-translation 适用场景与选型建议
tabaoman/laravel-translation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「i18n」 「translation」 「multilanguage」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tabaoman/laravel-translation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tabaoman/laravel-translation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tabaoman/laravel-translation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Easy to use i18n translation PHP class for multi-language websites
Store your language lines in the database, yaml or other sources
A custom URL rule class for Yii 2 which allows to create translated URL rules
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 24
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-04