jrmajor / laravel-fluent
Laravel的Fluent翻译
v1.1.0
2024-03-13 17:12 UTC
Requires
- php: 8.2 - 8.3
- jrmajor/fluent: ^1.0
- laravel/framework: ^10.0 || ^11.0
Requires (Dev)
- jrmajor/cs: ^0.5.6
- orchestra/testbench: ^8.21 || ^9.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2024-09-01 06:56:17 UTC
README
使用Project Fluent(由Mozilla设计的一种本地化系统)释放Laravel应用程序中自然语言的表述能力,以Project Fluent的方式。
阅读Fluent语法指南或尝试在Fluent游乐场中试用以了解更多关于语法的知识。
shared-photos = { $userName } { $photoCount -> [one] added a new photo *[other] added { $photoCount } new photos } to { $userGender -> [male] his stream [female] her stream *[other] their stream }.
__('stream.shared-photos', [ 'userName' => 'jrmajor', 'photoCount' => 2, 'userGender' => 'male', ]); // jrmajor added 2 new photos to his stream.
此包是jrmajor/fluent-php的Laravel包装器。
您可以通过Composer安装它:composer require jrmajor/laravel-fluent
。包将自动注册自己。
用法
此包使用Major\Fluent\Laravel\FluentTranslator
替换了默认的Laravel翻译器。
app('translator') instanceof Major\Fluent\Laravel\FluentTranslator; // true
Fluent翻译存储在.ftl
文件中。将它们放置在您的Laravel应用程序中的.php
翻译文件之间。
/resources
/lang
/en
menu.ftl
validation.php
/pl
menu.ftl
validation.php
如果没有给定键的Fluent消息,翻译器将回退到.php
文件,这允许您逐步引入Fluent翻译格式。
Laravel验证器使用用于替换:attribute
变量的自定义逻辑,并需要深层嵌套的键,这些键在Fluent中不受支持,因此您应保持validation.php
文件在默认的Laravel格式。
trans_choice()
辅助函数始终回退到默认翻译器,因为Fluent格式消除了对此函数的需求。
您可以使用FluentTranslator::addFunction()
方法注册Fluent函数。
配置
可选地,您可以使用以下命令发布配置文件
php artisan vendor:publish --tag fluent-config
这将在config/fluent.php
中发布以下文件
return [ /* * In strict mode, exceptions will be thrown for syntax errors * in .ftl files, unknown variables in messages etc. * It's recommended to enable this setting in development * to make it easy to spot mistakes. */ 'strict' => env('APP_ENV', 'production') !== 'production', /* * Determines if it should use Unicode isolation marks (FSI, PDI) * for bidirectional interpolations. You may want to enable this * behaviour if your application uses right-to-left script. */ 'use_isolating' => false, ];
测试
vendor/bin/phpunit # Tests vendor/bin/phpstan analyze # Static analysis vendor/bin/php-cs-fixer fix # Formatting