commerceguys/intl
Composer 安装命令:
composer require commerceguys/intl
包简介
Internationalization library powered by CLDR data.
README 文档
README
A PHP 8.0+ internationalization library, powered by CLDR data.
Features:
- NumberFormatter and CurrencyFormatter, inspired by intl.
- Currencies
- Languages
Looking for a list of countries and subdivisions? Check out commerceguys/addressing.
Why not use the intl extension?
The intl extension isn't present by default on PHP installs, requiring it can hurt software adoption. Behind the scenes the extension relies on libicu which includes the CLDR dataset, but depending on the OS/distribution used, could be several major CLDR releases behind.
Since the CLDR dataset is freely available in JSON form, it is possible to reimplement the intl functionality in pure PHP code while ensuring that the dataset is always fresh.
Having access to the CLDR dataset also makes it possible to offer additional APIs, such as listing all currencies.
More backstory can be found in this blog post.
Formatting numbers
Allows formatting numbers (decimals, percents, currency amounts) using locale-specific rules.
Two formatters are provided for this purpose: NumberFormatter and CurrencyFormatter.
use CommerceGuys\Intl\Currency\CurrencyRepository; use CommerceGuys\Intl\NumberFormat\NumberFormatRepository; use CommerceGuys\Intl\Formatter\NumberFormatter; use CommerceGuys\Intl\Formatter\CurrencyFormatter; $numberFormatRepository = new NumberFormatRepository; // Options can be provided to the constructor or the // individual methods, the locale defaults to 'en' when missing. $numberFormatter = new NumberFormatter($numberFormatRepository); echo $numberFormatter->format('1234.99'); // 1,234.99 echo $numberFormatter->format('0.75', ['style' => 'percent']); // 75% $currencyRepository = new CurrencyRepository; $currencyFormatter = new CurrencyFormatter($numberFormatRepository, $currencyRepository); echo $currencyFormatter->format('2.99', 'USD'); // $2.99 // The accounting style shows negative numbers differently and is used // primarily for amounts shown on invoices. echo $currencyFormatter->format('-2.99', 'USD', ['style' => 'accounting']); // (2.99$) // Arabic, Arabic extended, Bengali, Devanagari digits are supported as expected. $currencyFormatter = new CurrencyFormatter($numberFormatRepository, $currencyRepository, ['locale' => 'ar']); echo $currencyFormatter->format('1230.99', 'USD'); // US$ ١٬٢٣٠٫٩٩ // Parse formatted values into numeric values. echo $currencyFormatter->parse('US$ ١٬٢٣٠٫٩٩', 'USD'); // 1230.99
Currencies
use CommerceGuys\Intl\Currency\CurrencyRepository; // Reads the currency definitions from resources/currency. $currencyRepository = new CurrencyRepository; // Get the USD currency using the default locale (en). $currency = $currencyRepository->get('USD'); echo $currency->getCurrencyCode(); // USD echo $currency->getNumericCode(); // 840 echo $currency->getFractionDigits(); // 2 echo $currency->getName(); // US Dollar echo $currency->getSymbol(); // $ echo $currency->getLocale(); // en // Get the USD currency using the fr-FR locale. $currency = $currencyRepository->get('USD', 'fr-FR'); echo $currency->getName(); // dollar des États-Unis echo $currency->getSymbol(); // $US echo $currency->getLocale(); // fr-FR // Get all currencies, keyed by currency code. $allCurrencies = $currencyRepository->getAll();
Languages
use CommerceGuys\Intl\Language\LanguageRepository; // Reads the language definitions from resources/language. $languageRepository = new LanguageRepository; // Get the german language using the default locale (en). $language = $languageRepository->get('de'); echo $language->getLanguageCode(); // de echo $language->getName(); // German // Get the german language using the fr-FR locale. $language = $languageRepository->get('de', 'fr-FR'); echo $language->getName(); // allemand // Get all languages, keyed by language code. $allLanguages = $languageRepository->getAll();
Related projects
commerceguys/intl 适用场景与选型建议
commerceguys/intl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.55M 次下载、GitHub Stars 达 368, 最近一次更新时间为 2014 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 commerceguys/intl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 commerceguys/intl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10.55M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 371
- 点击次数: 37
- 依赖项目数: 37
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-08-22