coding-partners/transla-genius
Composer 安装命令:
composer require coding-partners/transla-genius
包简介
A Laravel package for automatic translations.
关键字:
README 文档
README
The TranslaGenius package is a Laravel package designed to automatically translate fields in your Eloquent models using an external translation API. It supports multiple languages and provides flexible translation management.
Features
- Multi-language support (configurable)
- Automatic field translation
- Queue-based translation processing
- Flexible API integration
- Comprehensive scope filters
Installation
To install the package, you can use Composer:
composer require coding-partners/transla-genius
Configuration
After installing the package, you need to publish the configuration file:
php artisan vendor:publish --provider="CodingPartners\TranslaGenius\TranslaGeniusServiceProvider" --tag="translagenius-config"
This will create a translaGenius.php file in your config directory. You can modify this file to set your Supported Languages, and other translation settings:
return [ /* |-------------------------------------------------------------------------- | Supported Languages |-------------------------------------------------------------------------- | List of supported language codes (ISO 639-1). | The first language will be considered as default. */ 'supported_languages' => ['en', 'ar', 'es', 'fr'], // ... other settings ];
Environment Variables
Make sure to set the following environment variables in your .env file:
TRANSLATION_API_KEY=your_openai_api_key TRANSLATION_API_URL=https://openrouter.ai/api/v1/chat/completions TRANSLATION_MODEL=openai/gpt-4o TRANSLATION_TEMPERATURE=0.3 TRANSLATION_MAX_TOKENS=200
Usage
Step 1: Include the Translatable Trait in Your Model
To enable automatic translation for your model, include the Translatable trait in your Eloquent model:
use CodingPartners\TranslaGenius\Traits\Translatable; class Post extends Model { use Translatable; // Define the fields that should be translated public $translatable = [ 'title', 'content', ]; }
Step 2: Update Your Migration
Ensure that the fields you want to translate are of type json in your migration:
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->json('title'); $table->json('content'); $table->timestamps(); });
Step 3: Middleware Setup
To automatically set the locale based on the request, you can include the SetLocale middleware in your bootstrap/app.php:
use CodingPartners\TranslaGenius\Middleware\SetLocale; ->withMiddleware(function (Middleware $middleware) { $middleware->api(prepend: [ SetLocale::class, ]); })
Step 4: Making Requests
When making requests to your application, you can set the Accept-Language header to specify the desired language:
curl -X GET http://yourapp.com/api/posts -H "Accept-Language: en"
This will set the locale to English (en), which means the default language of the system is now English. When you add a new record, you should send the fields in English, and the package will automatically translate them into Arabic.
Step 5: Running the Queue
The translation process is handled by a job that is dispatched when a model is created or updated. To ensure that the translations are processed, you need to run the queue worker:
php artisan queue:work
This will process the TranslateFields job and perform the translations in the background.
Example of Using the fullyTranslated Scope
The Translatable trait provides a fullyTranslated scope that allows you to filter models that are fully translated in the Supported Languages.
$posts = Post::fullyTranslated()->get();
This scope checks if all translatable fields have a translation in the Supported Languages. It ensures that only records with complete translations are returned, which is useful for ensuring data consistency and completeness.
How It Works
- Model Events: When a model is created or updated, the
Translatabletrait automatically dispatches aTranslateFieldsjob. - Translation Job: The
TranslateFieldsjob uses theAutoTranslationServiceto translate the specified fields into the target language. - Translation Service: The
AutoTranslationServicecommunicates with the external API to perform the translation and updates the model with the translated content.
Example Workflow
Here’s a complete example of how to use the package:
Model
use CodingPartners\TranslaGenius\Traits\Translatable; class Product extends Model { use Translatable; protected $fillable = [ 'price', 'name', 'description' ]; public $translatable = [ 'name', 'description' ]; }
Migration
Schema::create('products', function (Blueprint $table) { $table->id(); $table->decimal('price'); $table->json('name'); $table->json('description'); $table->timestamps(); });
Controller
public function store(Request $request) { $product = Product::create([ 'price' => $request->price, 'name' => $request->name, 'description' => $request->description ]); return response()->json([ "data" => $product, "message" => "Done!!" ], 201); }
Request
curl -X POST http://yourapp.com/api/posts -H "Accept-Language: en" -d '{"name": "name in English", "description": "description in English", "price": 100}'
After the request, the name and description fields will be automatically translated into Supported Languages.
Troubleshooting
Common Issues
Missing translations:
- Verify queue worker is running
- Check API credentials
- Confirm language codes match config
Performance:
- Add indexes to JSON columns
- Consider caching frequent translations
Conclusion
The TranslaGenius package simplifies the process of adding multi-language support to your Laravel application. By following the steps outlined above, you can easily configure and use the package to automatically translate your model fields, ensuring that your application is accessible to a global audience.
coding-partners/transla-genius 适用场景与选型建议
coding-partners/transla-genius 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「i18n」 「translation」 「middleware」 「laravel」 「localization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 coding-partners/transla-genius 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 coding-partners/transla-genius 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 coding-partners/transla-genius 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
PHP AMQP Binding Library
A Laravel Eloquent model trait for translatable resource
A Laravel package to monitor queue jobs.
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-26