mihkullorg/laravel-translatable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mihkullorg/laravel-translatable

Composer 安装命令:

composer require mihkullorg/laravel-translatable

包简介

Laravel package for multilingual models with Google Cloud Translate

README 文档

README

Latest Stable Version Total Downloads Build status StyleCI License

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 mihkullorg/laravel-translatable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 209
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-11