azizdevfull / lang-gen
Automatic translation generator for Laravel
Requires
- illuminate/console: ^10.0|^11.0|^12.0
- illuminate/filesystem: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^10.9
- phpunit/phpunit: ^12.5
README
Automatically generate missing translation keys in your Laravel application.
LangGen scans your application code (app and resources/views) for translation keys like __('messages.welcome') or @lang('auth.failed') and automatically creates the corresponding PHP language files.
Unlike other tools, LangGen supports nested array keys (dot-notation) and creates clean, native PHP arrays without needing a database.
๐ Features
- Auto-Discovery: Scans PHP and Blade files for
__(), @lang(), trans(). - JSON Support: Handles keys with spaces or single strings (e.g.,
__('Log in')) by generatinglang/{code}.json. - Nested Arrays: Converts
auth.password.mininto['password' => ['min' => '...']]. - Smart Conflict Handling: Configurable policy to
preserveoroverwriteexisting keys. - Custom Paths: Configurable directories to scan (e.g.,
Modules,routes). - Native PHP Files: Works with standard Laravel
lang/*.phpfiles. - No Database Required: Lightweight and zero-setup.
๐ฅ Video Tutorial
You can see how LangGen works and how to use it in a real project in this YouTube video:
๐ Watch on YouTube: https://youtu.be/UCr1kR8rMy8
๐ฆ Installation
You can install the package via composer:
composer require azizdevfull/lang-gen --dev
Optionally, you can publish the configuration file:
php artisan vendor:publish --tag="lang-gen-config"
๐ Usage
Basic Usage
Run the command to scan your codebase and generate missing keys for the default language (default: en):
php artisan lang:gen
Specify Language
To generate translations for a specific language (e.g., Uzbek):
php artisan lang:gen uz
This will:
- Scan your configured directories (default:
app,resources/views,routes). - Find all translation keys (nested arrays and JSON strings).
- Create or update
lang/uz/messages.phpandlang/uz.json. - Populate missing keys with a readable default value (e.g., "Messages Welcome").
โ๏ธ Configuration
The configuration file config/lang-gen.php allows you to customize the behavior:
<?php return [ /* |-------------------------------------------------------------------------- | Scanned Directories |-------------------------------------------------------------------------- | | Here you may define the directories that LangGen should scan for | translation keys. By default, we scan the 'app', 'resources/views', | and 'routes' directories. | | You can add custom paths here, for example: base_path('Modules') | */ 'paths' => [ base_path('app'), base_path('resources/views'), base_path('routes'), ], /* |-------------------------------------------------------------------------- | Conflict Policy |-------------------------------------------------------------------------- | | Determines how to handle specific key conflicts. | | Scenario: | You have an existing key as a string: 'messages.home' => 'Home' | But your code now uses a nested key: __('messages.home.title') | | Options: | 'preserve' - Keeps the existing string value. The new nested key is skipped/ignored. | 'overwrite' - Replaces the existing string with an array to allow the new nested key. | */ 'conflict_policy' => 'overwrite', // 'preserve' or 'overwrite' /* |-------------------------------------------------------------------------- | Default Language |-------------------------------------------------------------------------- | | The default language code to generate translations for. | Examples: 'en', 'uz', 'ru'. | */ 'default_lang' => 'uz', ];
๐ก Example
In your code (resources/views/welcome.blade.php):
<h1>{{ __('home.hero.title') }}</h1> <p>@lang('home.hero.subtitle')</p>
Run command:
php artisan lang:gen en
Result (lang/en/home.php):
<?php return [ 'hero' => [ 'title' => 'Home Hero Title', 'subtitle' => 'Home Hero Subtitle', ], ];
JSON Key Example
In your code:
<button>{{ __('Create Account') }}</button>
Result (lang/en.json):
{
"Create Account": "Create Account"
}
๐งช Testing
composer test
๐ Security
If you discover any security related issues, please email aziz16110904@gmail.com instead of using the issue tracker.
๐ฅ Credits
๐ License
The MIT License (MIT). Please see License File for more information.