• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

resources/H03-May-2022-496,019496,019

scripts/H03-May-2022-717499

src/H03-May-2022-2,9551,195

LICENSEH A D29-Dec-20171.1 KiB2217

README.mdH A D29-Dec-20175.9 KiB164123

phpunit.xmlH A D29-Dec-2017593 2421

README.md

1intl
2=====
3
4[![Build Status](https://travis-ci.org/commerceguys/intl.svg?branch=master)](https://travis-ci.org/commerceguys/intl)
5
6A PHP 5.4+ internationalization library, powered by CLDR data.
7
8Features:
9- NumberFormatter, inspired by [intl](http://php.net/manual/en/class.numberformatter.php).
10- Currencies
11- Countries
12- Languages
13
14Coming soon: date formatting.
15
16Why not use the intl extension?
17-------------------------------
18The intl extension isn't present by default on PHP installs, requiring
19it can hurt software adoption.
20Behind the scenes the extension relies on libicu which includes the CLDR dataset,
21but depending on the OS/distribution used, could be several major CLDR releases behind.
22
23Since the CLDR dataset is freely available in JSON form, it is possible to
24reimplement the intl functionality in pure PHP code while ensuring that the
25dataset is always fresh.
26
27Having access to the CLDR dataset also makes it possible to offer additional APIs,
28such as listing all currencies.
29
30More backstory can be found in [this blog post](https://drupalcommerce.org/blog/15916/commerce-2x-stories-internationalization).
31
32Formatting numbers
33------------------
34Formats numbers (decimals, percents, currency amounts) using locale-specific rules.
35
36This ensures that the decimal and grouping separators, the position of the currency
37symbol, as well as the actual symbol used match what the user is expecting.
38
39The amounts passed for formatting should already be rounded, because the
40formatter doesn't do any rounding of its own.
41
42```php
43use CommerceGuys\Intl\Currency\CurrencyRepository;
44use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
45use CommerceGuys\Intl\Formatter\NumberFormatter;
46
47$currencyRepository = new CurrencyRepository;
48$numberFormatRepository = new NumberFormatRepository;
49
50$currency = $currencyRepository->get('USD');
51$numberFormat = $numberFormatRepository->get('en');
52
53$decimalFormatter = new NumberFormatter($numberFormat);
54echo $decimalFormatter->format('1234.99'); // 123,456.99
55
56$percentFormatter = new NumberFormatter($numberFormat, NumberFormatter::PERCENT);
57echo $percentFormatter->format('0.75'); // 75%
58
59$currencyFormatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY);
60echo $currencyFormatter->formatCurrency('2.99', $currency); // $2.99
61
62// The accounting pattern shows negative numbers differently and is used
63// primarily for amounts shown on invoices.
64$invoiceCurrencyFormatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY_ACCOUNTING);
65echo $invoiceCurrencyFormatter->formatCurrency('-2.99', $currency); // (2.99$)
66
67// Arabic, Arabic extended, Bengali, Devanagari digits are supported as expected.
68$currency = $currencyRepository->get('USD', 'ar');
69$numberFormat = $numberFormatRepository->get('ar');
70$currencyFormatter = new NumberFormatter($numberFormat, NumberFormatter::CURRENCY);
71echo $currencyFormatter->formatCurrency('1230.99', $currency); // US$ ١٬٢٣٠٫٩٩
72
73// Parse formatted values into numeric values.
74echo $currencyFormatter->parseCurrency('US$ ١٬٢٣٠٫٩٩', $currency); // 1230.99
75```
76
77Currencies
78----------
79```php
80use CommerceGuys\Intl\Currency\CurrencyRepository;
81
82// Reads the currency definitions from resources/currency.
83$currencyRepository = new CurrencyRepository;
84
85// Get the USD currency using the default locale (en).
86$currency = $currencyRepository->get('USD');
87echo $currency->getCurrencyCode(); // USD
88echo $currency->getNumericCode(); // 840
89echo $currency->getFractionDigits(); // 2
90echo $currency->getName(); // US Dollar
91echo $currency->getSymbol(); // $
92echo $currency->getLocale(); // en
93
94// Get the USD currency using the fr-FR locale.
95$currency = $currencyRepository->get('USD', 'fr-FR');
96echo $currency->getName(); // dollar des États-Unis
97echo $currency->getSymbol(); // $US
98echo $currency->getLocale(); // fr-FR
99
100$allCurrencies = $currencyRepository->getAll();
101```
102
103Countries
104---------
105```php
106use CommerceGuys\Intl\Country\CountryRepository;
107
108// Reads the country definitions from resources/country.
109$countryRepository = new CountryRepository;
110
111// Get the US country using the default locale (en).
112$country = $countryRepository->get('US');
113echo $country->getCountryCode(); // US
114echo $country->getName(); // United States
115echo $country->getCurrencyCode(); // USD
116
117// Get the US country using the fr-FR locale.
118$country = $countryRepository->get('US', 'fr-FR');
119echo $country->getName(); // États-Unis
120
121$allCountries = $countryRepository->getAll();
122```
123
124Languages
125---------
126```php
127use CommerceGuys\Intl\Language\LanguageRepository;
128
129// Reads the language definitions from resources/language.
130$languageRepository = new LanguageRepository;
131
132// Get the german language using the default locale (en).
133$language = $languageRepository->get('de');
134echo $language->getLanguageCode(); // de
135echo $language->getName(); // German
136
137// Get the german language using the fr-FR locale.
138$language = $languageRepository->get('de', 'fr-FR');
139echo $language->getName(); // allemand
140
141$allLanguages = $languageRepository->getAll();
142```
143
144Implementing the library
145------------------------
146The base interfaces don't impose setters.
147Extended interfaces (with setters) are provided for (Doctrine, Drupal) entities.
148
149While the library can be used as-is, many applications will want to store parts of the dataset in a database.
150This allows for better performance while giving users the ability to modify and expand the data.
151Taking currencies as an example, a merchant frequently wants to be able to:
152
153- Define custom currencies.
154- Enable/disable existing currencies
155- Modify an existing currency (changing the default number of fraction digits, for example).
156
157This would be accomplished by using the CurrencyRepository to get all default currencies and
158insert them into the database. The doctrine entity (or any similar data object) would then implement
159the CurrencyEntityInterface so that the NumberFormatter can continue to work.
160
161Related projects
162----------------
163[commerceguys/pricing](http://github.com/commerceguys/pricing) provides a Price object.
164