Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Internationalisation and my PHP Development Infrastructure

By Tony Marston
2005-07-17


My Implementation - Get Language Array

Individual arrays of translated text will be extracted from the relevant 'language_array.inc' or 'sys.language_array.inc' files using the following function:

function getLanguageArray ($id)

// get named array from the language file.
{
static $array1;
static $array2;

if (!is_array($array1)) {
$array1 = array();
// include standard system text from current subdirectory
$subdir = getLanguageSubDir ('./text');
$fname = "$subdir/sys.language_array.inc";
if (!file_exists($fname)) {
// filename does not exist
trigger_error(getLanguageText('sys0057', $fname), E_USER_ERROR);
} // if
$array1 = require_once $fname;
unset ($array);
} // if

if (!is_array($array2)) {
$array2 = array();
// include application text from current directory
$subdir = getLanguageSubDir ('./text');
$fname = "$subdir/language_array.inc";
if (!file_exists($fname)) {
// filename does not exist
trigger_error(getLanguageText('sys0057', $fname), E_USER_ERROR);
} // if
$array2 = require_once $fname;
unset ($array);
} // if

// perform lookup for specified $id ($array2 first, then $array1)
if (isset($array2[$id])) {
$result = $array2[$id];
} elseif (isset($array1[$id])) {
$result = $array1[$id];
} else {
// nothing found, so return original input as an array
$result = array($id => $id);
} // if

foreach ($result as $key => $value) {
$result[$key] = convertEncoding($value, 'latin1', 'UTF-8');
} // foreach

return $result;

} // getLanguageArray

Please note the following:

  • The contents of 'language_array.inc' will be searched first for the relevant $id. If it is not found then the contents of 'sys.language_array.inc' will be searched.
  • The files are only read in once per HTTP request, during which the contents are loaded into memory. All subsequent accesses will read from memory without reading in the disk file again.

This new function should be used to obtain any array where the values will be displayed to the user. For example, instead of:

$languages = array('en' => 'English',

'es' => 'Spanish',
'fr' => 'French');

you should use the following:

$languages =  getLanguageArray('languages');


Tutorial Pages:
» Introduction
» Possible Methods
» Design Decisions
» My Implementation - Directory Structure
» My Implementation - File Names
» My Implementation - Determine User Language
» My Implementation - Locate Language Subdirectory
» My Implementation - Load Screen Structure file
» My Implementation - Get Language Text
» My Implementation - Get Language Array
» My Implementation - Handling Dates
» My Implementation - Handling Numbers
» Conclusion
» References


 | Bookmark
Related Tutorials:
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources