esign/laravel-underscore-translatable
Composer 安装命令:
composer require esign/laravel-underscore-translatable
包简介
A laravel package to make your eloquent models translatable.
README 文档
README
This package allows you to make eloquent models translatable by using separate columns for each language, e.g. title_nl and title_en. This package is heavily inspired by Spatie's spatie/laravel-translatable.
Installation
You can install the package via composer:
composer require esign/laravel-underscore-translatable
Usage
Preparing your model
To make your model translatable you need to use the Esign\UnderscoreTranslatable\UnderscoreTranslatable trait on the model.
Next up, you should define which fields are translatable by adding a public $translatable property.
use Esign\UnderscoreTranslatable\UnderscoreTranslatable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use UnderscoreTranslatable; public $translatable = ['title']; }
Your database structure should look like the following:
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title_nl')->nullable(); $table->string('title_fr')->nullable(); $table->string('title_en')->nullable(); });
Retrieving translations
To retrieve a translation in the current locale you may use the attribute you have defined in the translatable property. Or you could use the getTranslation method:
$post->title $post->getTranslation('title')
To retrieve a translation in a specific locale you may use the fully suffixed attribute or pass the locale to the getTranslation method:
$post->title_nl $post->getTranslation('title', 'nl')
To retrieve all translations, you may use getTranslations:
$post->getTranslations('title'); // returns ['en' => 'Your first translation', 'nl' => 'Jouw eerste vertaling']
If you omit the key, translations for all translatable attributes will be returned:
$post->getTranslations(); // returns ['title' => ['en' => '...', 'nl' => '...']]
You may also limit the returned locales:
$post->getTranslations('title', ['en']); // returns ['en' => 'Your first translation']
To check if a translation exists, you may use the hasTranslation method:
$post->title_en = 'Your first translation'; $post->title_nl = ''; $post->title_fr = null; $post->hasTranslation('title', 'en'); // returns true $post->hasTranslation('title', 'nl'); // returns false $post->hasTranslation('title', 'fr'); // returns false
To retrieve only the locales that have a translated column present for a specific key, use getTranslatedLocales:
$post->title_en = 'Your first translation'; $post->title_nl = 'Jouw eerste vertaling'; $post->getTranslatedLocales('title'); // returns ['en', 'nl']
In case you do not supply a locale, the current locale will be used.
Using a fallback
This package allows you to return the value of an attribute's fallback_locale defined in the config/app.php of your application.
The third useFallbackLocale parameter of the getTranslation method may be used to control this behaviour:
$post->title_en = 'Your first translation'; $post->title_nl = null; $post->getTranslation('title', 'nl', true); // returns 'Your first translation' $post->getTranslation('title', 'nl', false); // returns null
Or you may use dedicated methods for this:
$post->title_en = 'Your first translation'; $post->title_nl = null; $post->getTranslationWithFallback('title', 'nl'); // returns 'Your first translation' $post->getTranslationWithoutFallback('title', 'nl'); // returns null
Setting translations
To set the translation for the current locale you may use the attribute you have defined in the translatable property. Or you could pass it immediately when creating a model:
$post->title = 'Your first translation'; Post::create([ 'title' => 'Your first translation', ]);
You may also use the setTranslation method:
$post->setTranslation('title', 'en', 'Your first translation'); $post->setTranslation('title', 'nl', 'Jouw eerste vertaling');
You could also set multiple translations at once using the setTranslations method or immediately passing them along when creating a model:
$post->setTranslations('title', [ 'en' => 'Your first translation', 'nl' => 'Jouw eerste vertaling', ]); Post::create([ 'title' => [ 'en' => 'Your first translation', 'nl' => 'Jouw eerste vertaling', ], ]);
This package does not persist translations to the database, so don't forget to save your model:
$post->setTranslation('title', 'en', 'Your first translation'); $post->save();
Defining accessors and mutators
You're able to define accessors just like you're used to in Laravel:
public function getTitleAttribute($value): string { return ucfirst($value); }
The same goes for mutators:
public function setTitleAttribute($value, $locale): void { $this->attributes['title_' . $locale] = strtolower($value); }
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
esign/laravel-underscore-translatable 适用场景与选型建议
esign/laravel-underscore-translatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.81k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「translations」 「translatable」 「eloquent」 「esign」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 esign/laravel-underscore-translatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 esign/laravel-underscore-translatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 esign/laravel-underscore-translatable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
An Astrotomic Translatable extension for Laravel Nova.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Doctrine2 behavior traits - slowhop fork
A Laravel Nova package for translatable fields
A library to extend Object capabilities.
统计信息
- 总下载量: 26.81k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 16
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-14