Recipe 19.8. Localizing Included Files


19.8.1. Problem

You want to include locale-specific files in your pages.

19.8.2. Solution

Modify include_path once you've determined the appropriate locale, as shown in Example 19-21.

Modifying include_path for localization

<?php $base = '/usr/local/php-include'; $LANG = 'en_US'; $include_path = ini_get('include_path'); ini_set('include_path',"$base/$LANG:$base/global:$include_path"); ?>

19.8.3. Discussion

In Example 19-21, the $base variable holds the name of the base directory for your included localized files. Files that are not locale-specific go in the global subdirectory of $base, and locale-specific files go in a subdirectory named after their locale (e.g., en_US). Prepending the locale-specific directory and then the global directory to the include path makes them the first two places PHP looks when you include a file. Putting the locale-specific directory first ensures that nonlocalized information is loaded only if localized information isn't available.

This technique is similar to what the img( ) function does in the Recipe 19.7. Here, however, you can take advantage of PHP's include_path feature to have the directory searching happen automatically. For maximum utility, reset include_path as early as possible in your code, preferably at the top of a file loaded via auto_prepend_file on every request.

19.8.4. See Also

Documentation on include_path at http://www.php.net/manual/en/configuration.directives.php#ini.include-path and auto_prepend_file at http://www.php.net/manual/en/configuration.directives.php#ini.auto-prepend-file.




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