From 12f245767721483403246699495e4566289e66a1 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 17 Oct 2008 17:44:14 +0000 Subject: [PATCH] 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 --- core/i18nTextCollector.php | 26 +++++++--- tests/i18n/_fakewebroot/_manifest_exclude | 0 .../_fakewebroot/i18nothermodule/_config.php | 0 .../i18nothermodule/code/i18nOtherModule.php | 12 +++++ .../templates/i18nOtherModule.ss | 1 + .../templates/i18nTestModule.ss | 3 +- tests/i18nTextCollectorTest.php | 50 +++++++++++++------ 7 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 tests/i18n/_fakewebroot/_manifest_exclude create mode 100644 tests/i18n/_fakewebroot/i18nothermodule/_config.php create mode 100644 tests/i18n/_fakewebroot/i18nothermodule/code/i18nOtherModule.php create mode 100644 tests/i18n/_fakewebroot/i18nothermodule/templates/i18nOtherModule.ss diff --git a/core/i18nTextCollector.php b/core/i18nTextCollector.php index a7988c016..203cefcb7 100644 --- a/core/i18nTextCollector.php +++ b/core/i18nTextCollector.php @@ -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; } diff --git a/tests/i18n/_fakewebroot/_manifest_exclude b/tests/i18n/_fakewebroot/_manifest_exclude new file mode 100644 index 000000000..e69de29bb diff --git a/tests/i18n/_fakewebroot/i18nothermodule/_config.php b/tests/i18n/_fakewebroot/i18nothermodule/_config.php new file mode 100644 index 000000000..e69de29bb diff --git a/tests/i18n/_fakewebroot/i18nothermodule/code/i18nOtherModule.php b/tests/i18n/_fakewebroot/i18nothermodule/code/i18nOtherModule.php new file mode 100644 index 000000000..7c3eb7018 --- /dev/null +++ b/tests/i18n/_fakewebroot/i18nothermodule/code/i18nOtherModule.php @@ -0,0 +1,12 @@ + \ No newline at end of file diff --git a/tests/i18n/_fakewebroot/i18nothermodule/templates/i18nOtherModule.ss b/tests/i18n/_fakewebroot/i18nothermodule/templates/i18nOtherModule.ss new file mode 100644 index 000000000..adb46f302 --- /dev/null +++ b/tests/i18n/_fakewebroot/i18nothermodule/templates/i18nOtherModule.ss @@ -0,0 +1 @@ +<% _t('i18nOtherModule.MAINTEMPLATE',"Main Template Other Module")%> \ No newline at end of file diff --git a/tests/i18n/_fakewebroot/i18ntestmodule/templates/i18nTestModule.ss b/tests/i18n/_fakewebroot/i18ntestmodule/templates/i18nTestModule.ss index a40ac91d7..bf2fbc702 100644 --- a/tests/i18n/_fakewebroot/i18ntestmodule/templates/i18nTestModule.ss +++ b/tests/i18n/_fakewebroot/i18ntestmodule/templates/i18nTestModule.ss @@ -1 +1,2 @@ -<% _t('i18nTestModule.MAINTEMPLATE',"Main Template")%> \ No newline at end of file +<% _t('i18nTestModule.MAINTEMPLATE',"Main Template")%> +lonely _t() call that should be ignored \ No newline at end of file diff --git a/tests/i18nTextCollectorTest.php b/tests/i18nTextCollectorTest.php index 65e1d0f11..6a7587dfa 100644 --- a/tests/i18nTextCollectorTest.php +++ b/tests/i18nTextCollectorTest.php @@ -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 = << -<% 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;