fanswoo/laravel-google-translate
Composer 安装命令:
composer require fanswoo/laravel-google-translate
包简介
laravel google translate
README 文档
README
A comprehensive Laravel package that provides Google Translate functionality with support for both API v2 and v3, plus automated language file translation capabilities.
Features
- Google Translate API Integration: Support for both v2 (API key) and v3 (service account) authentication
- Language File Translation: Automatically translate Laravel language files using
translate:langcommand - Facade Support: Easy-to-use Laravel facade for translation operations
- Batch Translation: Translate multiple texts to multiple target languages
- Overwrite Protection: Preserve existing translations while adding new ones
- Nested Array Support: Handle complex Laravel language file structures
Requirements
- Laravel v11
- PHP ^8.4
Installation
You can install the package via composer:
composer require fanswoo/laravel-google-translate
After that run the vendor:publish command:
php artisan vendor:publish --provider=FF\\GoogleTranslate\\GoogleTranslateProvider --tag=config
This will publish config from fanswoo/laravel-google-translate
Configuration
// config/services.php [ 'google_translate_v2' => [ 'api_key' => env('GOOGLE_TRANSLATE_API_KEY'), ], 'google_translate_v3' => [ 'project_id' => env('GOOGLE_TRANSLATE_PROJECT_ID'), 'service_account_credentials_path' => env('GOOGLE_TRANSLATE_SERVICE_ACCOUNT_CREDENTIALS_PATH'), ], ],
// config/google-translate.php return [ // SSL verification for cURL requests 'ssl_verify_peer' => true, // Google Cloud Version, can be 'v2' or 'v3' 'api_version' => 'v2' ];
Usage
Basic Translation
use FF\GoogleTranslate\Facade\Translate; // Single text translation $text = Translate::get('hello world', 'zh_TW'); // Multiple texts to multiple languages $translations = Translate::multiple(['hello', 'world'], ['zh_TW', 'ja_JP']);
Language File Translation Command
Automatically translate all your Laravel language files:
# Translate from English to Traditional Chinese and Japanese php artisan translate:lang en zh_TW ja_JP # Overwrite existing translations php artisan translate:lang en zh_TW --overwrite # Use custom language directory path php artisan translate:lang en fr_FR --path=/custom/lang/path
Command Options:
source: Source locale (e.g., en)targets: One or more target locales (e.g., zh_TW ja_JP fr_FR)--overwrite: Overwrite existing translations (default: false)--path: Custom path to language directory (default: Laravel's lang directory)
Programmatic Language File Translation
use FF\GoogleTranslate\LocaleTranslation\LocaleTranslation; $localeTranslation = new LocaleTranslation(); // Translate from 'en' to multiple target locales $results = $localeTranslation->translate('en', ['zh_TW', 'ja_JP'], false); // With custom path and overwrite enabled $results = $localeTranslation->translate('en', ['fr_FR'], true);
API Version Configuration
Google Translate API v2 (Default)
- Simple API key authentication
- Faster setup
- Suitable for basic translation needs
Google Translate API v3
- Service account authentication
- More secure and feature-rich
- Required for batch operations via
multiple()method
The package automatically uses v3 for the multiple() method regardless of the configured version.
Testing
Run the test suite:
./vendor/bin/phpunit
The package includes comprehensive tests for:
- Basic translation functionality
- Language file scanning and translation
- Command structure validation
- Error handling and edge cases
Example Language File Structure
The package handles Laravel's standard language file structure:
// lang/en/messages.php return [ 'welcome' => 'Welcome to our application', 'goodbye' => 'Goodbye!', 'auth' => [ 'failed' => 'These credentials do not match our records.', 'password' => 'The provided password is incorrect.', ], ];
After running php artisan translate:lang en zh_TW, you'll get:
// lang/zh_TW/messages.php return [ 'welcome' => '歡迎使用我們的應用程式', 'goodbye' => '再見!', 'auth' => [ 'failed' => '這些憑據與我們的記錄不符。', 'password' => '提供的密碼不正確。', ], ];
License
The MIT License (MIT). Please see License File for more information.
fanswoo/laravel-google-translate 适用场景与选型建议
fanswoo/laravel-google-translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 622 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「google translate」 「fanswoo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fanswoo/laravel-google-translate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fanswoo/laravel-google-translate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fanswoo/laravel-google-translate 相关的其它包
同方向 / 同关键字的高下载量 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
Yii2 integration for AirBnB Polyglot.js
统计信息
- 总下载量: 622
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-05-29