pecl/ecma_intl
pecl/ecma_intl is a PHP implementation of the ECMAScript Internationalization API Specification (ECMA-402).
But, wait!
PHP isn’t JavaScript, silly! 🤪
That’s right!
There are a few differences from ECMA-402, but for the most part, things work as one might expect. This means you can read the Intl documentation on MDN and translate it to PHP with a few small changes!
Here are a few of the key differences:
- Namespaces:
This implementation uses PHP namespaces. The top-level namespace is
Ecma, and most everything is under theEcma\Intlnamespace. This means thatIntl.Localein JavaScript becomesEcma\Intl\Localein PHP.- Accessor syntax:
Where JavaScript uses dot notation (
.) for property and method access, PHP uses “arrows” made up of the hyphen and greater-than characters (->); for static methods, PHP uses double colon (::). This meansIntl.getCanonicalLocales()in JavaScript becomesEcma\Intl::getCanonicalLocales()in PHP, andlocale.baseNamein JavaScript becomes$locale->baseNamein PHP.- Object model:
JavaScript is a prototype-based language, while PHP is a class-based language. As a result, objects in this implementation have some nuanced differences that diverge from the ECMA-402 standard.
Additionally, JavaScript has generic objects (i.e.,
{}) it passes around like hashes. The closest equivalent in PHP is an associative array, but we chose to use stronger typing in this implementation. As a result, many of the object hashes defined in ECMA-402 (e.g., the options argument passed to theIntl.Localeconstructor) are objects with typed properties in PHP (e.g.,Ecma\Intl\Locale\Options).