macmotp / locale
一个用于处理 PHP 中区域设置的精选库
v0.1.0
2024-09-25 03:39 UTC
Requires
- php: >=8.2
- illuminate/collections: >=10.0
- macmotp/money: ^0.4.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.21
- phpunit/phpunit: ^10.2
README
一个简单轻量级的 PHP 库,用于处理国家、语言和时区
适用于任何需要本地化的应用程序。
需求
- PHP >= 8.2
安装
通过 composer 安装此包
composer require macmotp/locale
用法
创建国家对象
use Macmotp\Country; // Create the object $countryCode = Country::US; // Use alpha2 ISO code $country = new Country($countryCode); // equivalent to: // $country = Country::make($countryCode); echo $country->toArray(); // [ // 'continent' => 'North America', // 'name' => 'United States of America', // 'capital' => 'Washington DC', // 'code' => 'US', // 'alpha3_code' => 'USA', // 'dial_code' => '+1', // 'tld' => '.us', // 'date_format' => 'm-d-Y', // 'default_currency_code' => 'USD', // 'default_timezone' => 'America/New_York', // 'default_language' => 'en', // 'currencies' => ['USD'], // 'timezones' => ['America/New_York', ...], // 'languages' => ['en', ...], // ]
基本动态方法列表
getContinent()
:返回大陆;getName()
:返回名称;getCapital()
:返回首都;getCode()
:返回国家代码(alpha2);getAlpha3Code()
:返回国家代码(alpha3);getDialCode()
:返回区号;getTld()
:返回互联网顶级域名;getDateFormat()
:返回日期格式;getDefaultCurrency()
:返回默认货币;getDefaultTimezone()
:返回默认时区;getDefaultLanguage()
:返回默认区域语言;toArray()
:将对象转换为数组;
// Example use Macmotp\Country; $country = new Country(Country::US); echo $country->getDefaultCurrency(); // (string) USD
高级动态方法列表
getCurrencies()
:返回该国家采用的所有货币的集合;getTimezones()
:返回该国家应用的所有时区的集合;getLanguages()
:返回该国家使用的所有语言的集合;
// Example use Macmotp\Country; $country = new Country(Country::US); echo $country->getDefaultCurrency(); // (string) USD
包中的国家列表
use Macmotp\Country; $list = Country::all(); // it returns a Collection with all countries
按属性过滤国家
withCurrency($currencyCode)
:返回该国家采用的所有货币的集合;speaking($language)
:返回使用特定语言的国家的集合;ofContinent($continent)
:返回属于某个大陆的所有国家的集合;
// Examples use Macmotp\Continent; use Macmotp\Country; use Macmotp\Language; echo Country::all()->withCurrency('USD'); // (Collection) [ // [ // 'continent' => 'North America', // 'name' => 'United States of America', // 'code' => 'US', // ... // ], // [ // 'continent' => 'North America', // 'name' => 'El Salvador', // 'code' => 'ES', // ... // ], // ... // ] $list = Country::all()->speaking(Language::ENGLISH); // it returns a Collection with a list of all countries using english language
这些方法可以链式调用,例如:Country::all()->ofContinent(Continent::EUROPE)->speaking(Language::ENGLISH);
支持的区域设置(按区域代码顺序排列)
- Language::ARABIC = 'ar';
- Language::GERMAN = 'de';
- Language::ENGLISH = 'en';
- Language::SPANISH = 'es';
- Language::FRENCH = 'fr';
- Language::ITALIAN = 'it';
- Language::JAPANESE = 'ja';
- Language::KOREAN = 'ko';
- Language::DUTCH = 'nl';
- Language::PORTUGUESE = 'pt';
- Language::CHINESE = 'zh';
本地化
除了默认功能外,还可以本地化响应
use Macmotp\Country; // Create the object with locale $country = new Country(Country::US, Language::SPANISH); echo $country->toArray(); // [ // 'continent' => 'America del Nord', // 'name' => 'Stati Uniti', // ... // ]
测试
composer test
变更日志
请参阅变更日志以获取有关最近更改的更多信息。
贡献
请参阅贡献指南以获取详细信息。
安全漏洞
请参阅我们的安全策略,了解如何报告安全漏洞。
致谢
许可证
MIT 许可证 (MIT)。请参阅许可证文件以获取更多信息。