PHP Internationalization and Localization Localizing Included Files - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Internationalization and Localization Localizing Included Files - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, June 29, 2019

PHP Internationalization and Localization Localizing Included Files

PHP Internationalization and Localization


Localizing Included Files

Problem

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

Solution

Example  Modifying include_path for localization

       $base = '/usr/local/php-include';
       $locale = 'en_US';

       $include_path = ini_get('include_path');
       ini_set('include_path',"$base/$locale:$base/global:$include_path");

Discussion

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


No comments:

Post a Comment

Post Top Ad