BUGFIX Fixed distribution of textcollector files to modules (was collecting all entities into all modules before) - added unit tests

ENHANCEMENT Using ksort() in textcollector to get alphabetized language master files (=easier to compare and debug)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64494 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2008-10-17 17:44:14 +00:00
parent c4cdc9ce87
commit 12f2457677
7 changed files with 68 additions and 24 deletions

View File

@ -83,7 +83,7 @@ class i18nTextCollector extends Object {
// Search for calls in code files if these exists
if(is_dir("$this->basePath/$module/code")) {
$fileList = $this->getFilesRecursive("$this->basePath/$module/code");
} else if('sapphire' == $module) {
} else if($module == 'sapphire') {
// sapphire doesn't have the usual module structure, so we'll scan all subfolders
$fileList = $this->getFilesRecursive("$this->basePath/$module");
}
@ -92,7 +92,7 @@ class i18nTextCollector extends Object {
if(substr($filePath,-3) == 'php') {
$content = file_get_contents($filePath);
$entitiesArr = array_merge($entitiesArr,(array)$this->collectFromCode($content, $module));
//$entitiesArr = array_merge($entitiesArr, (array)$this->collectFromStatics($filePath, $module));
$entitiesArr = array_merge($entitiesArr, (array)$this->collectFromStatics($filePath, $module));
}
}
@ -108,7 +108,7 @@ class i18nTextCollector extends Object {
}
// sort for easier lookup and comparison with translated files
asort($entitiesArr);
ksort($entitiesArr);
return $entitiesArr;
}
@ -117,15 +117,15 @@ class i18nTextCollector extends Object {
* Write the master string table of every processed module
*/
protected function writeMasterStringFile($entitiesByModule) {
$php = '';
// Write each module language file
if($entitiesByModule) foreach($entitiesByModule as $module => $entities) {
$php = '';
// Create folder for lang files
$langFolder = $this->baseSavePath . '/' . $module . '/lang';
if(!file_exists($this->baseSavePath. '/' . $module . '/lang')) {
if(!file_exists($langFolder)) {
Filesystem::makeFolder($langFolder, Filesystem::$folder_create_mask);
touch($this->baseSavePath. '/' . $module . '/lang/_manifest_exclude');
touch($langFolder . '/_manifest_exclude');
}
// Open the English file and write the Master String Table
@ -161,7 +161,11 @@ class i18nTextCollector extends Object {
protected function getFilesRecursive($folder, &$fileList = null) {
if(!$fileList) $fileList = array();
$items = scandir($folder);
if($items) foreach($items as $item) {
$isValidFolder = (
!in_array('_manifest_exclude', $items)
&& !preg_match('/\/tests$/', $folder)
);
if($items && $isValidFolder) foreach($items as $item) {
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";
@ -186,6 +190,8 @@ class i18nTextCollector extends Object {
$content = str_replace($regs[0],"",$content);
}
ksort($entitiesArr);
return $entitiesArr;
}
@ -216,6 +222,8 @@ class i18nTextCollector extends Object {
$content = str_replace($regs[0],"",$content);
}
ksort($entitiesArr);
return $entitiesArr;
}
@ -311,6 +319,8 @@ class i18nTextCollector extends Object {
}
}
ksort($entitiesArr);
return $entitiesArr;
}

View File

@ -0,0 +1,12 @@
<?php
class i18nOtherModule extends Object {
function __construct() {
_t(
'i18nOtherModule.ENTITY',
'Other Module Entity',
);
parent::__construct();
}
}
?>

View File

@ -0,0 +1 @@
<% _t('i18nOtherModule.MAINTEMPLATE',"Main Template Other Module")%>

View File

@ -1 +1,2 @@
<% _t('i18nTestModule.MAINTEMPLATE',"Main Template")%>
<% _t('i18nTestModule.MAINTEMPLATE',"Main Template")%>
lonely _t() call that should be ignored

View File

@ -38,6 +38,10 @@ class i18nTextCollectorTest extends SapphireTest {
$_TEMPLATE_MANIFEST['i18nTestModuleInclude.ss'] = array(
'Includes' => Director::baseFolder() . $this->alternateBasePath . 'i18ntestmodule/templates/Includes/i18nTestModuleInclude.ss',
);
$_TEMPLATE_MANIFEST['i18nTestModule.ss'] = array(
'main' => Director::baseFolder() . $this->alternateBasePath . 'i18ntestmodule/templates/i18nTestModule.ss',
'Layout' => Director::baseFolder() . $this->alternateBasePath . 'i18ntestmodule/templates/Layout/i18nTestModule.ss',
);
}
function tearDown() {
@ -285,21 +289,14 @@ PHP;
function testCollectFromIncludedTemplates() {
$c = new i18nTextCollector();
$html = <<<SS
<% _t("MainTemplate.MAINVALUE", 'Main Value'); %>
<% if Bar %>
<% include i18nTextCollectorTest_Include %>
<% end_if %>
lonely _t() call that should be ignored
SS;
$templateFilePath = $this->alternateBasePath . 'i18ntestmodule/templates/Layout/i18nTestModule.ss';
$html = file_get_contents($templateFilePath);
$this->assertEquals(
$c->collectFromTemplate($html, 'mymodule', 'RandomNamespace'),
array(
'i18nTextCollectorTest_NestedInclude.ss.NESTEDINCLUDE' => array('Nested Include Value', null, null),
'Test.NESTEDINCLUDEWITHNAMESPACE' => array('Nested Include Value with namespace', null, null),
'i18nTextCollectorTest_Include.ss.INCLUDENONAMESPACE' => array('Include Value', null, null),
'Test.INCLUDEWITHNAMESPACE' => array('Include Value with namespace', null, null),
'MainTemplate.MAINVALUE' => array('Main Value', null, null),
'i18nTestModule.WITHNAMESPACE' => array('Include Entity with Namespace', null, null),
'i18nTestModuleInclude.ss.NONAMESPACE' => array('Include Entity without Namespace', null, null),
'i18nTestModule.LAYOUTTEMPLATE' => array('Layout Template', null, null),
)
);
}
@ -309,8 +306,9 @@ SS;
$c->basePath = $this->alternateBasePath;
$c->baseSavePath = $this->alternateBaseSavePath;
$c->run('i18ntestmodule');
$c->run();
// i18ntestmodule
$moduleLangFile = "{$this->alternateBaseSavePath}/i18ntestmodule/lang/" . $c->getDefaultLocale() . '.php';
$this->assertTrue(
file_exists($moduleLangFile),
@ -328,10 +326,32 @@ global \$lang;
PR_LOW,
'Comment for entity'
);
\$lang['en_US']['i18nTestModule']['WITHNAMESPACE'] = 'Include Entity with Namespace';
\$lang['en_US']['i18nTestModuleInclude.ss']['NONAMESPACE'] = 'Include Entity without Namespace';
\$lang['en_US']['i18nTestModule']['MAINTEMPLATE'] = 'Main Template';
\$lang['en_US']['i18nTestModule']['OTHERENTITY'] = 'Other Entity';
\$lang['en_US']['i18nTestModule']['WITHNAMESPACE'] = 'Include Entity with Namespace';
\$lang['en_US']['i18nTestModuleInclude.ss']['NONAMESPACE'] = 'Include Entity without Namespace';
?>
PHP;
$this->assertEquals(
file_get_contents($moduleLangFile),
$compareContent
);
// i18nothermodule
$moduleLangFile = "{$this->alternateBaseSavePath}/i18nothermodule/lang/" . $c->getDefaultLocale() . '.php';
$this->assertTrue(
file_exists($moduleLangFile),
'Master language file can be written to modules /lang folder'
);
$compareContent = <<<PHP
<?php
global \$lang;
\$lang['en_US']['i18nOtherModule']['ENTITY'] = 'Other Module Entity';
\$lang['en_US']['i18nOtherModule']['MAINTEMPLATE'] = 'Main Template Other Module';
?>
PHP;