Appearance
Translate Command
The mm:translate
command is used to translate existing language files for modules or the main application using AI services.
Usage
bash
php artisan mm:translate [options]
Options
Option | Description |
---|---|
--module= | (Optional) The specific module name to process |
--app | (Optional) Run on the main application instead of modules |
--from-language= | (Optional) Source language (default: from config) |
--to-language= | (Optional) Target languages, comma separated (default: from config) |
Description
This command translates existing language strings in your modules or main application to multiple target languages using AI translation services. It processes only keys that haven't been translated yet in the target language files.
When executed, this command performs the following actions:
Select Target: Determines whether to translate a specific module, all modules, or the main application.
Load Translations: Loads existing translation strings from the source language files.
Process Languages: Translates strings to each target language one by one, showing progress for each language.
Verify Completeness: After each language is processed, verifies if all keys from the source language are present in the target language and translates any missing keys.
Save Progress: Saves translations after each chunk is processed to preserve progress in case of interruptions.
Examples
Interactive Module Selection
bash
$ php artisan mm:translate
Which module do you want to translate?
[0] Blog
[1] Shop
[2] All modules
[3] Application (outside modules)
> 0
Translating module: Blog
Initiating translation from en to es
8/8 [============================] 100%
Finish translations for en to es
Initiating translation from en to pt_BR
8/8 [============================] 100%
Finish translations for en to pt_BR
Translation completed successfully!
Translate a Specific Module
bash
$ php artisan mm:translate --module=Blog
Translating module: Blog
Initiating translation from en to es
8/8 [============================] 100%
Finish translations for en to es
Initiating translation from en to pt_BR
8/8 [============================] 100%
Finish translations for en to pt_BR
Translation completed successfully!
Translate the Main Application
bash
$ php artisan mm:translate --app
Translating the main application
Initiating translation from en to es
12/12 [============================] 100%
Finish translations for en to es
Initiating translation from en to pt_BR
12/12 [============================] 100%
Finish translations for en to pt_BR
Translation completed successfully!
Specify Source and Target Languages
bash
$ php artisan mm:translate --module=Blog --from-language=en --to-language=fr,de
Translating module: Blog
Initiating translation from en to fr
8/8 [============================] 100%
Finish translations for en to fr
Initiating translation from en to de
8/8 [============================] 100%
Finish translations for en to de
Translation completed successfully!
Configuration
The translation functionality uses the configuration defined in config('module-manager.translation')
:
php
'translation' => [
'TRANSLATION_API_KEY' => env('TRANSLATION_API_KEY', null),
'TRANSLATION_MODEL_PROVIDER' => env('TRANSLATION_MODEL_PROVIDER', Prism\Prism\Enums\Provider::OpenAI),
'TRANSLATION_MODEL' => env('TRANSLATION_MODEL', 'gpt-4o-mini'),
'from_language' => 'en',
'to_language' => [
'es',
'pt_BR',
],
'chunk_size' => 10,
'retry_attempts' => 3,
'delay_between_chunks' => 1,
],
Configuration Options
Option | Description |
---|---|
TRANSLATION_API_KEY | API key for the translation service |
TRANSLATION_MODEL_PROVIDER | Provider for the translation model (default: OpenAI) |
TRANSLATION_MODEL | The specific model to use (default: gpt-4o-mini) |
from_language | Source language (default: en) |
to_language | Array of target languages |
chunk_size | Number of strings to translate in each batch (default: 10) |
retry_attempts | Number of retry attempts for failed translations (default: 3) |
delay_between_chunks | Delay in seconds between processing chunks (default: 1) |
Notes
- This command only translates strings that haven't been translated yet in the target language files.
- Each language is processed completely before moving to the next one.
- The command shows separate progress bars for each language being translated.
- If missing keys are found after the initial translation, they will be automatically translated.
- The command includes retry logic for handling API errors and timeouts.
- Translation files are saved in JSON format in the module's or application's
lang
directory.