亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
Install and Enable the intl Extension
Format Dates, Numbers, and Currencies by Locale
Translate Messages Using MessageFormatter
Sort Strings According to Locale (Collator)
Detect and Set Default Locale
首頁 后端開發(fā) php教程 如何將INTL擴(kuò)展用于PHP國際化

如何將INTL擴(kuò)展用于PHP國際化

Oct 04, 2025 am 12:51 AM
php 國際化

答案:PHP的intl擴(kuò)展基于ICU庫實(shí)現(xiàn)國際化,支持多語言格式化、翻譯和排序。首先安裝并啟用intl擴(kuò)展,Linux系統(tǒng)使用apt-get或yum安裝,Windows在php.ini中開啟extension=intl。通過NumberFormatter按地區(qū)格式化數(shù)字,如de_DE輸出1.234.567,89;IntlDateFormatter處理日期顯示,如fr_FR顯示“l(fā)undi 4 septembre 2023”;CurrencyFormatter格式化貨幣,en_US顯示$99.99。MessageFormatter支持帶占位符的翻譯,自動處理復(fù)數(shù)和語法,如es_ES將2映射為“2 libros”。Collator類按語言規(guī)則排序,sv_SE正確排列瑞典語字符順序。使用Locale::acceptFromHttp從HTTP頭檢測用戶語言,或設(shè)默認(rèn)locale如fr_FR,多數(shù)類會自動繼承。只需啟用擴(kuò)展,選擇合適類并傳入locale即可實(shí)現(xiàn)全球化功能。

How to use the intl extension for internationalization in PHP

The intl extension in PHP provides powerful tools for internationalization (i18n), allowing your applications to handle multiple languages, formats, and regional settings. It's built on top of the ICU (International Components for Unicode) library and supports features like locale-aware formatting, message translation, collation, and more. Here’s how to use it effectively.

Install and Enable the intl Extension

Before using intl, make sure it’s installed and enabled in your PHP environment.

  • On Ubuntu/Debian: Install via sudo apt-get install php-intl
  • On CentOS/RHEL: Use sudo yum install php-intl or dnf install php-intl
  • On Windows: Uncomment extension=intl in php.ini
  • Verify installation with php -m | grep intl or check phpinfo()

Format Dates, Numbers, and Currencies by Locale

The intl extension includes classes like NumberFormatter, IntlDateFormatter, and CurrencyFormatter to display data according to user locale.

Format numbers:

$formatter = new NumberFormatter('de_DE', NumberFormatter::DECIMAL);
echo $formatter->format(1234567.89); // Output: 1.234.567,89

Format dates:

$formatter = new IntlDateFormatter(
    'fr_FR',
    IntlDateFormatter::FULL,
    IntlDateFormatter::NONE,
    'Europe/Paris'
);
echo $formatter->format(time());
// Output: lundi 4 septembre 2023

Format currency:

$formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
echo $formatter->formatCurrency(99.99, 'USD'); // $99.99

Translate Messages Using MessageFormatter

Use MessageFormatter to support dynamic, localized messages with placeholders.

$message = "{0, plural, one {# book} other {# books}}";
$formatter = new MessageFormatter('es_ES', $message);
echo $formatter->format([2]); // "2 libros"

This handles pluralization and grammar rules per language automatically. You can also use named arguments and select clauses for gender or categories.

Sort Strings According to Locale (Collator)

Standard string sorting may not respect language-specific rules. Use Collator for accurate sorting.

$collator = new Collator('sv_SE'); // Swedish
$words = ['zoo', '?tta', '?pple', '?r'];
$collator->asort($words);
print_r($words); // Correct Swedish order: ?pple, ?r, ?tta, zoo

Without Collator, sorting would be based on ASCII values and incorrect for many languages.

Detect and Set Default Locale

You can detect user locale from HTTP headers or set a default for your app.

// Get best match from browser Accept-Language
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'en');
setlocale(LC_ALL, $locale);
<p>// Or set manually
Locale::setDefault('fr_FR');</p>

Many intl classes use the default locale if none is specified.

Basically just enable intl, pick the right formatter or collator, and pass the correct locale. It handles the rest—no need to reinvent date formats or sort rules for every language.

以上是如何將INTL擴(kuò)展用于PHP國際化的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Stock Market GPT

Stock Market GPT

人工智能驅(qū)動投資研究,做出更明智的決策

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

熱門話題

漫天星漫畫完整版入口_滿天星漫畫去廣告特別版鏈接 漫天星漫畫完整版入口_滿天星漫畫去廣告特別版鏈接 Sep 28, 2025 am 10:30 AM

漫天星漫畫完整版入口為https://www.mantianxingmh.com,平臺涵蓋熱血、戀愛、懸疑、科幻等多種題材,資源豐富且更新及時(shí),支持分類檢索;提供高清畫質(zhì)、多種翻頁模式、自定義背景與亮度調(diào)節(jié),具備護(hù)眼模式優(yōu)化閱讀體驗(yàn);用戶可創(chuàng)建書架、保存閱讀記錄、離線下載并實(shí)現(xiàn)跨設(shè)備同步進(jìn)度。

如何在PHP中使用面向?qū)ο蟮木幊蹋∣OP)? 如何在PHP中使用面向?qū)ο蟮木幊蹋∣OP)? Sep 28, 2025 am 03:26 AM

oopinphporganizesCodeIntOrsableClassesandObjects.1.ClassesdefinePropertiesandMethods,IntantiatedVia $ this.2.Constructors(__構(gòu)造)initializeObjectsproperties.3.accessmodifiers(公共,私人,私人,procected)ControlVisibility.4.Inheritance(Hersheritance(Extents))允許

如何在PHP MySQL中獲取最后一個(gè)插入的ID? 如何在PHP MySQL中獲取最后一個(gè)插入的ID? Sep 28, 2025 am 05:57 AM

使用mysqli_insert_id()(過程風(fēng)格)、$mysqli->insert_id(對象風(fēng)格)或$pdo->lastInsertId()(PDO)可獲取最后插入的ID,需在同連接中立即調(diào)用以確保準(zhǔn)確性。

如何在PHP中使用最終類和方法? 如何在PHP中使用最終類和方法? Sep 28, 2025 am 05:55 AM

finalClassEndMethodsInphpprevEntinHeritanceanDoverRidingToprotectecticalCode.2.afinalClassCannotBexended,確保behaviormainsunchanged.3.afinalmethodcannodcannodcannodcannodcannotbeoverridden,preserervingConsistentImpplementImpplementActatimpplentatimplectationAccsSssSssSsSsSsSsSsSsSsSsSsseClass.4.4.usefinalfinalfinalfinalfinalfinalfilitfinalfilit

如何在PHP中回聲HTML標(biāo)簽 如何在PHP中回聲HTML標(biāo)簽 Sep 29, 2025 am 02:25 AM

使用單引號或轉(zhuǎn)義雙引號在PHP中輸出HTML,推薦用單引號包裹字符串以避免屬性引號沖突,可結(jié)合變量拼接或heredoc語法生成動態(tài)內(nèi)容。

如何使用PHP中的GET請求變量? 如何使用PHP中的GET請求變量? Sep 29, 2025 am 01:30 AM

Use$_GETtoaccessURLquerystringvariablesinPHP,suchasname=Johnandage=30fromhttps://example.com/search.php?name=John&age=30;alwaysvalidateandsanitizeinputsusingfilter_input()andavoidsensitivedatainURLsduetoexposurerisks.

什么是特征以及如何在PHP中使用它們 什么是特征以及如何在PHP中使用它們 Oct 02, 2025 am 04:17 AM

特質(zhì)sinphpenablehorizo??ntalcodereusebyAllowingClassobalingMethodMethodSsobabableTraitContainers,旁路lephingsingleinheritancelimits.forexample,theloggabletraitprovidesalog(theloggabletraitprovidesalog)()methodyClassusisitit,suptoyclassusisitit,shisthencuser,shisthencuser,shisthencallencall $ the canthencall $ thiscrigthiscrea thiscreacreacrea

MBTI免費(fèi)測試官網(wǎng)入口_ MBTI測試免費(fèi)網(wǎng)站網(wǎng)址鏈接 MBTI免費(fèi)測試官網(wǎng)入口_ MBTI測試免費(fèi)網(wǎng)站網(wǎng)址鏈接 Sep 28, 2025 am 10:00 AM

MBTI免費(fèi)測試官網(wǎng)入口是https://www.16personalities.com/,該網(wǎng)站提供中英文版本測試,涵蓋性格維度分析、個(gè)性化報(bào)告及多場景應(yīng)用建議,幫助用戶深入了解自身人格類型。

See all articles