Recipe 19.9. Managing Localization Resources


19.9.1. Problem

You need to keep track of your various message catalogs and images.

19.9.2. Solution

Two techniques simplify the management of your localization resources. The first is making the new language of an object'for example, Canadian English'extend from a similar existing language, such as American English. You only have to change the words and phrases in the new object that differ from the original language.

The second technique: to track what phrases still need to be translated in new languages, put stubs in the new language object that have the same value as in your base language. By finding which values are the same in the base language and the new language, you can then generate a list of words and phrases to translate.

19.9.3. Discussion

The catalog-compare.php program shown in Example 19-22 prints out messages that are the same in two catalogs, as well as messages that are missing from one catalog but present in another.

catalog-compare.php

<?php if (! (isset($_SERVER['argv'][1]) && isset($_SERVER['argv'][2]))) {     die("Specify two locales to compare."); } $base = 'pc_MC_'.$_SERVER['argv'][1]; $other  = 'pc_MC_'.$_SERVER['argv'][2]; require_once 'pc_MC_Base.php'; require_once "$base.php"; require_once "$other.php"; $base_obj = new $base; $other_obj = new $other; /* Check for messages in the other class that  * are the same as the base class or are in  * the base class but missing from the other class */ foreach ($base_obj->messages as $k => $v) {     if (isset($other_obj->messages[$k])) {         if ($v == $other_obj->messages[$k]) {             print "SAME: $k\n";         }     } else {         print "MISSING: $k\n";     } } /* Check for messages in the other class but missing  * from the base class */ foreach ($other_obj->messages as $k => $v) {     if (! isset($base_obj->messages[$k])) {         print "MISSING (BASE): $k\n";     } }

To use this program, put each message catalog object in a file with the same name as the object (e.g., the pc_MC_en_US class should be in a file named pc_MC_en_US.php, and the pc_MC_es_US class should be in a file named pc_MC_es_US.php). You then call the program with the two locale names as arguments on the command line:

% php catalog-compare.php en_US es_US

In a web context, it can be useful to use a different locale and message catalog on a per-request basis. The locale to use may come from the browser (in an Accept-Language header), or it may be explicitly set by the server (different virtual hosts may be set up to display the same content in different languages). If the same code needs to select a message catalog on a per-request basis, the message catalog class can be instantiated as in Example 19-23.

Instantiating message catalogs

<?php // $locale comes from headers or virtual host name $classname = "pc_MC_$locale"; require_once 'pc_MC_Base.php'; require_once $classname.'.php'; $MC = new $classname; ?>

19.9.4. See Also

19.4 for a discussion of message catalogs; Recipe 7.19 for information on finding the methods and properties of an object.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net