Localization Demystified: Php-Intl for Everyone

Globe stock illustration

Most applications perform locale aware operations like working with texts, dates, timezones, etc. The PHP Intl extension provides a good API for accessing the widely known ICU library’s functions.

[author_more]

Installation

The extension is installed by default on PHP 5.3 and above. You can look for it by running the following command:

php -m | grep 'intl'

If the extension is not present, you can install it manually by following the installation guide. If you’re using Ubuntu, you can directly run the following commands.

sudo apt-get update
sudo apt-get install php5-intl

If you’re using PHP7 on your machine, you need to add the (ppa:ondrej/php) PPA, update your system and install the Intl extension.

# Add PPA
sudo add-apt-repository ppa:ondrej/php-7.0
# Update repository index
sudo apt-get update
# install extension
sudo apt-get install php7.0-intl

Message Formatting

Most modern applications are built with localization in mind. Sometimes, the message is a plain string with variable placeholders, other times it’s a complex pluralized string.

Simple Messages

We’re going to start with a simple message containing a placeholder. Placeholders are patterns enclosed in curly braces. Here is an example:

var_dump(
    MessageFormatter::formatMessage(
        "en_US",
        "I have {0, number, integer} apples.",
        [ 3 ]
    )
);
// output

string(16) "I have 3 apples."

Continue reading %Localization Demystified: Php-Intl for Everyone%


Source: Sitepoint