2017 © Pedro Peláez
 

library intl

Internationalization library powered by CLDR data.

image

commerceguys/intl

Internationalization library powered by CLDR data.

  • Thursday, June 21, 2018
  • by bojanz
  • Repository
  • 14 Watchers
  • 290 Stars
  • 1,115,429 Installations
  • PHP
  • 13 Dependents
  • 0 Suggesters
  • 37 Forks
  • 4 Open issues
  • 12 Versions
  • 11 % Grown

The README.md

intl

Build Status, (*1)

A PHP 8.0+ internationalization library, powered by CLDR data., (*2)

Features: - NumberFormatter and CurrencyFormatter, inspired by intl. - Currencies - Languages, (*3)

Looking for a list of countries and subdivisions? Check out commerceguys/addressing., (*4)

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., (*5)

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., (*6)

Having access to the CLDR dataset also makes it possible to offer additional APIs, such as listing all currencies., (*7)

More backstory can be found in this blog post., (*8)

Formatting numbers

Allows formatting numbers (decimals, percents, currency amounts) using locale-specific rules., (*9)

Two formatters are provided for this purpose: NumberFormatter and CurrencyFormatter., (*10)

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();

Laravel integration, (*11)

The Versions

21/06 2018

dev-master

9999999-dev

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Bojan Zivanovic

21/06 2018

v1.0.1

1.0.1.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Bojan Zivanovic

28/05 2018

v1.0.0

1.0.0.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Bojan Zivanovic

29/12 2017

v0.7.5

0.7.5.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Bojan Zivanovic

13/12 2016

v0.7.4

0.7.4.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Bojan Zivanovic

26/10 2016

v0.7.3

0.7.3.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Bojan Zivanovic

03/10 2016

v0.7.2

0.7.2.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Bojan Zivanovic

07/04 2016

v0.7.1

0.7.1.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-bcmath *

 

The Development Requires

by Bojan Zivanovic

05/10 2015

v0.7

0.7.0.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-bcmath *

 

The Development Requires

by Bojan Zivanovic

17/06 2015

v0.6.1

0.6.1.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-bcmath *

 

The Development Requires

by Bojan Zivanovic

30/04 2015

v0.6

0.6.0.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-bcmath *

 

The Development Requires

by Bojan Zivanovic

23/02 2015

v0.5

0.5.0.0

Internationalization library powered by CLDR data.

  Sources   Download

MIT

The Requires

  • php >=5.4.0
  • ext-bcmath *

 

The Development Requires

by Bojan Zivanovic