rpwebdevelopment / laravel-ugc-translate
用户生成内容翻译包
Requires
- php: ^8.1
- deeplcom/deepl-php: ^1.1
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpunit/phpunit: ^9.5
README
这是一个针对 Laravel 的特定包,旨在以数据库驱动的方式自动翻译用户生成的内容。
安装
您可以通过 composer 安装此包
composer require rpwebdevelopment/laravel-ugc-translate php artisan vendor:publish --tag="ugc-translate-migrations" php artisan migrate php artisan vendor:publish --tag="ugc-translate-config"
配置
此包实现了 DeepL API 的机器翻译,因此您需要在 .env 文件中提供适当的 API 密钥
DEEPL_AUTH_TOKEN=YOUR_VALID_DEEPL_API_KEY
如果您不希望每次记录更新/创建都触发自动翻译,您可以在 .env 文件中添加以下内容以禁用观察者
DEEPL_AUTO_DISABLED=true
有时您可能只想在有特定条件满足的情况下在模型上启用翻译,例如如果模型实例设置了标志,为了实现这一点,您可以向模型添加 getHasTranslationsAttribute 方法,返回一个布尔值,例如:
public function getHasTranslationsAttribute(): bool { // your code here - returning bool }
您可以通过更新配置文件 ugc-translate.php 来设置翻译所需的任何语言
'translation-languages' => [ 'en-GB', 'fr', 'it', 'de', ],
使用方法
该包提供了一种新特质,可用于应用自动翻译
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use RpWebDevelopment\LaravelUgcTranslate\Traits\HasTranslatable; class Posts extends Model { use HasTranslatable; protected $guarded = []; }
为了定义可翻译字段和所需的翻译语言,我们的模型现在需要一个新属性 $ugcTranslatable 来定义要翻译的数据库字段
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use RpWebDevelopment\LaravelUgcTranslate\Traits\HasTranslatable; class Posts extends Model { use HasTranslatable; public array $ugcTranslatable = [ 'title', 'body', ]; protected $guarded = []; }
除非您已禁用自动翻译,否则在模型中创建或更新记录将导致生成和存储翻译。该包利用 Laravel 的 locale 来断言要返回的语言。如果我们以以下记录为例,我们的帖子模型
| id | title | body |
| --- | --------------- | ----------------------- |
| 1 | This is a title | This is the main text |
您可以实现以下内容
$post = App\Models\Post::find(1); app()->setLocale('en_GB'); echo $post->title; // outputs "This is a title" app()->setLocale('it'); echo $post->title; // outputs "Questo è un titolo" app()->setLocale('fr'); echo $post->title; // outputs "Il s'agit d'un titre"
有时您可能希望在不切换应用程序区域设置的情况下输出特定的语言值,这可以通过以下方法实现
$post = App\Models\Post::find(1); app()->setLocale('en_GB'); echo $post->title; // outputs "This is a title" echo $post->localeField('title', 'it'); // outputs "Questo è un titolo" echo $post->localeField('title', 'fr'); // outputs "Il s'agit d'un titre"
有时您可能不需要每个模型的所有默认语言,为了覆盖模型上的默认语言,您只需向模型添加 getUgcLanguagesAttribute 方法,返回一个区域代码数组
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use RpWebDevelopment\LaravelUgcTranslate\Traits\HasTranslatable; class Posts extends Model { use HasTranslatable; protected $guarded = []; public function getUgcLanguagesAttribute(): array { return ['en_GB', 'it']; } }
如果您在应用程序中使用 Livewire,则有一个自定义模态可用于编辑和锁定特定的翻译,您可以通过运行以下命令发布模态组件和视图
php artisan vendor:publish --tag="ugc-translate-livewire"
发布后,您可以使用以下标签将模态添加到项目中
<livewire:ugc.modal :model="$model" field="title" />
这将向您的 blade 模板添加一个按钮,该按钮将打开以下模态
锁定翻译将防止它们自动更新。
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG
致谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件
