From 408c8d63d5ff2ce3b5b1b71785bab9f0fb090807 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sat, 15 Sep 2007 20:06:42 +0000 Subject: [PATCH] bfojcapell: better i18n support and more flexibility allowed in templates. Also fixed some possible warnings in 2.02rc1 if more than one language file is found with the same name. ManifestBuilder? now skips language files - they cannot be autoloaded and they are already loaded on demand, so no need to be in the manifest (merged from branches/gsoc) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41913 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- _config.php | 2 +- core/Core.php | 7 +++++++ core/ManifestBuilder.php | 3 +++ core/SSViewer.php | 9 +++++---- core/i18n.php | 14 ++++---------- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/_config.php b/_config.php index c8e8e672141..fcaf95fd883 100644 --- a/_config.php +++ b/_config.php @@ -62,7 +62,7 @@ /** * Define a default language different than english */ -//LocaleAPI::set_locale('ca_AD'); +//i18n::set_locale('ca_AD'); define('MCE_ROOT', 'jsparty/tiny_mce2/'); diff --git a/core/Core.php b/core/Core.php index 32f97836a1d..f0092119c51 100755 --- a/core/Core.php +++ b/core/Core.php @@ -83,6 +83,13 @@ function stripslashes_recursively(&$array) { } } +/** + * Priorities definition. These constants are used in calls to _t() as an optional argument + */ +define('PR_HIGH',100); +define('PR_MEDIUM',50); +define('PR_LOW',10); + /** * This is the main translator function. Returns the string defined by $class and $entity according to the currently set locale * diff --git a/core/ManifestBuilder.php b/core/ManifestBuilder.php index 5f91bdd3ec3..1f1e84cf485 100644 --- a/core/ManifestBuilder.php +++ b/core/ManifestBuilder.php @@ -184,6 +184,9 @@ private static function getClassManifest($folder, &$classMap) { // ignore certain directories if(is_dir("$folder/$item") && in_array($item, self::$ignore_folders)) continue; + + // i18n: ignore language files (loaded on demand) + if($item == 'lang' && is_dir("$folder/$item") && ereg_replace("/[^/]+/\\.\\.","",$folder.'/..') == Director::baseFolder()) continue; if(is_dir("$folder/$item")) { // recurse into directories (if not in $ignore_folders) diff --git a/core/SSViewer.php b/core/SSViewer.php index 44355236d15..9f02841e3cb 100644 --- a/core/SSViewer.php +++ b/core/SSViewer.php @@ -154,7 +154,7 @@ public function process($item) { if(isset($_GET['debug_profile'])) Profiler::mark("SSViewer::process - compile", " for $template"); $content = file_get_contents($template); - $content = SSViewer::parseTemplateContent($content); + $content = SSViewer::parseTemplateContent($content, $template); $fh = fopen($cacheFile,'w'); fwrite($fh, $content); @@ -186,7 +186,7 @@ public function process($item) { $itemStack = array(); $val = ""; - + include($cacheFile); $output = $val; @@ -199,7 +199,7 @@ public function process($item) { return $output; } - static function parseTemplateContent($content) { + static function parseTemplateContent($content, $template="") { while(true) { $oldContent = $content; $content = preg_replace_callback('/<' . '% include +([A-Za-z0-9_]+) +%' . '>/', create_function( @@ -282,7 +282,8 @@ static function parseTemplateContent($content) { $content = ereg_replace('<' . '% +end_if +%' . '>', '', $content); // i18n - $content = ereg_replace('<' . '% +_t\((([^)]|\)[^;])*)\); +%' . '>', '', $content); + ereg('.*[\/](.*)',$template,$path); + $content = ereg_replace('<' . '% +_t\((\'([^\']*)\'|"([^"]*)")(([^)]|\)[^;])*)\); +%' . '>', '', $content); // isnt valid html? !? $content = ereg_replace('<' . '% +base_tag +%' . '>', '', $content); diff --git a/core/i18n.php b/core/i18n.php index b3e1aa74f9f..329a4829800 100755 --- a/core/i18n.php +++ b/core/i18n.php @@ -6,13 +6,6 @@ * @author Bernat Foj Capell */ -/** - * Priorities definition. These constants are used in calls to _() as an optional argument - */ -define('PR_HIGH',100); -define('PR_MEDIUM',50); -define('PR_LOW',10); - class i18n extends Controller { /** @@ -118,6 +111,7 @@ private static function report_calls_code($file) { if (isset($callMap[$class.'--'.$entity])) echo "Warning! Redeclaring entity $entity in file $file
"; + if (substr($regs[2],0,1) == '"') $regs[2] = addcslashes($regs[2],'\''); $mst .= '$lang[\'en_US\'][\'' . $class . '\'][\'' . $entity . '\'] = '; if ($regs[5]) { $mst .= "array(\n\t'" . substr($regs[2],1,-1) . "',\n\t" . substr($regs[5],1); @@ -148,11 +142,11 @@ private static function report_calls_tpl($index, $file) { static $callMap; $content = file_get_contents($file); $mst = ''; - while (ereg('_t[[:space:]]*\([[:space:]]*("[^,]*"|\\\'[^,]*\\\')[[:space:]]*,[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\')([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*\)',$content,$regs)) { + while (ereg('_t[[:space:]]*\([[:space:]]*("[^"]*"|\\\'[^\']*\\\')[[:space:]]*,[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\')([[:space:]]*,[[:space:]]*[^,)]*)?([[:space:]]*,[[:space:]]*("([^"]|\\\")*"|\'([^\']|\\\\\')*\'))?[[:space:]]*\)',$content,$regs)) { $entityParts = explode('.',substr($regs[1],1,-1)); $entity = array_pop($entityParts); - + // Entity redeclaration check if (isset($callMap[$index.'--'.$entity])) echo "Warning! Redeclaring entity $entity in file $file
"; @@ -165,7 +159,7 @@ private static function report_calls_tpl($index, $file) { $mst .= ",\n\t'" . substr($regs[6],2,-1) . '\''; } $mst .= "\n);"; - } else $mst .= '\'' . substr($regs[2],2,-1) . '\';'; + } else $mst .= '\'' . substr($regs[2],1,-1) . '\';'; $mst .= "\n"; $content = str_replace($regs[0],"",$content);