Recipe 19.7. Localizing Images

19.7.1. Problem

You want to display images that have text in them and have that text in a locale-appropriate language.

19.7.2. Solution

Make an image directory for each locale you want to support, as well as a global image directory for images that have no locale-specific information in them. Create copies of each locale-specific image in the appropriate locale-specific directory. Make sure that the images have the same filename in the different directories. Instead of printing out image URLs directly, use a wrapper function similar to the msg( ) function in 19.4 that prints out locale-specific text.

19.7.3. Discussion

The img( ) wrapper function in Example 19-18 looks for a locale-specific version of an image first, then a global one. If neither are present, it prints a message to the error log.

Finding locale-specific images

<?php $image_base_path = '/usr/local/www/images'; $image_base_url  = '/images'; function img($f) {     global $LANG;     global $image_base_path;     global $image_base_url;     if (is_readable("$image_base_path/$LANG/$f")) {         return "$image_base_url/$LANG/$f";     } elseif (is_readable("$image_base_path/global/$f")) {         return "$image_base_url/global/$f";     } else {         error_log("l10n error: LANG: $lang, image: '$f'");     } }

The img( ) function needs to know both the path to the image file in the filesystem ($image_base_path) and the path to the image from the base URL of your site (/images). It uses the first to test if the file can be read and the second to construct an appropriate URL for the image.

A localized image must have the same filename in each localization directory. For example, an image that says "New!" on a yellow starburst should be called new.gif in both the images/en_US directory and the images/es_US directory, even though the file images/es_US/new.gif is a picture of a yellow starburst with "¡Nuevo!" on it.

Don't forget that the alt text you display in your image tags also needs to be localized. Example 19-19 prints a complete localized <img/> element.

A localized <img> element

<?php print '<img src="/books/3/131/1/html/2/' . img('cancel.png') . '" ' .       'alt="' . msg('Cancel') . '"/>'; ?>

If the localized versions of a particular image have varied dimensions, store image height and width in the message catalog as well. Example 19-20 prints a localized <img/> element with height and width attributes.

A localized <img/> element with height and width

<?php print '<img src="/books/3/131/1/html/2/' . img('cancel.png') . '" ' .       'alt="' . msg('Cancel') . '" ' .       'height="' . msg('img-cancel-height') . '" ' .       'width="' . msg('img-cancel-width') . '"/>'; ?>

The localized messages for img-cancel-height and img-cancel-width are not text strings, but integers that describe the dimensions of the cancel.png image in each locale.

19.7.4. See Also

19.4 discusses locale-specific message catalogs.

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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: