skysplit / laravel5-intl-translation
Laravel 5 包,使用 php-intl 扩展提供更好的翻译语法
Requires
- php: >=7.4.0
- ext-intl: *
- illuminate/support: ^8
- illuminate/translation: ^8
- illuminate/validation: ^8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: 6.2.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-21 17:34:27 UTC
README
介绍
Laravel Intl Translator 使用 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,
并添加行
Skysplit\Laravel\Translation\TranslationServiceProvider::class, Skysplit\Laravel\Translation\ValidationServiceProvider::class,
发布配置和语言文件
小心!这将覆盖您现有的
resources/lang/{lang}文件!检查 当前适配的语言区域 表以查看哪些文件可能会被覆盖。
php artisan vendor:publish --provider="Skysplit\Laravel\Translation\TranslationServiceProvider" --force
如果您只想发布配置
php artisan vendor:publish --provider="Skysplit\Laravel\Translation\TranslationServiceProvider" --tag=config
如果您只想发布一组语言文件
php artisan vendor:publish --provider="Skysplit\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 还支持 序数、spellout、数字、日期、时间 和 持续时间 格式化。
有关详细信息,请访问这个优秀的Yii2框架国际化指南,它详细覆盖了每个国际主题。