aw-studio/laravel-deeplable
Composer 安装命令:
composer require aw-studio/laravel-deeplable
包简介
README 文档
README
A package for translating Laravel Models using DeepL's API. It contains a translator for the astrotomic/laravel-translatable composer package. Translators for other usecases can be written easily.
Installation
Install the package via composer:
composer require aw-studio/laravel-deeplable
and publish the config file:
php artisan vendor:publish --tag=deeplable --force
Setup
After publishing the config/deeplable.php you must enter your DeepL-Secret in order to use their API.
You may also configure all Models you want to translate when you run the deeplable:run command.
Usage
You can translate your translatable models to all languages that are set in your config/translatable.php as long as they are available on DeepL. In order to translate your models to a target language the default translation (fallback_locale) must be available. You can use the Deeplable trait to easily translate your model to a target language.
use AwStudio\Deeplable\Traits\Deeplable; class Post extends Model { use Deeplable; }
Imagine you have stored the default language ('en') and want to auto-generate the german translation for all translated attributes:
$post = Post::first(); $post->translateTo('de'); $post->translateAttributeTo('de', 'title'); $post->translateAttributesTo('de', ['title', 'text']);
If you want to translate all Models to all locales, simply run the deeplable artisan command:
php artisan deeplable:run
You may also set an argument if you want to translate a specific language:
php artisan deeplable:run fr
Translators
Translators have the purpose to bind a translatable attribute to a model. A build in example is the AstrotomicTranslator that updates models that are translated by the astrotomic/laravel-translatable package.
Creating A Translator
A translator must extend the AwStudio\Deeplable\Translators\BaseTranslator class which has 2 abstract methods:
class AstrotomicTranslator { protected function translateAttribute(Model $model, $attribute, $locale, $translation) { $model->translateOrNew($locale)->setAttribute($attribute, $translation); } public function getTranslatedAttributes(Model $model, $locale) { return array_keys($model->getTranslationsArray()[$locale] ?? []); } }
Registering The Translator
A translator may be registered in a Service Provider like this:
use AwStudio\Deeplable\Translators\Resolver; use Astrotomic\Translatable\Contracts\Translatable; public function register() { Translator::register(Translatable::class, function () { return new AstrotomicTranslator($this->app['deeplable.api']); }); }
Using The Translator
The translator can then be used like this:
use AwStudio\Deeplable\Facades\Translator; use Astrotomic\Translatable\Contracts\Translatable; Translator::get(Translatable::class)->translate($post, 'de', 'en'); // Translates the whole model from en to de Translator::get(Translatable::class)->translateAttributes($post, ['title', 'text'], 'de', 'en'); // Translates attributes title and text from en to de
Translation Strategy
Given the case that different models need different translators, a translation strategy can be specified in a service provider like this:
use AwStudio\Deeplable\Facades\Translator; use AwStudio\Deeplable\Translators\Resolver; use Astrotomic\Translatable\Contracts\Translatable; public function register() { Translator::strategy(function(Model $model) { if($model instanceof Translatable) { return Translatable::class; } // something else... }); }
The correct translator can now be received like this:
Translator::for($post)->translate(...);
aw-studio/laravel-deeplable 适用场景与选型建议
aw-studio/laravel-deeplable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.59k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 aw-studio/laravel-deeplable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aw-studio/laravel-deeplable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-04