amirami / localizator
Localizator 是一个为 Laravel 开发的小型工具,它允许您从项目文件中提取未翻译的字符串。它通过 artisan 命令行和提供的本地化命令工作。
Requires
- php: ^7.2.5|^8.0
- ext-json: *
- illuminate/config: ^8.0|^9.0|^10.0|^11.0
- illuminate/console: ^8.0|^9.0|^10.0|^11.0
- illuminate/filesystem: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
- symfony/finder: ^5.1|^6.0|^7.0
Requires (Dev)
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^6.6|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5|^10.0
This package is auto-updated.
Last update: 2024-08-27 10:00:17 UTC
README
Localizator 是一个为 Laravel 开发的小型工具,它允许您使用一条命令从您的项目文件中提取未翻译的字符串。
支持
兼容性
安装
您可以通过 composer 安装此包
composer require --dev amirami/localizator
此包使用 Laravels 包自动发现机制,这意味着如果您在生产环境中未安装开发依赖项,它也不会被加载。
如果您出于某些原因想要手动控制此操作
- 将包添加到
composer.json
中的extra.laravel.dont-discover
键,例如"extra": { "laravel": { "dont-discover": [ "amirami/localizator" ] } }
- 将以下类添加到
config/app.php
中的providers
数组Amirami\Localizator\ServiceProvider::class,
如果您只想在非生产环境中手动加载它,则可以将此添加到您的AppServiceProvider
中的register()
方法public function register() { if ($this->app->isLocal()) { $this->app->register(\Amirami\Localizator\ServiceProvider::class); } // ... }
注意:请避免在开发环境中缓存配置,这可能会在安装此包后导致问题;相应地,在运行命令时遇到问题之前,请先通过
php artisan cache:clear
清除缓存
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Amirami\Localizator\ServiceProvider" --tag="config"
这是发布配置文件的内容
<?php return [ /** * Localize types of translation strings. */ 'localize' => [ /** * Short keys. This is the default for Laravel. * They are stored in PHP files inside folders name by their locale code. * Laravel comes with default: auth.php, pagination.php, passwords.php and validation.php */ 'default' => true, /** * Translations strings as key. * They are stored in JSON file for each locale. */ 'json' => true, ], /** * Search criteria for files. */ 'search' => [ /** * Directories which should be looked inside. */ 'dirs' => ['resources/views'], /** * Subdirectories which will be excluded. * The values must be relative to the included directory paths. */ 'exclude' => [ // ], /** * Patterns by which files should be queried. * The values can be a regular expression, glob, or just a string. */ 'patterns' => ['*.php'], /** * Functions that the strings will be extracted from. * Add here any custom defined functions. * NOTE: The translation string should always be the first argument. */ 'functions' => ['__', 'trans', '@lang'] ], /** * Should the localize command sort extracted strings alphabetically? */ 'sort' => true, ];
用法
要提取所有字符串,只需运行
php artisan localize de,fr
此命令将在 resources/lang
目录中创建(如果不存在)de.json
和 fr.json
文件。如果您在文件中启用了并使用了短键(例如 pagination.next
),本地化命令将在 resources/lang
目录中创建 de
和 fr
文件夹,并使用短键的前缀创建 PHP 文件(例如 pagination.php
)。
您也可以在没有国家代码参数的情况下运行 artisan 命令。
php artisan localize
在这种情况下,将根据 app.locale
配置中指定的语言生成翻译字符串。
注意:您已翻译的字符串将不会被覆盖。
删除缺失的键
默认情况下,即使本地化命令下一次运行时这些字符串不存在,本地文件中的字符串也会被保留。如果您想删除那些不再存在于您的文件中的键,可以将 --remove-missing 选项追加到本地化命令。
php artisan localize --remove-missing
键排序
默认情况下,JSON 文件内生成的字符串将按照键的字母顺序排序。如果您想关闭此功能,只需在配置文件中将 sort => false
设置为 true。
搜索
字符串提取的方式很简单。
我们在 search.dirs
配置中定义的目录中查找,使用 search.patterns
中定义的模式匹配文件,并最后提取 search.functions
中定义的函数的第一个参数作为字符串。
您可以在配置文件中更改这些值以满足您的需求。
测试
composer test
变更日志
请参阅变更日志获取最近更改的更多信息。
贡献
请参阅贡献指南获取详细信息。
安全漏洞
请查阅我们的安全策略了解如何报告安全漏洞。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。