Skip to content

Commit

Permalink
bfojcapell: better i18n support and more flexibility allowed in templ…
Browse files Browse the repository at this point in the history
…ates. 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
  • Loading branch information
chillu committed Sep 15, 2007
1 parent f2dc279 commit 408c8d6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion _config.php
Expand Up @@ -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/');

Expand Down
7 changes: 7 additions & 0 deletions core/Core.php
Expand Up @@ -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
*
Expand Down
3 changes: 3 additions & 0 deletions core/ManifestBuilder.php
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions core/SSViewer.php
Expand Up @@ -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);
Expand Down Expand Up @@ -186,7 +186,7 @@ public function process($item) {

$itemStack = array();
$val = "";

include($cacheFile);

$output = $val;
Expand All @@ -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(
Expand Down Expand Up @@ -282,7 +282,8 @@ static function parseTemplateContent($content) {
$content = ereg_replace('<' . '% +end_if +%' . '>', '<? } ?>', $content);

// i18n
$content = ereg_replace('<' . '% +_t\((([^)]|\)[^;])*)\); +%' . '>', '<?= _t(\\1) ?>', $content);
ereg('.*[\/](.*)',$template,$path);
$content = ereg_replace('<' . '% +_t\((\'([^\']*)\'|"([^"]*)")(([^)]|\)[^;])*)\); +%' . '>', '<?= _t(\''. $path[1] . '.\\2\\3\'\\4) ?>', $content);

// </base> isnt valid html? !?
$content = ereg_replace('<' . '% +base_tag +%' . '>', '<base href="<?= Director::absoluteBaseURL(); ?>" />', $content);
Expand Down
14 changes: 4 additions & 10 deletions core/i18n.php
Expand Up @@ -6,13 +6,6 @@
* @author Bernat Foj Capell <bernat@silverstripe.com>
*/

/**
* 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 {

/**
Expand Down Expand Up @@ -118,6 +111,7 @@ private static function report_calls_code($file) {

if (isset($callMap[$class.'--'.$entity])) echo "Warning! Redeclaring entity $entity in file $file<br>";

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);
Expand Down Expand Up @@ -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<br>";

Expand All @@ -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);

Expand Down

0 comments on commit 408c8d6

Please sign in to comment.