public function testJavaScriptTranslationWriting() { $jsFilePath = tempnam('/tmp', self::TEMP_PO); $pr = new tao_helpers_translation_POFileReader(dirname(__FILE__) . self::RAW_PO); $pr->read(); $tf = $pr->getTranslationFile(); $tw = new tao_helpers_translation_JSFileWriter($jsFilePath, $tf); $tw->write(); $this->assertTrue(file_exists($jsFilePath)); unlink($jsFilePath); $jsFilePath = tempnam('/tmp', self::TEMP_PO); $pr->setFilePath(dirname(__FILE__) . self::ESCAPING_PO); $pr->read(); $tf = $pr->getTranslationFile(); $tw->setFilePath($jsFilePath); $tw->setTranslationFile($tf); $tw->write(); $this->assertTrue(file_exists($jsFilePath)); unlink($jsFilePath); }
/** * Implementation of the 'compile' action. * * @access public * @author Joel Bout, <*****@*****.**> * @return void */ public function actionCompile() { $extensionsToCreate = explode(',', $this->options['extension']); $extensionsToCreate = array_unique($extensionsToCreate); foreach ($extensionsToCreate as $extension) { $language = $this->options['language']; $compiledTranslationFile = new tao_helpers_translation_TranslationFile(); $compiledTranslationFile->setTargetLanguage($this->options['language']); $this->outVerbose("Compiling language '{$language}' for extension '{$extension}'..."); // Get the dependencies of the target extension. // @todo Deal with dependencies at compilation time. $dependencies = array(); if ($extension !== 'tao') { $dependencies[] = 'tao'; } $this->outVerbose("Resolving Dependencies..."); foreach ($dependencies as $depExtId) { $this->outVerbose("Adding messages from extension '{$depExtId}' in '{$language}'..."); // Does the locale exist for $depExtId? $depPath = $this->buildLanguagePath($depExtId, $language) . '/' . self::DEF_PO_FILENAME; if (!file_exists($depPath) || !is_readable($depPath)) { $this->outVerbose("Dependency on extension '{$depExtId}' in '{$language}' does not exist. Trying to resolve default language..."); $depPath = $this->buildLanguagePath($depExtId, tao_helpers_translation_Utils::getDefaultLanguage() . '/' . self::DEF_PO_FILENAME); if (!file_exists($depPath) || !is_readable($depPath)) { $this->outVerbose("Dependency on extension '{$depExtId}' in '{$language}' does not exist."); continue; } } // Recompile the dependent extension (for the moment 'tao' meta-extension only). $oldVerbose = $this->options['verbose']; $this->parameters['verbose'] = false; $this->options['extension'] = $depExtId; $this->actionCompile(); $this->options['extension'] = $extension; $this->parameters['verbose'] = $oldVerbose; $poFileReader = new tao_helpers_translation_POFileReader($depPath); $poFileReader->read(); $poFile = $poFileReader->getTranslationFile(); $poCount = $poFile->count(); $compiledTranslationFile->addTranslationUnits($poFile->getTranslationUnits()); $this->outVerbose("{$poCount} messages added."); } if (($extDirectories = scandir(ROOT_PATH)) !== false) { // Get all public messages accross extensions. foreach ($extDirectories as $extDir) { $extPath = ROOT_PATH . '/' . $extDir; if (is_dir($extPath) && is_readable($extPath) && $extDir[0] != '.' && !in_array($extDir, $dependencies) && $extDir != $extension && $extDir != 'generis') { $this->outVerbose("Adding public messages from extension '{$extDir}' in '{$language}'..."); $poPath = $this->buildLanguagePath($extDir, $language) . '/' . self::DEF_PO_FILENAME; if (!file_exists($poPath) || !is_readable($poPath)) { $this->outVerbose("Extension '{$extDir}' is not translated in language '{$language}'. Trying to retrieve default language..."); $poPath = $this->buildLanguagePath($extDir, tao_helpers_translation_Utils::getDefaultLanguage()) . '/' . self::DEF_PO_FILENAME; if (!file_exists($poPath) || !is_readable($poPath)) { $this->outVerbose("Extension '{$extDir}' in '{$language}' does not exist."); continue; } } $poFileReader = new tao_helpers_translation_POFileReader($poPath); $poFileReader->read(); $poFile = $poFileReader->getTranslationFile(); $poUnits = $poFile->getByFlag('tao-public'); $poCount = count($poUnits); $compiledTranslationFile->addTranslationUnits($poUnits); $this->outVerbose("{$poCount} public messages added."); } } // Finally, add the translation units related to the target extension. $path = $this->buildLanguagePath($extension, $language) . '/' . self::DEF_PO_FILENAME; if (file_exists($path) && is_readable($path)) { $poFileReader = new tao_helpers_translation_POFileReader($path); $poFileReader->read(); $poFile = $poFileReader->getTranslationFile(); $compiledTranslationFile->addTranslationUnits($poFile->getTranslationUnits()); // Sort the TranslationUnits. $sortingMethod = tao_helpers_translation_TranslationFile::SORT_ASC_I; $compiledTranslationFile->setTranslationUnits($compiledTranslationFile->sortBySource($sortingMethod)); $phpPath = $this->buildLanguagePath($extension, $language) . '/' . self::DEF_PHP_FILENAME; $phpFileWriter = new tao_helpers_translation_PHPFileWriter($phpPath, $compiledTranslationFile); $phpFileWriter->write(); $this->outVerbose("PHP compiled translations for extension '{$extension}' with '{$language}' written."); $jsPath = $this->buildLanguagePath($extension, $language) . '/' . self::DEF_JS_FILENAME; $jsFileWriter = new tao_helpers_translation_JSFileWriter($jsPath, $compiledTranslationFile); $jsFileWriter->write(); $this->outVerbose("JavaScript compiled translations for extension '{$extension}' with '{$language}' written."); } else { $this->err("PO file '{$path}' for extension '{$extension}' with language '{$language}' cannot be read.", true); } } else { $this->err("Cannot list TAO Extensions from root path. Check your system rights.", true); } $this->outVerbose("Translations for '{$extension}' with language '{$language}' gracefully compiled."); } }