编程

使用 Laravel Gemini Translator 将你的应用翻译成其他语言

25 2025-07-30 06:15:00

Laravel 的 Gemini AI translator 包提取 Laravel 项目中的翻译键,并使用谷歌的 Gemini AI 代理进行翻译。该软件包生成必要的语言文件,以便快速翻译你的应用。

此包通过使用交互式 CLI 命令工作,该命令将扫描你的项目,提示你选择要处理的翻译文件,使用 AI 翻译字符串,然后生成翻译键。

使用它提供的 artisan 命令,你可以指定要将字符串翻译成的语言和其他配置选项,如分块,以确定在每个请求中向 Gemini 发送多少个键:

# Basic usage with custom languages
php artisan translations:extract-and-generate --langs=en,es,fr,de
 
# High-performance processing with fork driver and smaller chunks
php artisan translations:extract-and-generate --driver=fork --chunk-size=50
 
# Exclude additional directories and only scan Blade files
php artisan translations:extract-and-generate --exclude=vendor,tests,docs --extensions=blade.php
 
# Skip existing translations and use a different target directory
php artisan translations:extract-and-generate --skip-existing --target-dir=resources/lang
 
# Get help with all available options
php artisan help translations:extract-and-generate

注:该包使用 Gemini 2.0 Flash-Lite 模型。

主要特性

  • 交互式提示:交互式选择要处理的翻译文件(messages.phpvalidation.php 等)和 JSON 键名前缀。
  • 强大的键提取:使用精确的正则表达式扫描 Blade、PHP、Vue、JS和 TypeScript 文件中的翻译键。
  • 智能排除:自动忽略 route()config() 助手函数以防止误报。
  • AI 翻译:使用 Gemini AI API 同时为多种语言提供高质量的翻译。
  • 并发支持:使用 ·spatie/fork· 进行并行处理,使 API 调用速度显著加快。
  • 优雅停止:随时按键(默认为 “q”)停止翻译过程。
  • 详细日志记录:创建一个 translation_extraction_log.json 文件,详细说明每个密键及其所在位置,并为错误创建 failed_translation_keys.json

了解更多