bakame / laravel-intl-formatter
Laravel 应用程序内部化的全局辅助工具
Requires
- php: ^7.4 || ^8.0
- ext-intl: *
- ext-json: *
- bakame/intl-formatter: ~0.3.0
- illuminate/config: ^8.0 || ^9.0
- illuminate/support: ^8.0 || ^9.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- moneyphp/money: ^3.0 || ^4.0
- nunomaduro/larastan: ^1.0.0 || ^2.1
- orchestra/testbench: ^6.6.0 || ^7.5
- phpstan/phpstan: ^1.7
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.1
- phpstan/phpstan-strict-rules: ^1.2
- phpunit/phpunit: ^9.5
Suggests
- moneyphp/money: To format currency based on the Money library
README
这是 Twig Intl Extension 包的 Laravel 版本。
该包可以用于任何基于 Laravel 的应用程序,通过提供 Blade 模板或 Laravel 代码库中的辅助函数来快速处理国际化。
系统要求
- Laravel 8 和/或 9
- Symfony Intl 组件
安装
使用 composer
composer require bakame/laravel-intl-formatter
配置
为了编辑默认配置,您需要将包配置发布到您的应用程序配置目录
php artisan vendor:publish --provider="Bakame\Laravel\Intl" --tag=config
配置文件将发布到您的应用程序目录中的 config/bakame-intl-formatter.php
。
请参考配置文件以了解可用选项的概述。
文档
一旦安装,该包提供以下全局辅助函数
国家名称
根据其两字母/五字母代码返回国家名称;
country name: {{ country_name($country, $locale) }}
echo view($templatePath, ['country' => 'FR', 'locale' => 'NL'])->render(); // country name: Frankrijk
货币名称
根据其三位字母代码返回货币名称;
currency name: {{ currency_name($currency, $locale) }}
echo view($templatePath, ['currency' => 'JPY', 'locale' => 'PT'])->render(); // currency name: Iene japonês
货币符号
根据其三位字母代码返回货币符号;
currency symbol: {{ currency_symbol($currency, $locale) }}
echo view($templatePath, ['currency' => 'JPY', 'locale' => 'PT'])->render(); // currency symbol: JP¥
语言名称
根据其三位字母代码返回货币符号;
language name: {{ language_name($language, $locale) }}
echo view($templatePath, ['language' => 'it', 'locale' => 'nl'])->render(); // language name: Italiaans
区域名称
根据其三位字母代码返回货币符号;
locale name: {{ locale_name($data, $locale) }}
echo view($templatePath, ['data' => 'sw', 'locale' => 'nl'])->render(); // locale name: Swahili
时区名称
根据其标识符返回时区名称;
timezone name: {{ locale_name($data, $locale) }}
echo view($templatePath, ['timezone' => 'Asia/Tokyo', 'locale' => 'es'])->render(); // timezone name: hora de Japón (Tokio)
国家时区
根据给定国家代码返回时区标识符;
country timezones: {{ implde(", ", country_timezones($country)) }}
$content = view($templatePath, ['country' => 'CD', 'locale' => 'es'])->render(); echo $content, PHP_EOL; // country timezones: Africa/Kinshasa, Africa/Lubumbashi
格式化货币
将数字格式化为货币;
format currency: {{ format_currency($amount, $currency, $attrs, $locale) }}
$templateData = [ 'amount' => 100.356, 'currency' => 'USD', 'locale' => 'ES', 'attrs' => [ 'fraction_digit' => 1, 'rounding_mode' => 'floor', ] ]; echo view($templatePath, $templateData)->render(); // format currency: 100,3 US$
格式化数字
格式化数字;
format number: {{ format_number($number, $locale, $attrs) }}
$templateData = [ 'number' => 100.356, 'locale' => 'nl', 'style' => 'spellout', 'type' => 'double', 'attrs' => [ 'fraction_digit' => 1, 'rounding_mode' => 'floor', ] ]; echo view($templatePath, $templateData)->render(); // format number: honderd komma drie
格式化日期和时间
格式化日期和时间;
format datetime: {{ format_datetime($date, $locale, $timezone, $dateFormat, $timeFormat, $pattern, $calendar) }}
$templateData = [ 'date' => 'yesterday', 'dateFormat' => 'full', 'timeFormat' => 'full', 'pattern' => '' , 'timezone' => 'Africa/Lubumbashi', 'calendar' => 'gregorian' , 'locale' => 'sw', ]; echo view($templatePath, $templateData)->render(); // format datetime: Alhamisi, 2 Juni 2022 00:00:00 Saa za Afrika ya Kati
格式化日期
格式化日期时间中的日期部分;
format date: {{ format_date($date, $locale, $timezone, $dateFormat, $pattern, $calendar) }}
$templateData = [ 'date' => 'yesterday', 'dateFormat' => 'long', 'pattern' => '' , 'timezone' => 'Africa/Lubumbashi', 'calendar' => 'gregorian' , 'locale' => 'sw', ]; echo view($templatePath, $templateData)->render(); // format date: 2 Juni 2022
格式化时间
格式化日期时间中的时间部分;
format time: {{ format_time($date, $locale, $timezone, $timeFormat, $pattern, $calendar) }}
$templateData = [ 'date' => 'yesterday', 'dateFormat' => 'full', 'pattern' => '' , 'timezone' => 'Africa/Lubumbashi', 'calendar' => 'gregorian' , 'locale' => 'sw', ]; echo view($templatePath, $templateData)->render(); // format time: 00:00:00 Saa za Afrika ya Kati
每个函数使用与 Twig Extra 包的筛选器/函数相同的参数顺序。
区域指定
如果在函数调用中未指定 locale
,则函数将使用 Illuminate\Support\Facades\App::currentLocale()
的结果作为要使用的区域值。
函数签名
在 PHP8+ 中,您可以使用命名参数来改进函数的使用,因为它们通常有很多参数
在 PHP7.4
<?php echo format_datetime('2019-08-07 23:39:12', 'fr', null, medium', 'medium', '', 'gregorian', 'fr');
在 PHP8+
<?php echo format_datetime(date: '2019-08-07 23:39:12', locale: 'fr');
贡献
欢迎贡献,并将得到充分认可。请参阅 CONTRIBUTING 和 CODE OF CONDUCT 了解详细信息。
测试
库
- 有一个 PHPUnit 测试套件
- 有一个使用 PHP CS Fixer 的编码风格合规性测试套件。
- 有一个使用 PHPStan 的代码分析合规性测试套件。
要运行测试,请从项目文件夹中运行以下命令。
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件 nyamsprod@gmail.com 反馈,而不是使用问题跟踪器。
致谢
署名
该包的辅助函数在很大程度上受到了 Fabien Potencier 在 Twig Intl Extension 上的先前工作的启发。
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。