mihkullorg/laravel-translatable
Composer 安装命令:
composer require mihkullorg/laravel-translatable
包简介
Laravel package for multilingual models with Google Cloud Translate
README 文档
README
This is a Laravel package for (auto)translatable models. It provides functionality of multilingual models with an option of using external services for automatic translations. Google Cloud Translate is already integrated into the package and adding another service is made pretty easy.
If you're not interested in using any automatic translation services, and just looking to make your models multilingual I suggest using dismav/laravel-translatable package as it has a lot more history and community behind it is way way larger.
Index
Info
The package is create for Laravel 5.6 and 5.7.
Quick start
Step 1: Install package
Add the package in your composer.json by executing the command.
composer require mihkullorg/laravel-translatable
Next, add the service provider to config/app.php
Mihkullorg\Translatable\TranslatableServiceProvider::class
Step 2: Set up the environment and migration.
Publish the migration file.
php artisan vendor:publish --tag=translatable
Set up your Google Cloud Translate/GCT service account and select your preferred way of
authentication from Googe Cloud authenticiation docs. As Google supports authenticating
through environment variables and it being really comfortable, this package is using it too. I prefer authenticating by setting
GOOGLE_APPLICATION_CREDENTIALS variable to the path of the provided service account private key file.
Step 3: Add Translatable trait to model
use Mihkullorg\Translatable\Traits\Translatable; class Post extends Model { use Translatable; public $fillable = ['body']; }
The trait adds necessary translation relationship methods
$post->translations(); $post->translation($field, $language, $createIfNotFresh = false);
Translation is "fresh" when the updated_at of the Translation model is older than the update_at of the Translatable model.
$post->freshTranslation($field, $language);
To create a translation manually, you can call method translate($field, $language, $value = null)
$post->translate('body', 'en', 'New translated body');
To use the GCT service, do not specify the value and the translation is retrieved and model created automatically.
$translation = $post->translate('body', 'en');
translation method acts similarly and if $createIfNotFresh is set true, it tries to retrieve a fresh translation from the database
and if none exists, uses the GCT service.
FAQ
- What about Laravel <5.6?
I'm honestly not sure whether it would work with older version than 5.6 but as of creating this package, only 5.6 and 5.7 are officially supported. If anyone is willing to test it on earlier versions and make sure it works, create an issue and let's discuss it.
Contribution
Feel free to create issues and make PRs to make this package better. I hope you do. :)
mihkullorg/laravel-translatable 适用场景与选型建议
mihkullorg/laravel-translatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 209 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「translate」 「google-translate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mihkullorg/laravel-translatable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mihkullorg/laravel-translatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mihkullorg/laravel-translatable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Store your language lines in the database, yaml or other sources
An Astrotomic Translatable extension for Laravel Nova.
腾讯翻译君翻译、百度翻译 、谷歌翻译(Google 翻译)
DeepL integration for Craft CMS
Translations Manager extension to use Google translation crawling to auto translate your __(), trans() fn
统计信息
- 总下载量: 209
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-11