signifly / laravel-translator
为您的Eloquent模型提供基于数据库的翻译。
v2.0.0
2023-11-21 14:19 UTC
Requires
- php: ^7.2.5|^8.0
- illuminate/console: ^8.0|^9.0|^10.0
- illuminate/database: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0|^7.0|^8.0
- phpunit/phpunit: ^7.0|^8.0|^9.0|^10.0
README
该signifly/laravel-translator
包允许您轻松地将基于数据库的翻译添加到Eloquent模型中。
以下是如何使用它的一个简单示例
// Add the trait to your translatable models use Signifly\Translator\Concerns\Translatable; class Post extends Model { use Translatable; /** @var array */ protected $translatable = [ 'title', 'description', ]; }
为了存储翻译,您可以执行以下操作
$post = Post::find(1); $post->translate('en', [ 'title' => 'Some title', 'description' => 'description', ]); // returns a Illuminate\Support\Collection of translations
您还可以翻译单个属性
$post->translateAttribute('en', 'title', 'Some title'); // returns Signifly\Translator\Contracts\Translation
如果您还想更新模型的属性,可以使用以下方法实现
Post::createAndTranslate('en', [ 'title' => 'Some title', 'description' => 'description', ]); // or when updating $post->updateAndTranslate('en', [ 'title' => 'New title', 'description' => 'New description', ]);
updateAndTranslate
方法将检测是否是默认语言,并相应地进行更新。
文档
要开始,请按照以下安装说明进行。
安装
您可以通过composer安装此包
composer require signifly/laravel-translator
该包将自动注册自己。
您可以使用以下命令发布迁移
php artisan vendor:publish --tag="translator-migrations"
注意:默认迁移假设您使用整数作为模型ID。如果您使用UUID或其他格式,请相应地调整迁移。
php artisan migrate
您可以选择使用以下命令发布配置文件
php artisan vendor:publish --tag="translator-config"
这是发布配置文件的内容
return [ /* * The active language code that is used by the package * to return the correct language for a model. */ 'active_language_code' => null, /* * By default the package will not translate model attributes automatically. * It should be used with caution as it performs extra requests. * Remember to eager load the translations * in order to optimize performance. */ 'auto_translate_attributes' => false, /* * The default language code that is used by the package * to make comparisons against other languages * in order to provide statistics. */ 'default_language_code' => 'en', /* * By default the package will use the `lang` paramater * to set the active language code. */ 'language_parameter' => 'lang', /* * This determines if the translations can be soft deleted. */ 'soft_deletes' => false, /* * This is the name of the table that will be created by the migration and * used by the Translation model shipped with this package. */ 'table_name' => 'translations', /* * This model will be used to store translations. * It should be implements the Signifly\Translator\Contracts\Translation interface * and extend Illuminate\Database\Eloquent\Model. */ 'translation_model' => \Signifly\Translator\Models\Translation::class, ];
高级
该包包含一些中间件,您可能需要应用以获取一些额外功能。
自动翻译
您可以通过在配置中将auto_translate_attributes
设置为true
或将对路由应用Signifly\Translator\Http\Middleware\AutoTranslate
中间件来启用此功能。
为此,您还必须在配置中设置active_language_code
。
激活语言
如果您将对路由应用Signifly\Translator\Http\Middleware\ActivateLanguage
中间件,则可以从请求中推断出此信息。
但是,您也可以通过调用Translator::activateLanguage('en')
来手动完成此操作。
测试
composer test
安全
如果您发现任何安全问题,请通过电子邮件发送至dev@signifly.com,而不是使用问题跟踪器。
鸣谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。