<?php
/*
I18N.class.php - String Translation class for PHP
Copyright (C) 2005 Alan H. Lake
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Current version: 1.0.2 (September 18, 2005)
*/
class I18N_class {
function XL ($identifier, $destination, $locale) {
// Return the translated string specified by $identifier as
// translated for $locale
// Global variables
global $LocalePath;
global $LocalePhpFileExtension;
global $LocaleMechanism;
$oLang = $_SESSION['oLang'];
if (empty ($LocalePhpFileExtension))
$LocalePhpFileExtension = "php";
// Die if one of the required variables has not been set
if($LocaleMechanism != 'onefile' and
$LocaleMechanism != 'severalfiles' and
$LocaleMechanism != 'dbm' and
$LocaleMechanism != 'SQL')
die ("I18N:XL ($identifier,$destination,$locale): The Locale Mechanism is ".'"'.$LocaleMechanism.'". It must be "onefile", "severalfiles", "dbm" or "SQL".');
if (empty ($LocalePath) and $LocaleMechanism != "SQL")
die ("I18N:XL ($identifier,$destination,$locale): The Locale Path must be supplied.");
if (empty ($locale))
die ("I18N:XL ($identifier,$destination,$locale): The Locale (3rd parameter) must be supplied.");
if (empty ($identifier))
die ("I18N:XL ($identifier,$destination,$locale): The Identifier (1st parameter) must be supplied.");
if (empty ($destination))
die ("I18N:XL ($identifier,$destination,$locale): The Destination (2nd parameter) must be supplied.");
// Using the specified storage mechanism, access the translated
// variables list and return the requested value
if ($LocaleMechanism == "onefile") {
$localefile = $LocalePath . $locale . "." . $LocalePhpFileExtension;
return $this->LookupFromFile ($identifier, $destination, $localefile);
} elseif ($LocaleMechanism == "severalfiles") {
$firstchar = strtolower (substr ($identifier, 0, 1));
$localefile = $LocalePath . $locale . "." . $firstchar . "." .
$LocalePhpFileExtension;
return $this->LookupFromFile ($identifier, $destination, $localefile);
} elseif ($LocaleMechanism == "dbm") {
$localefile = $LocalePath . $locale . ".dbm";
return $this->LookupFromDBM ($identifier, $destination, $localefile);
} elseif ($LocaleMechanism == "SQL") {
return $this->LookupFromSQL($identifier, $destination);
}
}
function Get_variable_list ($string) {
if (empty ($string))
return;
// Match on all variables in $string
$count = preg_match_all (
"/\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/",
$string, $matches);
// Return array of matches ($matches[0] is most useful)
return $matches;
}
function LookupFromFile ($identifier, $destination, $localefile) {
global $locale;
if (!file_exists ($localefile))
die ("LookupFromFile: Specified translation file ($localefile) could not be opened.");
include ($localefile);
if (!isset ($string[$identifier])){
die ("LookupFromFile: Requested identifier, $identifier, does not exist in the translation file $localefile.");
}
$value = $string[$identifier];
$variables = $this->Get_variable_list ($value);
if (empty ($variables[0]))
return "$destination = $value";
else {
while (list ($num, $variable) = each ($variables[0])) {
$variablecheck = "if (!isset ($variable))\n" .
"die (\"LookupFromFile: $identifier references a variable that is not set.\");\n";
}
return "$variablecheck $destination = $value";
}
}
function LookupFromDBM ($identifier, $destination, $localefile) {
$dbi = dbmopen ($localefile, "r");
$value = stripslashes (unserialize (urldecode (
(dbmfetch ($dbi, $identifier)))));
dbmclose ($dbi);
if (empty ($value)) {
return ("die (\"LookupFromDBM: Requested identifier does not exist in the \" .
\"translation file for specified locale. \" .
\"($identifier)\");");
}
$variables = $this->Get_variable_list ($value);
if (empty ($variables[0]))
return "$destination = $value";
else {
while (list ($num, $variable) = each ($variables[0])) {
$variablecheck = "if (!isset ($variable))\n" .
"die (\"LookupFromDBM: $identifier references a variable that is not set.\");\n";
}
return "$variablecheck $destination = $value";
}
}
function LookupFromSQL ($identifier, $destination) {
global $locale;
global $oLang;
$value = $oLang->GetValue($locale,$identifier);
if (empty($value))
die ("LookupFromSQL: Requested identifier, $identifier, does not exist in the database.");
$variables = $this->Get_variable_list ($value);
if (empty ($variables[0]))
return "$destination = $value";
else {
while (list ($num, $variable) = each ($variables[0])) {
$variablecheck = "if (!isset ($variable))\n" .
"die (\"LookupFromSQL: $identifier references a variable that is not set.\");\n";
}
return "$variablecheck $destination = $value";
}
}
function GetLocales() {
global $LocalePath;
global $LocalePhpFileExtension;
global $LocaleMechanism;
$lang = array();
if (empty ($LocalePhpFileExtension))
$LocalePhpFileExtension = "php";
$ext = '.'.$LocalePhpFileExtension;
if($LocaleMechanism == 'onefile' or $LocaleMechanism == 'severalfiles' or $LocaleMechanism == 'dbm'){
$d = dir($LocalePath);
while(false !== ($entry = $d->read()))
if($entry != '.' and $entry != '..'){
$pos = strpos($entry,$ext);
if($pos > 0){
$count = strlen($entry)-strlen($ext);
$entry = substr($entry,0,$count);
$pos = strpos($entry,'.');
if($pos > 0)
$entry = substr($entry,0,$pos);
$oRfc1766 = $_SESSION['oRfc1766'];
if(!isset($oRfc1766))
die('$oRfc1766 is not set.');
else{
if($oRfc1766->is_valid_tag($entry)){
if(!array_key_exists($entry,$lang)){
$name = $oRfc1766->get_name($entry);
$lang["$entry"] = $name;
}
}else
die($entry." is not a valid language tag.");
}
}
}
$d->close();
}else{ //if $LocaleMechanism == 'SQL'
$oLang = $_SESSION['oLang'];
$lang = $oLang->GetLocales();
}
return($lang);
}
}
?>