alright / laravel-icu
Laravel 包,使用 php-intl 扩展改善 ICU 翻译语法
Requires
- ext-intl: *
- illuminate/support: ^5.3
- illuminate/translation: ^5.3
- illuminate/validation: ^5.3
Requires (Dev)
- orchestra/testbench: ^3.3
- phpunit/phpunit: ^4.0|^5.0
README
简介
Laravel ICU 使用 php-intl 扩展为您的应用程序提供翻译。
请注意,此包会打破验证器框架的默认行为。
由于 MessageFormatter::formatMessage
方法,Validator::replacer
方法应返回 键值对数组作为参数,而不是在消息中替换占位符。
除此之外,app('translator')->get($key)
总是返回原始格式的消息(未经解析)。翻译消息由
app('translator')->trans($key) app('translator')->formatMessage($locale, $message, $params)
需求
- Laravel 6
- php-intl 扩展已安装
请随意为此包贡献以支持其他 Laravel 版本!
安装
如果您没有 php-intl 扩展,可以通过以下命令安装它(Ubuntu,Debian)
$ sudo apt-get install php-intl
如果您有其他操作系统,您可以使用相应的包管理器
所有版本
在您的 config/app.php
提供者中
删除行
Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class,
并添加行
Alright\Laravel\Translation\TranslationServiceProvider::class, Alright\Laravel\Translation\ValidationServiceProvider::class,
发布配置和语言文件
小心!这将覆盖您现有的
resources/lang/{lang}
文件!查看 当前适配的本地化 表以查看哪些文件可能会被覆盖。
php artisan vendor:publish --provider="Alright\Laravel\Translation\TranslationServiceProvider" --force
如果您只想发布配置
php artisan vendor:publish --provider="Alright\Laravel\Translation\TranslationServiceProvider" --tag=config
如果您只想发布一组语言文件
php artisan vendor:publish --provider="Alright\Laravel\Translation\TranslationServiceProvider" --force --tag="lang.{locale}[,lang.{other_locale}]"
当前适配的本地化
使用示例
both trans()
和 trans_choice()
辅助函数都使用此翻译器,因此您唯一需要更改的是您的语言文件。
有关详细文档,请访问 php 的 MessageFormatter 文档及其相关链接
占位符
app/resources/lang/en/custom.php
return [ 'placeholder' => 'Hello there, {username}!' ]
view.blade.php
{{ trans('custom.placeholder', ['username' => 'Jane']); }}
返回
Hello there, Jane!
选择
app/resources/lang/en/custom.php
return [ 'select' => '{gender, select, male{He} female{She} other{It}} has two legs and is {gender}!' ]
view.blade.php
{{ trans('custom.select', ['gender' => 'male']); }} {{ trans('custom.select', ['gender' => 'female']); }} {{ trans('custom.select', ['gender' => 'penguin']); }}
返回
He has two legs and is male!
She has two legs and is female!
It has two legs and is penguin!
复数
app/resources/lang/en/custom.php
return [ 'plural' => 'Jon has {n, plural, =0{no apples} one{# apple} other{# apples}}' ]
view.blade.php
{{ trans_choice('custom.plural', 0); }} {{ trans_choice('custom.plural', 1); }} {{ trans_choice('custom.plural', 2); }}
返回
Jon has no apples
Jon has 1 apples
Jon has 2 apples
除了 trans_choice()
之外,您还可以使用 trans()
辅助函数。
resources/lang/en/custom.php
return [
'custom.plural' => 'Jon has {0, plural, =0{no apples} one{# apple} other{# apples}}, {grapes, plural, =0{no grapes} one{# grape} other{# grapes} and {oranges, plural, =0{no oranges} one{# orange} other{# oranges}}'
];
view.blade.php
{{ trans('custom.plural', [3, 'grapes' => 1, 'oranges' => 0]) }}
返回
Jon has 3 apples, 1 grape and no oranges
如你所见,trans_choice()
做的唯一一件事是将第一个参数作为 n
参数传递给 trans()
辅助函数。
复数偏移
您可以为您的复数规则设置偏移量。考虑以下示例
You {n, plural, offset:1 =0{do not like this yet} =1{liked this} one{and one other person liked this} other{and # others liked this}}
结果
You do not like this yet // n = 0
You liked this // n = 1
You and one other person liked this // n = 2
You and 2 others liked this // n = 3
You and 3 others liked this // n = 4
对于某些语言,复数规则通常非常复杂。Intl 会为您处理。
例如,在波兰语中,当 n % 10 = 2..4
且 n % 100 != 12..14
时应用 few
规则,而当 n != 1
且 n % 10 = 0..1
或 n % 10 = 5..9
或 n % 100 = 12..14
时应用 many
规则。
在塞尔维亚语中,当 n = 1
时将匹配 =1
,但 one
将在 n = 1, 21, 31, 41
等 时应用。
记住!您 始终 必须为复数翻译提供
other
规则。
有关复数化的更多详细信息,请访问 CLDR Plural Rules 规范和 CLDR 语言复数规则。
详细格式化
PHP 的 MessageFormatter 还支持 序数、拼写、数字、日期、时间 和 持续时间 格式化。
有关详细信息,请访问这个优秀的 Yii2 框架 i18n 指南,它详细介绍了每个 intl 主题。