yannxtrem/laralang

A Laravel package to extract translatable strings from views and Livewire components into a JSON file.

Maintainers

Package info

github.com/yannXtrem/laralang

pkg:composer/yannxtrem/laralang

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v1.0.0 2026-01-23 21:47 UTC

This package is auto-updated.

Last update: 2026-03-23 22:14:14 UTC


README

Latest Stable Version License

Laravel Lang Extractor is a lightweight, zero-dependency development tool for Laravel 11 & 12.

It automatically scans your application's Views and Livewire components to find translatable strings (using the __('key') helper), deduplicates them, and exports them into a clean JSON file ready for translation.

It replaces complex Bash scripts or heavy dependencies when you just need a simple JSON extraction.

🚀 Features

  • Recursive Scanning: Scans resources/views (Blade files) and app/Livewire (Component classes).
  • Regex Extraction: Detects __('string') and __("string").
  • Smart Deduplication: Automatically removes duplicate keys.
  • JSON Output: Generates a valid JSON file compatible with Laravel's JSON translation support.
  • Unicode Support: Preserves accents and special characters (no escaped Unicode sequences).
  • Laravel Native: Fully integrated Artisan command.

📋 Requirements

  • PHP 8.2 or higher
  • Laravel 11.0 or higher

📦 Installation

Install the package via Composer as a development dependency:

composer require yannxtrem/laralang --dev

Note: Since this tool is used during development to generate translation files, it is recommended to install it with the --dev flag.

🛠 Usage

Basic Extraction

Run the artisan command to scan your project and generate the JSON file:

php artisan laralang:extract

By default, this will create a file named a_trad.json in your project root.

Custom Output File

You can specify the output filename and path using the --output option:

php artisan laralang:extract --output=lang/fr.json

Or for another language:

php artisan laralang:extract --output=lang/es.json

🔍 How it works

  1. Scan: The command looks for *.php and *.blade.php files in:
    • resources/views/
    • app/Livewire/
  2. Extract: It applies a regex pattern to find usage of the Laravel translation helper:
    • Matches: __('Hello World')
    • Matches: __("Hello World")
  3. Process:
    • Keys are sorted alphabetically.
    • Duplicates are removed.
  4. Save: The result is saved as a JSON object where the key is the string found, and the value is an empty string (or the existing translation if you implement merging logic in the future).

Example Output

{
    "Back to home": "",
    "Confirm password": "",
    "Login": "",
    "Welcome to our application": ""
}

⚡ Workflow Example

  1. Develop your features using __('My text') in Blade or PHP files.
  2. Run php artisan translation:extract --output=lang/fr.json.
  3. Open lang/fr.json and fill in the empty values with translations.
  4. Laravel automatically uses these translations when the locale is set to fr.

🤝 Contribution

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

📄 License

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