esign / laravel-helpermodel-translatable
一个laravel包,使你的eloquent模型可翻译。
Requires
- php: ^8.0
- illuminate/database: ^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.0|^10.0
README
此包允许您通过使用单独的模型来存储翻译,例如Post
和PostTranslation
,来使eloquent模型可翻译。
安装
您可以通过composer安装此包
composer require esign/laravel-helpermodel-translatable
该包将自动注册服务提供者。
接下来,您可以发布配置文件
php artisan vendor:publish --provider="Esign\HelperModelTranslatable\HelperModelTranslatableServiceProvider" --tag="config"
配置文件将发布为config/helpermodel-translatable.php
,内容如下
return [ /** * These are the default namespaces where the HelperModelTranslatable * looks for the helper models. You may pass in either a string * or an array, they are tried in order and the first match is used. */ 'model_namespaces' => ['App', 'App\\Models'], ];
用法
准备您的模型
要使模型可翻译,您需要在模型上使用Esign\HelperModelTranslatable\HelperModelTranslatable特质。接下来,您应该通过添加公共$translatable属性来定义哪些字段是可翻译的。
use Esign\HelperModelTranslatable\HelperModelTranslatable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HelperModelTranslatable; public $translatable = ['title']; }
接下来,您可以创建一个辅助模型,就像您通常做的那样
use Illuminate\Database\Eloquent\Model; class PostTranslation extends Model { ... }
检索翻译
要检索当前区域设置的翻译,您可以使用在translatable
属性中定义的属性。或者,您也可以使用getTranslation
方法
$post->title $post->getTranslation('title')
要检索特定区域设置的翻译,您可以使用完全后缀的属性或将区域设置传递给getTranslation
方法
$post->getTranslation('title', 'nl')
要检查翻译属性是否存在,您可以使用hasTranslation
方法
PostTranslation::create(['language' => 'en', 'title' => 'Test en', 'tags' => ['🍎', '🍐', '🍋']]); PostTranslation::create(['language' => 'nl', 'title' => null, 'tags' => []]); PostTranslation::create(['language' => 'fr', 'title' => '']); $post->hasTranslation('title', 'en'); // returns true $post->hasTranslation('title', 'nl'); // returns false $post->hasTranslation('title', 'fr'); // returns false $post->hasTranslation('tags', 'en'); // returns true $post->hasTranslation('tags', 'nl'); // returns false
如果您需要检查实际翻译模型是否存在,您可以使用hasTranslationModel
方法
PostTranslation::create(['language' => 'en']); $post->hasTranslationModel('en'); // returns true $post->hasTranslationModel('nl'); // returns false
要检索实际翻译模型,您可以使用getTranslationModel
方法
$post->getTranslationModel(); $post->getTranslationModel('nl');
如果您不提供区域设置,将使用当前区域设置。
使用回退
此包允许您返回在应用程序的config/app.php
中定义的属性的fallback_locale
的值。
getTranslation
方法的第三个useFallbackLocale
参数可用于控制此行为
PostTranslation::create(['language' => 'en', 'title' => 'Your first translation']); PostTranslation::create(['language' => 'nl', 'title' => null]); $post->getTranslation('title', 'nl', true); // returns 'Your first translation' $post->getTranslation('title', 'nl', false); // returns null
或者,您可以使用专用方法来完成此操作
PostTranslation::create(['language' => 'en', 'title' => 'Your first translation']); PostTranslation::create(['language' => 'nl', 'title' => null]); $post->getTranslationWithFallback('title', 'nl'); // returns 'Your first translation' $post->getTranslationWithoutFallback('title', 'nl'); // returns null
您可以通过覆盖HelperModelTranslatable
特质中的getFallbackLocale
方法来配置回退区域设置。最初请求的区域设置作为参数传递
use Esign\HelperModelTranslatable\HelperModelTranslatable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HelperModelTranslatable; public $translatable = ['title']; public function getFallbackLocale(?string $locale = null): ?string { return 'fr'; } }
自定义关系
按照惯例,此包假定您的辅助模型与您的主模型名称相同,后缀为Translation
,例如Post
和PostTranslation
。此模型用于加载translations
关系,您可以通过定义模型/外键或覆盖整个关系来自定义此关系。
use Esign\HelperModelTranslatable\HelperModelTranslatable; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HelperModelTranslatable; public $translatable = ['title']; protected function getHelperModelClass(): string { return CustomPostTranslation::class; } protected function getHelperModelForeignKey(): string { return 'custom_post_id'; } }
use Esign\HelperModelTranslatable\HelperModelTranslatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; class Post extends Model { use HelperModelTranslatable; public $translatable = ['title']; public function translations(): HasMany { return $this->hasMany(PostTranslation::class); } }
如果您需要自定义默认关系名称,您可以通过覆盖模型上的helperModelRelation
属性来实现
use Esign\HelperModelTranslatable\HelperModelTranslatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; class Post extends Model { use HelperModelTranslatable; protected $helperModelRelation = 'otherTranslations'; public $translatable = ['title']; }
您还可以通过使用useHelperModelRelation
方法动态地使用不同的关系
$post->useHelperModelRelation('secondaryTranslations')->getTranslation('title');
作用域
此包还提供了一些作用域,允许您为翻译关系设置约束
Post::whereTranslation('title', 'Post about dogs'); Post::whereTranslation('title', 'like', '%dogs%'); Post::whereTranslation('title', 'like', '%dogs%', 'nl'); Post::whereTranslation('title', 'like', '%dogs%', ['nl', 'en']); Post::whereTranslation('title', 'like', '%dogs%')->orWhereTranslation('title', 'like', '%cats%'); Post::translatedIn('nl'); Post::translatedIn(['nl', 'en']); Post::translatedIn('nl')->orTranslatedIn('en');
测试
composer test
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。