bfojcapell: LocaleAPI renamed to i18n

(merged from branches/gsoc)


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41829 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-15 00:16:11 +00:00
parent bd037abcdc
commit 46be50f0a2
2 changed files with 38 additions and 42 deletions

View File

@ -88,11 +88,11 @@ function stripslashes_recursively(&$array) {
*/
function _($class, $entity, $string="", $priority=PR_MEDIUM, $context="") {
global $lang;
$locale = LocaleAPI::getLocale();
$locale = i18n::getLocale();
$class = ereg_replace('.*([/\\]+)',"",$class);
if (substr($class,-4) == '.php') $class = substr($class,0,-4);
if (!$lang[$locale][$class]) LocaleAPI::includeByClass($class);
$transEntity = $lang[LocaleAPI::getLocale()][$class][$entity];
if (!$lang[$locale][$class]) i18n::includeByClass($class);
$transEntity = $lang[i18n::getLocale()][$class][$entity];
return (is_array($transEntity) ? $transEntity[0] : $transEntity);
}
?>

View File

@ -1,16 +1,12 @@
<?php
define('PR_HIGH', 10);
define('PR_MEDIUM', 5);
class LocaleAPI extends Controller {
class i18n extends Controller {
static $currentlocale = 'en';
static function textCollector() {
echo "Collecting text...<br /><br />";
//Calculate base directory
$baseDir = Director::baseFolder();
@ -18,26 +14,26 @@ class LocaleAPI extends Controller {
if (!isset($_GET['module'])) {
$topLevel = scandir($baseDir);
foreach($topLevel as $module) {
LocaleAPI::processModule($baseDir, $module);
i18n::processModule($baseDir, $module);
}
} else {
LocaleAPI::processModule($baseDir, $_GET['module']);
i18n::processModule($baseDir, $_GET['module']);
}
echo "Done!";
}
private static function processModule($baseDir, $module) {
private static function processModule($baseDir, $module) {
if(is_dir("$baseDir/$module") && !in_array($module, array('sapphire','jsparty','assets')) && substr($module,0,1) != '.') {
LocaleAPI::getFilesRec("$baseDir/$module/code", $fileList);
i18n::getFilesRec("$baseDir/$module/code", $fileList);
foreach($fileList as $index => $file) {
$mst .= LocaleAPI::reportCallsCode($index, $file);
$mst .= i18n::reportCallsCode($index, $file);
}
$fileList = NULL;
LocaleAPI::getFilesRec("$baseDir/$module/templates", $fileList);
i18n::getFilesRec("$baseDir/$module/templates", $fileList);
foreach($fileList as $index => $file) {
$mst .= LocaleAPI::reportCallsTpl($index, $file);
$mst .= i18n::reportCallsTpl($index, $file);
}
if ($mst) {
// Create folder for lang files
@ -45,13 +41,13 @@ class LocaleAPI extends Controller {
if(!file_exists($baseDir. '/' . $module . '/lang')) {
mkdir($langFolder);
}
// Open the English file and write the Master String Table
if($fh = fopen($langFolder . '/en.php', "w")) {
fwrite($fh, "<?php\n\nglobal \$lang;\n\n" . $mst . "\n?>");
fwrite($fh, "<?php\n\nglobal \$lang;\n\n" . $mst . "\n?>");
fclose($fh);
echo "Created file: $langFolder/en.php<br />";
} else {
die("Cannot write language file! Please check permissions of $langFolder/en.php");
}
@ -65,12 +61,12 @@ class LocaleAPI extends Controller {
if(substr($item,0,1) == '.') continue;
if(substr($item,-4) == '.php') $fileList[substr($item,0,-4)] = "$folder/$item";
else if(substr($item,-3) == '.ss') $fileList[$item] = "$folder/$item";
else if(is_dir("$folder/$item")) LocaleAPI::getFilesRec("$folder/$item", $fileList);
else if(is_dir("$folder/$item")) i18n::getFilesRec("$folder/$item", $fileList);
}
}
/**
* Look for calls to the underscore function and build our MST
* Look for calls to the underscore function and build our MST
*/
private static function reportCallsCode($index, $file) {
static $callMap;
@ -79,13 +75,13 @@ class LocaleAPI extends Controller {
$class = ($regs[1] == '__FILE__' ? $index : $regs[1]);
$entity = substr($regs[2],2,-1);
if ($callMap[$class.'--'.$entity]) echo "Warning! Redeclaring entity $entity in file $file<br>";
$mst .= '$lang[\'en\'][\'' . $class . '\'][\'' . substr($regs[2],2,-1) . '\'] = ';
if ($regs[4]) {
$mst .= "array(\n\t'" . substr($regs[3],2,-1) . "',\n\t" . substr($regs[4],1);
if ($regs[5]) $mst .= ",\n\t'" . substr($regs[5],2,-1) . '\'';
if ($regs[5]) $mst .= ",\n\t'" . substr($regs[5],2,-1) . '\'';
$mst .= "\n);";
} else $mst .= '\'' . substr($regs[3],2,-1) . '\';';
$mst .= "\n";
@ -93,12 +89,12 @@ class LocaleAPI extends Controller {
$callMap[$class.'--'.$entity] = $regs[3];
}
return $mst;
}
/**
* Look for calls to the underscore function and build our MST
* Look for calls to the underscore function and build our MST
* Template version - no "class" argument
*/
private static function reportCallsTpl($index, $file) {
@ -107,13 +103,13 @@ class LocaleAPI extends Controller {
while (ereg('_\(([^$][^,"\']*|"[^,]*"|\'[^,]*\')(,[^$][^,)]*)(,[^,)]*)?(,[^)]*)?\)',$content,$regs)) {
$entity = substr($regs[1],2,-1);
if ($callMap[$index.'--'.$entity]) echo "Warning! Redeclaring entity $entity in file $file<br>";
$mst .= '$lang[\'en\'][\'' . $index . '\'][\'' . substr($regs[1],1,-1) . '\'] = ';
if ($regs[3]) {
$mst .= "array(\n\t'" . substr($regs[2],2,-1) . "',\n\t" . substr($regs[3],1);
if ($regs[4]) $mst .= ",\n\t'" . substr($regs[4],2,-1) . '\'';
if ($regs[4]) $mst .= ",\n\t'" . substr($regs[4],2,-1) . '\'';
$mst .= "\n);";
} else $mst .= '\'' . substr($regs[2],2,-1) . '\';';
$mst .= "\n";
@ -121,29 +117,29 @@ class LocaleAPI extends Controller {
$callMap[$index.'--'.$entity] = $regs[3];
}
return $mst;
}
static function setLocale($locale) {
if ($locale) LocaleAPI::$currentlocale = $locale;
if ($locale) i18n::$currentlocale = $locale;
}
static function getLocale() {
return LocaleAPI::$currentlocale;
return i18n::$currentlocale;
}
/**
* Includes all available language files for a certain defined locale
*/
static function includeByLocale($locale) {
$topLevel = scandir(Director::baseFolder());
foreach($topLevel as $module) {
if (file_exists($file = Director::getAbsFile("$module/lang/$locale.php"))) {
if (file_exists($file = Director::getAbsFile("$module/lang/$locale.php"))) {
include_once($file);
}
}
}
/**
* Given a class name (a "locale namespace"), will search for its module and, if available,
* will load the resources for the currently defined locale.
@ -160,11 +156,11 @@ class LocaleAPI extends Controller {
$path = $_CLASS_MANIFEST[$class];
ereg('.*/([^/]+)/code/',$path,$module);
}//die($class);
if (file_exists($file = Director::getAbsFile("{$module[1]}/lang/". LocaleAPI::getLocale() . '.php'))) {
if (file_exists($file = Director::getAbsFile("{$module[1]}/lang/". i18n::getLocale() . '.php'))) {
include_once($file);
} else if (LocaleAPI::getLocale() != 'en') {
LocaleAPI::setLocale('en');
LocaleAPI::includeByClass($class);
} else if (i18n::getLocale() != 'en') {
i18n::setLocale('en');
i18n::includeByClass($class);
} else {
user_error("Locale file $file should exist", E_USER_WARNING);
}