hsm/lokale

Laravel Auto Locale Generator: Scan your Laravel project and automatically generate missing translation keys.

Installs: 302

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/hsm/lokale

v1.1.6 2025-11-20 07:27 UTC

This package is auto-updated.

Last update: 2025-12-20 07:41:13 UTC


README

Lokale is a Laravel package designed to automatically extract and generate translation files by scanning your source code for translation keys. It also supports syncing translations between different locales.

🧩 Installation

Install the package using Composer:

composer require hsm/lokale

⚙️ Configuration

This package uses Laravel's package discovery. If for some reason it's not auto-discovered, register it manually:

'providers' => [
    Hsm\Lokale\LokaleServiceProvider::class,
];

🚀 Usage

1. Generate Locale Files

You can generate translation files by scanning your source code:

php artisan locale:make

Options

Option Description
--locale Target locale to generate files for (default: value of app.locale).
--src Directory to scan for translation keys (default: app/).
--default Used for generating default translation placeholders (default: default).
--comment Adds @TODO comments to missing translations with context information.
--output Output directory for language files (default: lang/).
--no-placeholder Do not generate placeholder values.

Example

php artisan locale:make --locale=fr --src=app --comment

This will generate files like:

resources/lang/fr/messages.php
resources/lang/fr/auth.php

Each file will contain translation keys extracted from __(), trans(), or trans_choice() usage across your source code.

2. Sync Between Locales

You can synchronize translation files between two locales using:

php artisan locale:sync --from=en --to=fr

Options

Option Description
--from Source locale. Required.
--to Target locale. Required.
--output Base directory for language files (default: lang/).
--comment Adds @TODO comments for untranslated keys in target files.
--no-placeholder Do not generate placeholder values.

This is useful when adding new keys in one language and you want the same structure in another language.

3. Generate Attribute Translations

This command generates translation files for form request attributes:

php artisan locale:attributes --locale=fr

Options

Option Description
--locale Target locale.
--src Directory for form requests (default: app/Http/Requests).
--output Output directory (default: lang/).
--comment Add comments for missing translations.
--no-placeholder Do not generate placeholder values.

This helps to keep validation error messages localized and consistent.

🧠 How It Works

  • Scans PHP and Blade files for translation functions (__(), trans(), trans_choice()).
  • Extracts keys and organizes them into proper translation files.
  • Adds @TODO comments for untranslated placeholders when --comment is enabled.
  • Generates modern, readable PHP array syntax.

📂 Output Example

resources/lang/fr/messages.php

<?php
/**
 * Generated with hsm/lokale
 */
return [
    'welcome' => 'Welcome',
    'greeting' => 'Greeting', // @TODO Add translation
];

👨‍💻 Author

📄 License

MIT © Amirhesam Bahmankhah