承接 hsm/lokale 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hsm/lokale

最新稳定版本:v1.1.6

Composer 安装命令:

composer require hsm/lokale

包简介

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

README 文档

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

统计信息

  • 总下载量: 307
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-12