Internationalisation and my PHP Development Infrastructure
By Tony Marston2005-07-17
My Implementation - Get Language Text
Individual pieces of translated text will be extracted from the relevant 'language_text.inc' or 'sys.language_text.inc' files using the following function:
function getLanguageText ($id, $arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
// get text from the language file and include up to 5 arguments.
{
static $array1;
static $array2;
if (!is_array($array1)) {
$array1 = array();
// include standard system text from current directory
$subdir = getLanguageSubDir ('./text');
$fname = "$subdir/sys.language_text.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_text.inc";
if (!file_exists($fname)) {
// filename does not exist
trigger_error(getLanguageText('sys0057', $fname), E_USER_ERROR);
} // if
$array2 = require_once $fname;
unset ($array);
// use this language code for the HTML output
$pos = strrpos($subdir, '/');
$GLOBALS['language'] = substr($subdir, $pos +1);
} // if
// perform lookup for specified $id ($array2 first, then $array1)
if (isset($array2[$id])) {
$string = $array2[$id];
} elseif (isset($array1[$id])) {
$string = $array1[$id];
} else {
// nothing found, so return original input
return $id;
} // if
$string = convertEncoding($string, 'latin1', 'UTF-8');
if (!is_null($arg1)) {
// insert argument(s) into string
$string = sprintf($string, $arg1, $arg2, $arg3, $arg4, $arg5);
} // if
return $string;
} // getLanguageText
Please note the following:
- The contents of 'language_text.inc' will be searched first for the relevant
$id. If it is not found then the contents of 'sys.language_text.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.
- The reason that I convert the input from 'latin1' to 'UTF-8' is that during testing I discovered that accented characters needed to be converted (such as 'é' into 'é') otherwise after passing through the XML file and XSL transformation they appeared corrupted in the HTML output.
- As well as the
$idup to 5 optional arguments can be supplied. This can be used for error messages which may need to include values which are only available at run time. - The language found for the applicatiion text will be used as the language for the resulting output. For example, the HTML output file will contain the following line:
<html xml:lang="??" lang="??">
This new function has been inserted into the following places:
- Inside
addParams2XMLdoc()to load script titles:$xsl_params['title'] = getLanguageText($task_id);
- Inside
setActBar()to load action buttons:$label = getLanguageText($label);
- Inside
setMenuBar()to load menu buttons:$button['button_text'] = getLanguageText($button['button_text']);
- Inside
setNavBar()to load navigation buttons:$button['button_text'] = getLanguageText($button['button_text']);
- Inside
setScreenStructure()to load field labels:$fieldlabel = getLanguageText($fieldlabel);
- In various places for all error messages, such as:
if (strlen($fieldvalue) > $size) {
// '$fieldname cannot be > $size characters
$this->errors[$fieldname] = getLanguageText('sys0021', $fieldname, $size);
} // if
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
