fevrok / laravel-translatable
该包允许您翻译模型字段。
Requires
- php: ^7.1.3|^8.0
- illuminate/support: ^5.6|^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^3.6|^4.0|^5.0|^6.0
- phpunit/phpunit: ^7.5|^8.0|^9.0
README
这是一个 Laravel 模型列翻译管理器
它是如何工作的?
安装
您可以通过 composer 安装此包
composer require fevrok/laravel-translatable
如果您有 Laravel 5.5 及以上版本,该包将自动注册自身。
否则,您需要将服务提供者添加到 config/app.php
Fevrok\Translatable\TranslatableServiceProvider::class,
发布配置文件和迁移。
php artisan vendor:publish --provider="Fevrok\Translatable\TranslatableServiceProvider"
然后迁移翻译表
php artisan migrate
设置
安装完成后,您可以打开 config/translatable.php
return [ /** * Set whether or not the translations is enbaled. */ 'enabled' => true, /** * Select default language */ 'locale' => 'en', ];
并根据需要进行更新。
使模型可翻译
使模型可翻译的必要步骤是
- 使用
Fevrok\Translatable\Translatable
特性。 - 在
$translatable
属性中定义模型的可翻译字段。
以下是一个准备好的模型示例
use Fevrok\Translatable\Translatable; use Illuminate\Database\Eloquent\Model; class Item extends Model { use Translatable; /** * The attributes that are Translatable. * * @var array */ protected $translatable = [ 'name', 'description', ]; }
自定义翻译模型
要开始使用,请重新发布资产,这将创建新的迁移并更新表名到您想要的名称。
CustomTranslation.php
class CustomTranslation extends \Fevrok\Translatable\Models\Translation { protected $table = 'custom_translations'; }
添加 $translations_model
属性并将其赋予您想要定制的模型。
use Illuminate\Database\Eloquent\Model; use Fevrok\Translatable\Translatable; class Item extends Model { use Translatable; /** * The attributes that are Translatable. * * @var array */ protected $translatable = [ 'name' ]; /** * The model used to get translatios. * * @var string */ protected $translations_model = CustomTranslation::class; }
用法
预加载翻译
// Loads all translations $posts = Post::with('translations')->get(); // Loads all translations $posts = Post::all(); $posts->load('translations'); // Loads all translations $posts = Post::withTranslations()->get(); // Loads specific locales translations $posts = Post::withTranslations(['en', 'da'])->get(); // Loads specific locale translations $posts = Post::withTranslations('da')->get(); // Loads current locale translations $posts = Post::withTranslations('da')->get();
获取默认语言值
echo $post->title;
获取翻译值
echo $post->getTranslatedAttribute('title', 'locale', 'fallbackLocale');
如果您没有定义地区,将使用当前应用程序的地区。您可以传递一个字符串作为自己的地区。如果您没有定义 fallbackLocale,将使用当前应用程序的回退地区。您可以传递一个字符串作为自己的地区。如果您想关闭回退地区,请传递 false。如果找不到特定属性(地区或回退)的模型值,则将设置该属性为 null。
翻译整个模型
$post = $post->translate('locale', 'fallbackLocale'); echo $post->title; echo $post->body; // You can also run the `translate` method on the Eloquent collection // to translate all models in the collection. $posts = $posts->translate('locale', 'fallbackLocale'); echo $posts[0]->title;
如果您没有定义地区,将使用当前应用程序的地区。您可以传递一个字符串作为自己的地区。如果您没有定义 fallbackLocale,将使用当前应用程序的回退地区。您可以传递一个字符串作为自己的地区。如果您想关闭回退地区,请传递 false。如果找不到特定属性(地区或回退)的模型值,则将设置该属性为 null。
检查模型是否可翻译
// with string if (Translatable::translatable(Post::class)) { // it's translatable } // with object of Model or Collection if (Translatable::translatable($post)) { // it's translatable }
设置属性翻译
$post = $post->translate('da'); $post->title = 'foobar'; $post->save();
这将更新或创建标题的翻译,地区为 da。请注意,如果修改的属性不可翻译,则它将直接修改模型本身。这意味着它将覆盖默认语言设置的属性。
查询可翻译模型
要搜索翻译值,您可以使用 whereTranslation
方法。例如,要搜索帖子的 slug,您将使用
$page = Page::whereTranslation('slug', 'my-translated-slug'); // Is the same as $page = Page::whereTranslation('slug', '=', 'my-translated-slug'); // Search only locale en, de and the default locale $page = Page::whereTranslation('slug', '=', 'my-translated-slug', ['en', 'de']); // Search only locale en and de $page = Page::whereTranslation('slug', '=', 'my-translated-slug', ['en', 'de'], false);
whereTranslation
接受以下参数
field
您想要搜索的字段operator
操作符。默认为=
。也可以是值(与 where 相同)value
您想要搜索的值locales
您想要搜索的地区,以数组形式。如果要搜索所有地区,请留为null
default
也在默认值/地区中搜索。默认为 true。