这是 Twig Intl 扩展包的 Laravel 端口。
该包可用于任何基于 Laravel 的应用程序,通过在 Blade 模板或 Laravel 代码库中提供帮助函数来快速处理国际化。
使用作曲家:
composer require bakame/laravel-intl-formatter
为了编辑默认配置,您需要将包配置发布到应用程序配置目录:
php artisan vendor:publish --provider= " BakameLaravelIntl " --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
设置,则该函数将使用IlluminateSupportFacadesApp::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 ' );
欢迎贡献,并将全额记入。有关详细信息,请参阅贡献和行为准则。
图书馆:
要运行测试,请从项目文件夹运行以下命令。
$ composer test
如果您发现任何与安全相关的问题,请发送电子邮件至 [email protected],而不是使用问题跟踪器。
包助手函数很大程度上受到 Fabien Potencier 之前在 Twig Intl Extension 上所做的工作的启发。
麻省理工学院许可证 (MIT)。请参阅许可证文件以获取更多信息。