PHP Internationalization and Localization Localizing Text Messages - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Internationalization and Localization Localizing Text Messages - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Wednesday, June 26, 2019

PHP Internationalization and Localization Localizing Text Messages

PHP Internationalization and Localization


Localizing Text Messages

Problem

You want to display text messages in a locale-appropriate language.

Solution

Maintain a message catalog of words and phrases and retrieve the appropriate string from the message catalog before passing it to a MessageFormatter object to format it for printing:

       $messages = array();
       $messages['en_US'] =
                 array('FAVORITE_FOODS' => 'My favorite food is {0}.',
                            'FRIES' => 'french fries',
                            'CANDY' => 'candy',
                            'CHIPS' => 'potato chips',
                            'EGGPLANT' => 'eggplant');
       $messages['en_GB'] =
                 array('FAVORITE_FOODS' => 'My favourite food is {0}.',
                            'FRIES' => 'chips',
                            'CANDY' => 'sweets',
                            'CHIPS' => 'crisps',
                            'EGGPLANT' => 'aubergine');

       foreach (array('en_US', 'en_GB') as $locale) {
             $candy = new MessageFormatter($locale, $messages[$locale]['CANDY']);
             $favs = new MessageFormatter($locale, $messages[$locale]['FAVORITE_FOODS']);
             print $favs->format(array($candy->format(array()))) . "\n";
       }

This prints:

       My favorite food is candy.
       My favourite food is sweets.

Discussion

The first argument to the MessageFormatter constructor is the locale for which the message should be formatted. The second argument is the message pattern. The power of MessageFormatter comes from the special bits in the pattern delimited by curly braces. This is where the arguments supplied to the format() method get inserted into  the pattern. In the code in the Solution, the {0} in the pattern is replaced by the first element in the array passed to format(). A {1} in a pattern would be replaced by the second element of the array, and so on.

The easiest way to specify pattern arguments is with numbers—{0} is the first argument, {1} the second, and so on. Then the value of the first element in the array passed to format() replaces the {0}, the second replaces the {1}, and so forth down the line. In the example above, there is one replacement in the FAVORITE_FOODS pattern and no replacements in the CANDY pattern, so the array passed to $favs->format() has one element, and the array passed to $candy->format() is empty.

Admittedly, a plain old formatting pattern argument such as {0} is not very exciting. Using all the machinery of ICU for simple string replacement is underwhelming. More complicated pattern arguments show how MessageFormatter shines. For example, consider a message where the text needs to be different based on how many objects are involved. For example, “You have one item in your shopping cart” versus “You have two items in your shopping cart.” Here’s how to express that with MessageFormatter:

       $messages = array();
       $messages['en_US'] =
              array('CART' => "You have {0,spellout} " .
                                             "{0, plural, " .
                                             " =1 {item} " .
                                             " other {items} } " .
                                             "in your shopping cart.");
       $messages['fr_FR'] =
              array('CART' => "Vous {0, plural, " .
                                             " =0 {n'avez pas d'articles} ".
                                             " =1 {avez un article} ".
                                             " other {avez {0,spellout} articles}} ".
                                             "dans votre panier.");
       $fmts = array();
       foreach (array_keys($messages) as $locale) {
              $fmts[$locale] = new MessageFormatter($locale, $messages[$locale]['CART']);
       }

       for ($i = 0; $i < 10; $i++) {
              foreach ($fmts as $locale => $obj) {
                     print $obj->format(array($i)) . "\n";
              }
       }

This prints:

      You have zero items in your shopping cart.
      Vous n'avez pas d'articles dans votre panier.
      You have one item in your shopping cart.
      Vous avez un article dans votre panier.
      You have two items in your shopping cart.

      Vous avez deux articles dans votre panier.
      You have three items in your shopping cart. 
      Vous avez trois articles dans votre panier.
      You have four items in your shopping cart.
      Vous avez quatre articles dans votre panier.
      You have five items in your shopping cart.
      Vous avez cinq articles dans votre panier.
      You have six items in your shopping cart.
      Vous avez six articles dans votre panier.
      You have seven items in your shopping cart.
      Vous avez sept articles dans votre panier.
      You have eight items in your shopping cart.
      Vous avez huit articles dans votre panier.
      You have nine items in your shopping cart.
      Vous avez neuf articles dans votre panier.

The pattern arguments in this example are more extensive than a simple {0}. The {0,spellout} argument says “use argument 0, but treat it as type spellout“. This is a built-in ICU type which turns numerals into their spelled-out equivalents. Because MessageFormatter is locale-aware, it knows what words to use. E.g., “three” in English but “trois” in French. The pattern also includes an argument of type plural. Reusing argument 0, this allows for wholesale different text based on the value of that argument. In English, it outputs item if the argument is 1, but items otherwise. The French construction distinguishes between 0, 1, and everything else to ensure proper grammar.

The plural argument type lets the message formatter make a choice based on the numerical value of an argument. The more general select argument type lets the message formatter make a choice based on arbitrary values. This is useful for choosing different words based on the gender of an argument. Here’s how that can work in English:

       $message = '{0, select, f {She} m {He} other {It}} went to the store.';

       $fmt = new MessageFormatter('en_US', $message);

       print $fmt->format(array('f')) .   "\n";
       print $fmt->format(array('m')) . "\n";
       print $fmt->format(array('Unknown')) . "\n";

This prints:

       She went to the store.
       He went to the store.
       It went to the store.

When argument 0 is f, “She” is interpolated into the output. When it’s m, then “He” goes into the output. Otherwise, “It” goes into the output.

In PHP 5.5, MessageFormatter supports not just numbered arguments, but named arguments, too. Just make sure that how you refer to the argument in the pattern matches the array key you use when calling format(). For example:

       $message = 'I like to eat {food} and {drink}.';
       $fmt = new MessageFormatter('en_US', $message);
       print $fmt->format(array('food' => 'eggs',
                                                       'drink' => 'water'));

This prints:

       I like to eat eggs and water.

If you’re using an older version of PHP, you can install version 3.0 (or later) of the intl extension from PECL to get this capability.


No comments:

Post a Comment

Post Top Ad