eii/locale-generator

A Laravel CLI to extract and translate Blade file strings to JSON language files

Maintainers

Package info

github.com/amit-eiitech/locale-generator

pkg:composer/eii/locale-generator

Statistics

Installs: 153

Dependents: 0

Suggesters: 0

Stars: 3

Open Issues: 0

1.3.0 2026-05-03 05:40 UTC

This package is auto-updated.

Last update: 2026-05-07 00:26:52 UTC


README

Laravel Installer

Locale Generator is a Laravel Artisan command that simplifies multilingual development by automatically extracting translation strings (__(), trans(), @lang(), trans_choice()) from Blade templates and generating JSON language files.
It optionally integrates with Google Cloud Translate or DeepL to provide instant translations, helping you speed up localization workflows.

Changelog

Please see CHANGELOG.md for more information on what has changed recently.

Installation

composer require eii/locale-generator

Setup

  1. Publish Configuration:

    php artisan vendor:publish --tag=config
  2. Configure Translation Providers:

    • Google Cloud Translate:
      • Option A (V2 Basic - Recommended):
        • Obtain a simple Google Translate API Key. No extra SDK installation is required!
        • Add to .env:
          GOOGLE_TRANSLATE_API_KEY=your-api-key
      • Option B (V3 Advanced):
        • Install the SDK: composer require google/cloud-translate
        • Create a Google Cloud service account and download the JSON key file.
        • Place the key file in storage/app/google-credentials.json or update the path in config/locale-generator.php.
        • Note: Ensure you do NOT commit this file to version control. Add google-credentials.json to your project's .gitignore file.
        • Add to .env:
          GOOGLE_TRANSLATE_KEY_FILE=/path/to/google-credentials.json
          GOOGLE_PROJECT_ID=your-project-id
    • DeepL:
      • Obtain a DeepL API key (free tier available).
      • Add to .env:
        DEEPL_API_KEY=your-deepl-api-key

Usage

Run the lang:extract command:

php artisan lang:extract welcome --locales=ja,es,de --translate=google
php artisan lang:extract mail.notification --locales=fr,es --translate=deepl
  • file: Blade file name (e.g., welcome, mail.notification).
  • --locales: Comma-separated list of target locales (e.g., ja,es,de).
  • --translate: Translation provider (google or deepl).
  • --dry-run: Show what would be extracted and translated without saving to files (useful for CI or review workflows).
  • --cleanup: Remove obsolete translation keys from JSON files that are no longer found in any of your Blade views.

Cleanup Example

Over time, your translation files may collect obsolete strings that were deleted from your Blade files. The script will automatically warn you if it detects unused translations. To safely remove them:

php artisan lang:extract welcome --locales=fr --cleanup

Dry Run Example

If you want to preview the extraction and translation without modifying any JSON files, use the --dry-run flag:

php artisan lang:extract welcome --locales=fr --dry-run

Configuration

Edit config/locale-generator.php to customize:

  • views_path: Directory for Blade files (default: resources/views).
  • google_translate.key_file: Path to Google Cloud service account JSON key.
  • google_translate.project_id: Google Cloud project ID.
  • deepl_api_key: DeepL API key.
  • api_limit.chunk_size: Number of strings to send in a single API request (default: 20).
  • api_limit.sleep_duration: Seconds to wait between chunked API requests to prevent rate limiting (default: 1).

Requirements

  • PHP 8.1+
  • Laravel 9.0, 10.0, 11.0, 12.0
  • Google Cloud Translate API (for google provider)
  • DeepL API (for deepl provider)

License

This package is open-sourced software licensed under the MIT license.