這是 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)。請參閱許可證文件以獲取更多資訊。