InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Localizing Text Messages in PHP
PHP

Localizing Text Messages in PHP

InfinityCoder December 21, 2016

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

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$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:

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

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:

1
2
3
4
5
6
7
$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:

1
2
3
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:

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

This prints:

1
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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Building a Query String in PHP
You need to construct a link that includes name/value pairs …

Building a Query String in PHP

Using Non-Gregorian Calendars in PHP
You want to use a non-Gregorian calendar, such as a …

Using Non-Gregorian Calendars in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap