s-shiryaev/laravel-translatable
Composer 安装命令:
composer require s-shiryaev/laravel-translatable
包简介
Trait for implementing model localization
README 文档
README
This package contains a trait to make it easier to work with translating Eloquent models.
Installation
You can install the package via composer:
composer require s-shiryaev/laravel-translatable
Version Compatibility
| Laravel/Lumen | Package |
|---|---|
| 7.x | 1.x |
| 8.x | 1.x |
| 9.x | 1.x |
| 10.x | 2.x |
Usage
Just add the SShiryaev\LaravelTranslatable\Translatable trait to the model and create a property translatable, which holds an array with all the names of attributes you wish to make translatable:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use SShiryaev\LaravelTranslatable\Translatable; class Currency extends Model { use Translatable; protected $translatable = ['name', 'code']; protected $fillable = [ 'id', 'name_ru', 'name_en', 'code_ru', 'code_en', 'code_de', 'active', 'sort', ]; }
Now, when accessing the properties of the model, the value will be returned in accordance with the application locale:
App::setLocale('ru'); $currency = new Currency(['name_ru' => 'Доллар', 'name_en' => 'Dollar']); echo $currency->name; //Доллар
Also, when converting a model and their eloquent collection to an array (for example, in presenters), field values will be returned according to the application locale:
App::setLocale('ru'); $currency = Currency::find(1); $currency->toArray(); //['name' => 'Доллар', 'name_en' => 'Dollar'] $currencies = Currency::all(); $currencies->toArray(); //[0 => ['name' => 'Доллар', 'name_en' => 'Dollar']]
Sometimes, when converting to an array, you need to get the original fields of the model without translation. This can be done by passing an optional parameter in the toArray() method:
$currency = Currency::find(1); $currency->toArray(false); //['name_ru' => 'Доллар', 'name_en' => 'Dollar'] $currencies = Currency::all(); $currencies->toArray(false); //[0 => ['name_ru' => 'Доллар', 'name_en' => 'Dollar']]
Translation also works with relationships.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
s-shiryaev/laravel-translatable 适用场景与选型建议
s-shiryaev/laravel-translatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「laravel」 「translate」 「eloquent」 「laravel-translatable」 「s-shiryaev」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 s-shiryaev/laravel-translatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 s-shiryaev/laravel-translatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 s-shiryaev/laravel-translatable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Store your language lines in the database, yaml or other sources
An Astrotomic Translatable extension for Laravel Nova.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
腾讯翻译君翻译、百度翻译 、谷歌翻译(Google 翻译)
统计信息
- 总下载量: 69
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-14