定制 artryazanov/artisan-translator 二次开发

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

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

artryazanov/artisan-translator

Composer 安装命令:

composer require artryazanov/artisan-translator

包简介

A Laravel package to extract Blade translation strings and translate them using AI.

README 文档

README

Artisan Translator streamlines the localization workflow in Laravel applications. It automates the extraction of strings from Blade templates, translates them using Google's Gemini AI, and helps keep your language files clean by removing unused keys.

Tests Coverage Pint Latest Version on Packagist Total Downloads PHP Version Laravel Version License

Key Features

  • 🔍 Automatic Extraction: Scans Blade templates (.blade.php) for raw text wrapped in common helpers (e.g. __('Hello')), replaces them with translation keys, and saves the source strings to language files.
  • 🤖 AI Translation: Uses Google Gemini to translate your strings into multiple languages.
  • 🚀 Batch Processing: Translates strings in batches to optimize API usage and reduce costs/time.
  • 🧹 Smart Cleanup: Detects and removes translation keys that are no longer used in your codebase.
  • 🛡️ Safe & Robust: Preserves HTML tags and Laravel placeholders (:name, {count}) during translation. Includes retry mechanisms for API stability.

Requirements

  • PHP: 8.2 or higher
  • Laravel: 11.0+ or 12.0+

Installation

Install the package via Composer:

composer require --dev artryazanov/artisan-translator

The package will automatically register its service provider.

Setup

  1. Get a Gemini API Key: Obtain an API key from Google AI Studio.

  2. Configure Environment: Add the key to your .env file:

    GEMINI_API_KEY=your-api-key-here
    GEMINI_MODEL=gemma-3-27b-it
  3. (Optional) Publish Configuration: Customise default settings by publishing the config file:

    php artisan vendor:publish --provider="Artryazanov\ArtisanTranslator\ArtisanTranslatorServiceProvider" --tag="config"

Usage

1. Extract Strings

Scan your resources/views directory to find static strings, replace them with translation keys in the Blade files, and save the original strings to your source language files (default: en).

php artisan translate:extract

Options:

  • --path=dir/name: Limit scanning to a specific subdirectory within resources/views.
  • --dry-run: Preview changes without modifying any files.
  • --force: Overwrite existing keys in translation files if they overlap.

📝 What strings are extracted?

The command scans for strings wrapped in __('...') or @lang('...'). It intelligently distinguishes between "plain text" that needs extraction and existing translation keys.

String Type Example Action Reason
Plain Text __('Hello World') Extract Contains spaces or punctuation.
Plain Text @lang('Click here') Extract Contains spaces.
Existing Key __('messages.welcome') Ignore Looks like a key (dots, no spaces).
Existing Key @lang('auth.failed') Ignore Looks like a key.
Existing Key __('forms/user.email') Ignore Looks like a key (slashes allowed).

2. Translate with AI

Translate your extracted strings from the source language to one or more target languages using Gemini.

php artisan translate:ai --targets=fr --targets=de

Arguments & Options:

  • source (optional): Specify source language (defaults to source_language in config, usually en).
  • --targets: Required. The target language code(s) (e.g., fr, es, de). Can be repeated.
  • --force: Overwrite existing translations in the target files.

Note: If you have mcamara/laravel-localization installed, the package can automatically detect supported locales if you omit --targets.

3. Cleanup Unused Translations

Keep your language files tidy by removing keys that are no longer referenced in your codebase.

php artisan translations:cleanup --dry-run

Options:

  • --dry-run: List unused keys without deleting them (Recommended first step).
  • --force: Skip the confirmation prompt and delete immediately.

Configuration

Main Config (config/artisan-translator.php)

Option Env Variable Default Description
source_language ARTISAN_TRANSLATOR_SOURCE_LANG en The source language of your application.
lang_root_path ARTISAN_TRANSLATOR_LANG_ROOT blade Subdirectory under resources/lang/{locale} where files are stored.
ai_request_delay_seconds ARTISAN_TRANSLATOR_AI_DELAY 2.0 Minimum delay between AI API requests to avoid rate limits.
gemini.api_key GEMINI_API_KEY - Your Google Gemini API Key.
gemini.model GEMINI_MODEL gemma-3-27b-it The AI model to use.

Cleaner Config (config/translation-cleaner.php)

  • scan_paths: Directories to scan for translation usage (defaults to app_path() and resource_path('views')).
  • file_extensions: File types to scan (defaults to *.php, *.blade.php).
  • translation_functions: Functions to look for (e.g., __, trans, @lang).

Supported AI Models

You can use any string supported by the Gemini API, or one of the built-in Enum values:

  • gemini-3.0-pro
  • gemini-3.0-flash
  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite
  • gemma-3-27b-it (Default)

Testing

Run the test suite to ensure everything is working correctly:

composer test

License

MIT. See LICENSE.

artryazanov/artisan-translator 适用场景与选型建议

artryazanov/artisan-translator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 371 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 08 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 artryazanov/artisan-translator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-13