/** * Execute the command. * * @return integer * * @since 1.0 * @throws \UnexpectedValueException */ public function execute() { $this->getApplication()->outputTitle('Check language files'); ExtensionHelper::addDomainPath('Core', JPATH_ROOT . '/src'); ExtensionHelper::addDomainPath('Template', JPATH_ROOT . '/templates'); ExtensionHelper::addDomainPath('App', JPATH_ROOT . '/src/App'); $scopes = array('Core' => array('JTracker', 'JTracker.js'), 'Template' => array('JTracker'), 'App' => (new Filesystem(new Local(JPATH_ROOT . '/src/App')))->listPaths()); $languages = $this->getApplication()->get('languages'); $languages[] = 'templates'; $errors = false; foreach ($scopes as $domain => $extensions) { foreach ($extensions as $extension) { $scopePath = ExtensionHelper::getDomainPath($domain); $extensionPath = ExtensionHelper::getExtensionLanguagePath($extension); foreach ($languages as $language) { $path = $scopePath . '/' . $extensionPath . '/' . $language; $path .= 'templates' == $language ? '/' . $extension . '.pot' : '/' . $language . '.' . $extension . '.po'; $this->debugOut(sprintf('Check: %s-%s %s in %s', $domain, $extension, $language, $path)); if (false == file_exists($path)) { $this->debugOut('not found'); continue; } // Check if the language file has UNIX style line endings. if ("\n" != PHP_CodeSniffer_File::detectLineEndings($path)) { $this->out($path)->out('<error> The file does not have UNIX style line endings ! </error>')->out(); continue; } // Check the language file for errors. $output = shell_exec('msgfmt -c ' . $path . ' 2>&1'); if ($output) { // If the command produces any output, that means errors. $errors = true; $this->out($output); } else { $this->debugOut('ok'); } } } } $this->out($errors ? '<error> There have been errors. </error>' : '<ok>Language file syntax OK</ok>'); if ($this->exit) { exit($errors ? 1 : 0); } return $errors ? 1 : 0; }
/** * Process language files for a domain. * * @param string $extension Extension name. * @param string $domain Extension domain. * @param boolean $templates If templates should be exported. * * @throws \DomainException * @return $this * * @since 1.0 */ protected function processDomain($extension, $domain, $templates) { $domainBase = trim(str_replace(JPATH_ROOT, '', g11nExtensionHelper::getDomainPath($domain)), '/'); $g11nPath = g11nExtensionHelper::$langDirName; $filesystem = new Filesystem(new Local($this->exportDir . '/' . $domainBase . '/' . $extension . '/' . $g11nPath)); $this->out(sprintf('Processing %s %s:... ', $domain, $extension), false); // Process language templates if ($templates) { $this->out('templates... ', false); $path = g11nStorage::getTemplatePath($extension, $domain); $contents = (new Filesystem(new Local(dirname($path))))->read(basename($path)); if (false == $filesystem->put('templates/' . basename($path), $contents)) { throw new \DomainException('Can not write the file at: ' . $path); } } // Process language files foreach ($this->languages as $lang) { if ('en-GB' == $lang) { continue; } $this->out($lang . '... ', false); $path = g11nExtensionHelper::findLanguageFile($lang, $extension, $domain); if (!$path) { $this->out('<error> ' . $lang . ' NOT FOUND </error>... ', false); continue; } $contents = (new Filesystem(new Local(dirname($path))))->read(basename($path)); if (false == $filesystem->put($lang . '/' . basename($path), $contents)) { throw new \DomainException('Can not write the file: ' . basename($path)); } } $this->out('ok'); return $this; }
/** * Process language files for a domain. * * @param string $extension Extension name. * @param string $domain Extension domain. * @param string $lang Language tag e.g. en-GB or de-DE. * * @return $this * * @since 1.0 * @throws \Exception */ protected function processDomain($extension, $domain, $lang) { $this->out(sprintf('Processing: %1$s %2$s %3$s', $domain, $extension, $lang)); $languageFile = ExtensionHelper::findLanguageFile($lang, $extension, $domain); $templateFile = Storage::getTemplatePath($extension, $domain); // Check if the language file has UNIX style line endings. if ("\n" != PHP_CodeSniffer_File::detectLineEndings($templateFile)) { $this->out($templateFile)->out('<error> The file does not have UNIX style line endings ! </error>')->out(); return $this; } if (false == $languageFile) { $this->out('Creating language file...'); $scopePath = ExtensionHelper::getDomainPath($domain); $extensionPath = ExtensionHelper::getExtensionLanguagePath($extension); $path = $scopePath . '/' . $extensionPath . '/' . $lang; if (!is_dir($path)) { if (!mkdir($path, 0755, true)) { throw new \Exception('Can not create the language folder'); } } $fileName = $lang . '.' . $extension . '.po'; $options = array(); $options[] = 'input=' . $templateFile; $options[] = 'output=' . $path . '/' . $fileName; $options[] = 'no-wrap'; $options[] = 'locale=' . $lang; $cmd = 'msginit --' . implode(' --', $options) . ' 2>&1'; $this->debugOut($cmd); ob_start(); system($cmd); $msg = ob_get_clean(); if (!file_exists($templateFile)) { throw new \Exception('Can not create the language file'); } $this->out('The language file has been created')->out($msg); } else { $this->out('Updating language file...'); $options = array(); $options[] = 'update'; $options[] = 'backup=off'; $options[] = 'no-fuzzy-matching'; $options[] = 'verbose'; $options[] = 'no-wrap'; $paths = array(); $paths[] = $languageFile; $paths[] = $templateFile; $cmd = 'msgmerge --' . implode(' --', $options) . ' "' . implode('" "', $paths) . '"' . ' 2>&1'; $this->debugOut($cmd); ob_start(); system($cmd); $msg = ob_get_clean(); if ($msg) { $this->out($msg); } } return $this; }
/** * Receives language files from Transifex * * @param string $extension The extension to process * @param string $domain The domain of the extension * * @return void * * @since 1.0 * @throws \Exception */ private function receiveFiles($extension, $domain) { $this->out(sprintf('Processing: %s %s... ', $domain, $extension), false); $scopePath = ExtensionHelper::getDomainPath($domain); $extensionPath = ExtensionHelper::getExtensionLanguagePath($extension); // Fetch the file for each language and place it in the file tree foreach ($this->languages as $language) { if ('en-GB' == $language) { continue; } $this->out($language . '... ', false); // Call out to Transifex $translation = $this->transifex->translations->getTranslation($this->getApplication()->get('transifex.project'), strtolower(str_replace('.', '-', $extension)) . '-' . strtolower($domain), str_replace('-', '_', $language)); // Write the file $path = $scopePath . '/' . $extensionPath . '/' . $language . '/' . $language . '.' . $extension . '.po'; if (false == is_dir(dirname($path))) { if (false == mkdir(dirname($path))) { throw new \Exception('Could not create the directory at: ' . str_replace(JPATH_ROOT, '', dirname($path))); } } if (!file_put_contents($path, $translation->content)) { throw new \Exception('Could not store language file at: ' . str_replace(JPATH_ROOT, '', $path)); } } $this->out('ok'); }
/** * Execute the command. * * @return void * * @since 1.0 */ public function execute() { $this->getApplication()->outputTitle('Make Language templates'); ExtensionHelper::addDomainPath('Core', JPATH_ROOT . '/src'); ExtensionHelper::addDomainPath('CoreJS', JPATH_ROOT . '/www/jtracker'); ExtensionHelper::addDomainPath('Template', JPATH_ROOT . '/cache/twig'); ExtensionHelper::addDomainPath('App', JPATH_ROOT . '/cache/twig'); ExtensionHelper::addDomainPath('CLI', JPATH_ROOT); defined('JDEBUG') || define('JDEBUG', 0); $reqExtension = $this->getApplication()->input->getCmd('extension'); // Cleanup $this->delTree(JPATH_ROOT . '/cache/twig'); if (!$reqExtension || $reqExtension == 'JTracker') { // Process core files $extension = 'JTracker'; $domain = 'Core'; $this->out('Processing: ' . $domain . ' ' . $extension); $templatePath = Storage::getTemplatePath($extension, $domain); $paths = array(ExtensionHelper::getDomainPath($domain)); $this->processTemplates($extension, $domain, 'php', $paths, $templatePath); // Process core JS files $extension = 'core.js'; $domain = 'CoreJS'; $this->out('Processing: ' . $domain . ' ' . $extension); $templatePath = Storage::getTemplatePath('JTracker.js', 'Core'); $paths = array(ExtensionHelper::getDomainPath($domain)); $this->processTemplates($extension, $domain, 'js', $paths, $templatePath); // Process base template $extension = 'JTracker'; $domain = 'Template'; $this->out('Processing: ' . $domain . ' ' . $extension); $twigDir = JPATH_ROOT . '/cache/twig/JTracker'; $this->makePhpFromTwig(JPATH_ROOT . '/templates', $twigDir); $templatePath = JPATH_ROOT . '/templates/' . $extension . '/' . ExtensionHelper::$langDirName . '/templates/' . $extension . '.pot'; $paths = array(ExtensionHelper::getDomainPath($domain)); $this->processTemplates($extension, $domain, 'php', $paths, $templatePath); $this->replacePaths(JPATH_ROOT . '/templates', $twigDir, $templatePath); // Process the CLI application $extension = 'cli'; $domain = 'CLI'; $this->out('Processing: ' . $domain . ' ' . $extension); $templatePath = Storage::getTemplatePath($extension, $domain); $paths = array(ExtensionHelper::getDomainPath($domain)); $this->processTemplates($extension, $domain, 'php', $paths, $templatePath); } // Process App templates /* @type \DirectoryIterator $fileInfo */ foreach (new \DirectoryIterator(JPATH_ROOT . '/src/App') as $fileInfo) { if ($fileInfo->isDot()) { continue; } $extension = $fileInfo->getFileName(); if ($reqExtension && $reqExtension != $extension) { continue; } $this->out('Processing App: ' . $extension); $domain = 'App'; $this->makePhpFromTwig(JPATH_ROOT . '/templates/' . strtolower($extension), JPATH_ROOT . '/cache/twig/' . $extension, true); $templatePath = JPATH_ROOT . '/src/App/' . $extension . '/' . ExtensionHelper::$langDirName . '/templates/' . $extension . '.pot'; $paths = array(ExtensionHelper::getDomainPath($domain), JPATH_ROOT . '/src/App'); $this->processTemplates($extension, $domain, 'php', $paths, $templatePath); $this->replacePaths(JPATH_ROOT . '/templates/' . strtolower($extension), JPATH_ROOT . '/cache/twig/' . $extension, $templatePath); } }