定制 rpwebdevelopment/laravel-ugc-translate 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

rpwebdevelopment/laravel-ugc-translate

Composer 安装命令:

composer require rpwebdevelopment/laravel-ugc-translate

包简介

User generated content translation package

README 文档

README

Latest Version on Packagist Packagist Downloads License: MIT

This is a Laravel specific package designed to automate the translation of user generated content in a database driven fashion.

Installation

You can install the package via 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"

Configuration

This package offers you the option of using the free-to-use, zero configuration Google Translate package from Stichoza, or DeepL API, or Amazon Translate.

If leveraging the DeepL API you will need to add a valid DEEPL_AUTH_TOKEN to your .env.

If leveraging the Amazon Translate API you will need to ensure you have valid AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION values set in your .env.

If you do not wish every record update/creation to trigger automatic translation, you can disable the observer by adding the following to your .env file:

UGC_AUTO_TRANSLATE_DISABLED=true

Sometimes you may only want translations enabled on a model if certain conditions are met, for example if a flag is set against the model instance, in order to achieve this you can add the method getHasTranslationsAttribute to the model returning a boolean value, i.e.

    public function getHasTranslationsAttribute(): bool
    {
        // your code here - returning bool
    }

You can update the config file ugc-translate.php in order to set any languages needed for translation:

    'translation-languages' => [
        'en-GB',
        'fr',
        'it',
        'de',
    ],

Usage

The package provides a new trait that can be used in order to apply automatic translations:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use RpWebDevelopment\LaravelUgcTranslate\Traits\HasTranslatable;

class Posts extends Model
{
    use HasTranslatable;
    
    protected $guarded = [];
}

In order to define translatable fields, and languages required for translation our models will now require a new property $ugcTranslatable defining the DB fields to be translated:

<?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 = [];
}

Unless you have disabled auto-translate, creating or updating a record within your model will cause translations to be generated and stored. The package leverages Laravel's locale in order to assert which language is to be returned. If we take our posts model above with the following record:

| id  | title           | body                    |
| --- | --------------- | ----------------------- |
| 1   | This is a title | This is the main text   |

You can achieve the following:

$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"

Sometimes you may wish to output a specific language value without switching the application locale, this can be achieved using the following method:

$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"

Sometimes you may not need all the default languages for every model, in order to override the default languages on a model you can simply add the getUgcLanguagesAttribute method to the model returning an array of locale codes:

<?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'];
    }
}

If you use Livewire in your application there is a custom modal available for editing & locking specific translations, you can publish the modal component and view by runningg the following command:

php artisan vendor:publish --tag="ugc-translate-livewire"

Once published you can add the modal into your project with the following tag:

<livewire:ugc.modal :model="$model" field="title" />

This will add a button to your blade template that will open the following modal:

step 1

Locking translations will prevent them from being automatically updated.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

rpwebdevelopment/laravel-ugc-translate 适用场景与选型建议

rpwebdevelopment/laravel-ugc-translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 05 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「rpwebdevelopment」 「laravel-ugc-translate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rpwebdevelopment/laravel-ugc-translate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rpwebdevelopment/laravel-ugc-translate 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-15