16.8. Localizing Included Files
Problem
You want to include locale-specific files in your pages.
Solution
Dynamically modify the include_path once
you’ve determined the appropriate locale:
$base = '/usr/local/php-include';
$LANG = 'en_US';
$include_path = ini_get('include_path');
ini_set('include_path',"$base/$LANG:$base/global:$include_path");Discussion
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 Section 16.8. 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.
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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access