innoboxrr / locale-generator
Generador de traducciónes en formato JSON
Requires
- php: ^8.3
- guzzlehttp/guzzle: ^7.7
- illuminate/support: ^13.0
Requires (Dev)
- innoboxrr/larapack-generator: ^7.0
- orchestra/testbench: ^11.0
- phpunit/phpunit: ^11.5 || ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Collects the translation keys your Laravel app and its SPA use into <locale>.json, keeps the translations you already have, and can fill the missing ones with Google Translate.
php artisan locale:generate es # add the keys found in the code to es.json php artisan locale:translate es # translate the ones that are still empty
Install
composer require innoboxrr/locale-generator
The service providers are auto-discovered. Publishing the config is optional; its tag is config, and passing --provider keeps other packages that use the same tag from publishing their files too:
php artisan vendor:publish --provider="Innoboxrr\LocaleGenerator\Providers\AppServiceProvider" --tag=config
Configuration
| Key | Default | Meaning |
|---|---|---|
lang_path |
env('LOCALE_GENERATOR_LANG_PATH'), or lang_path() when empty |
Directory of the <locale>.json files. A relative path is resolved against the project root. |
routes |
[resource_path()] |
Directories to scan. node_modules and vendor are always skipped. |
extensions |
php, vue, js, jsx, ts, tsx |
Extensions of the files to read. |
google_api_key |
env('GOOGLE_TRANSLATE_API_KEY') |
Used by locale:translate. |
An empty routes or extensions, as in a config published by an older version, means the default.
If your SPA loads its translations from another directory, point lang_path there:
LOCALE_GENERATOR_LANG_PATH=resources/locales
locale:generate {locale}
Extracts the literal keys of __('…'), trans('…'), t('…') and $t('…') (also this.$t('…')), in single or double quotes, and merges them into <lang_path>/<locale>.json.
- The key has to be a complete string literal as the first argument:
t('Hello ' + name)and template literals are skipped. - The function name can't be glued to something else:
alert('x'),emit('x'),import('x')ori18n.t('x')are not keys. - Entries already in the file are kept as they are, including keys that no longer appear in the code. New keys are appended with an empty value; until they are translated, Laravel's
__()shows the key itself. - If the existing file isn't valid JSON, it is left untouched and the command fails.
locale:translate {filename} {--force}
Translates the empty values of <lang_path>/<filename>.json with the Google Cloud Translation API (v2), one request per key. The target language is the file name: es, or pt_BR, sent as pt-BR. Both es and es.json are accepted.
- Set
GOOGLE_TRANSLATE_API_KEYin.env. Without it the command fails without calling Google. - Existing translations are not overwritten.
--forcetranslates every key again. - If Google fails halfway, the translations obtained so far are saved and the command exits with an error.
Documentación / Documentation
Documentación completa del ecosistema, en español y en inglés / Full ecosystem documentation, in Spanish and English: https://innoboxrr.github.io/docs/paquetes/locale-generator