/** * 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 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'); }