/** * Perform message string munging. * @param $localeFile string * @param $localeFilePath string * @param $outFile string */ function generateLocaleFile($localeFile, $localeFilePath, $outFile) { $localeData = LocaleFile::load($localeFilePath); if (!isset($localeData)) { printf('Invalid locale \'%s\'', $this->inLocale); exit(1); } $destDir = dirname($outFile); if (!file_exists($destDir)) { import('lib.pkp.classes.file.FileManager'); $fileManager = new FileManager(); if (!$fileManager->mkdir($destDir)) { printf('Failed to createDirectory \'%s\'', $destDir); exit(1); } } $fp = fopen($outFile, 'wb'); if (!$fp) { printf('Failed to write to \'%s\'', $outFile); exit(1); } $dtdLocation = substr($localeFilePath, 0, 3) == 'lib' ? '../../dtd/locale.dtd' : '../../lib/pkp/dtd/locale.dtd'; fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . "<!DOCTYPE locale SYSTEM \"{$dtdLocation}\">\n\n" . "<!--\n" . " * {$localeFile}\n" . " *\n" . " * Copyright (c) 2013-2016 Simon Fraser University Library\n" . " * Copyright (c) 2003-2016 John Willinsky\n" . " * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.\n" . " *\n" . sprintf(" * Localization strings for the %s (%s) locale.\n", $this->outLocale, DEFAULT_OUT_LOCALE_NAME) . " *\n" . " -->\n\n" . sprintf("<locale name=\"%s\" full_name=\"%s\">\n", $this->outLocale, DEFAULT_OUT_LOCALE_NAME)); foreach ($localeData as $key => $message) { $outMessage = $this->fancifyString($message); if (strstr($outMessage, '<') || strstr($outMessage, '>')) { $outMessage = '<![CDATA[' . $outMessage . ']]>'; } fwrite($fp, sprintf("\t<message key=\"%s\">%s</message>\n", $key, $outMessage)); } fwrite($fp, "</locale>\n"); fclose($fp); }
/** * Create the test locale file. */ function execute() { Locale::initialize(); $localeFiles =& Locale::getLocaleFiles(); $localeFile = array_shift($localeFiles[$this->inLocale]); $localeData = LocaleFile::load($localeFile->filename); if (!isset($localeData)) { printf('Invalid locale \'%s\'', $this->inLocale); exit(1); } $outFile = sprintf('locale/%s/locale.xml', $this->outLocale); $fp = fopen($outFile, 'wb'); if (!$fp) { printf('Failed to write to \'%s\'', $outFile); exit(1); } fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . "<!DOCTYPE locale SYSTEM \"../locale.dtd\">\n\n" . "<!--\n" . " * locale.xml\n" . " *\n" . " * Copyright (c) 2003-2007 John Willinsky\n" . " * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.\n" . " *\n" . sprintf(" * Localization strings for the %s (%s) locale.\n", $this->outLocale, DEFAULT_OUT_LOCALE_NAME) . " *\n" . " * \$Id\$\n" . " -->\n\n" . sprintf("<locale name=\"%s\" full_name=\"%s\">\n", $this->outLocale, DEFAULT_OUT_LOCALE_NAME)); foreach ($localeData as $key => $message) { $outMessage = $this->fancifyString($message); if (strstr($outMessage, '<') || strstr($outMessage, '>')) { $outMessage = '<![CDATA[' . $outMessage . ']]>'; } fwrite($fp, sprintf("\t<message key=\"%s\">%s</message>\n", $key, $outMessage)); } fwrite($fp, "</locale>\n"); fclose($fp); }
/** * @copydoc Gridhandler::initialize() */ function initialize($request, $args = null) { parent::initialize($request); // Set the grid details. $this->setTitle('plugins.generic.translator.localeFiles'); $this->setInstructions('plugins.generic.translator.localeFileDescription'); // Fetch and prepare the grid data. $fileList = TranslatorAction::getLocaleFiles($this->locale); sort($fileList); $fileData = array(); foreach ($fileList as $file) { $referenceData = LocaleFile::load(str_replace($this->locale, MASTER_LOCALE, $file)); $referenceCount = count($referenceData); if ($exists = file_exists($file)) { $localeData = LocaleFile::load($file); $completeCount = $this->_getTranslatedCount($referenceData, $localeData); } $fileData[] = array('filename' => $file, 'status' => $exists ? $completeCount == $referenceCount ? __('plugins.generic.translator.localeFile.complete', array('reference' => $referenceCount)) : __('plugins.generic.translator.localeFile.incompleteCount', array('complete' => $completeCount, 'reference' => $referenceCount, 'percent' => (int) ($completeCount * 100 / $referenceCount))) : __('plugins.generic.translator.localeFile.doesNotExist', array('reference' => $referenceCount))); } $this->setGridDataElements($fileData); }
function EditableLocaleFile($locale, $filename) { parent::LocaleFile($locale, $filename); $this->editableFile = new EditableFile($this->filename); }
/** * Test a locale file against the given reference locale file and * return an array of errorType => array(errors). * @param $referenceLocaleFile object * @return array */ function testLocale(&$referenceLocaleFile) { $errors = array(LOCALE_ERROR_MISSING_KEY => array(), LOCALE_ERROR_EXTRA_KEY => array(), LOCALE_ERROR_DIFFERING_PARAMS => array(), LOCALE_ERROR_MISSING_FILE => array()); if ($referenceLocaleFile->isValid()) { if (!$this->isValid()) { $errors[LOCALE_ERROR_MISSING_FILE][] = array('locale' => $this->locale, 'filename' => $this->filename); return $errors; } } else { // If the reference file itself does not exist or is invalid then // there's nothing to be translated here. return $errors; } $localeContents = LocaleFile::load($this->filename); $referenceContents = LocaleFile::load($referenceLocaleFile->filename); foreach ($referenceContents as $key => $referenceValue) { if (!isset($localeContents[$key])) { $errors[LOCALE_ERROR_MISSING_KEY][] = array('key' => $key, 'locale' => $this->locale, 'filename' => $this->filename, 'reference' => $referenceValue); continue; } $value = $localeContents[$key]; $referenceParams = AppLocale::getParameterNames($referenceValue); $params = AppLocale::getParameterNames($value); if (count(array_diff($referenceParams, $params)) > 0) { $errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array('key' => $key, 'locale' => $this->locale, 'mismatch' => array_diff($referenceParams, $params), 'filename' => $this->filename, 'reference' => $referenceValue, 'value' => $value); } // After processing a key, remove it from the list; // this way, the remainder at the end of the loop // will be extra unnecessary keys. unset($localeContents[$key]); } // Leftover keys are extraneous. foreach ($localeContents as $key => $value) { $errors[LOCALE_ERROR_EXTRA_KEY][] = array('key' => $key, 'locale' => $this->locale, 'filename' => $this->filename); } return $errors; }
/** * Register a locale file against the current list. * @param $locale string Locale key * @param $filename string Filename to new locale XML file * @param $addToTop boolean Whether to add to the top of the list (true) * or the bottom (false). Allows overriding. */ function ®isterLocaleFile($locale, $filename, $addToTop = false) { $localeFiles =& AppLocale::getLocaleFiles($locale); $localeFile = new LocaleFile($locale, $filename); if (!$localeFile->isValid()) { $localeFile = null; return $localeFile; } if ($addToTop) { // Work-around: unshift by reference. array_unshift($localeFiles, ''); $localeFiles[0] =& $localeFile; } else { $localeFiles[] =& $localeFile; } HookRegistry::call('PKPLocale::registerLocaleFile', array(&$locale, &$filename, &$addToTop)); return $localeFile; }
/** * Test all locale files for the supplied locale against the supplied * reference locale, returning an array of errors. * @param $locale string Name of locale to test * @param $referenceLocale string Name of locale to test against * @return array */ function testLocale($locale, $referenceLocale) { $localeFileNames = AppLocale::getFilenameComponentMap($locale); $errors = array(); foreach ($localeFileNames as $localeFileName) { $referenceLocaleFileName = str_replace($locale, $referenceLocale, $localeFileName); $localeFile = new LocaleFile($locale, $localeFileName); $referenceLocaleFile = new LocaleFile($referenceLocale, $referenceLocaleFileName); $errors = array_merge_recursive($errors, $localeFile->testLocale($referenceLocaleFile)); unset($localeFile); unset($referenceLocaleFile); } $plugins =& PluginRegistry::loadAllPlugins(); foreach (array_keys($plugins) as $key) { $plugin =& $plugins[$key]; $referenceLocaleFilenames = $plugin->getLocaleFilename($referenceLocale); if ($referenceLocaleFilenames) { if (is_scalar($referenceLocaleFilenames)) { $referenceLocaleFilenames = array($referenceLocaleFilenames); } $localeFilenames = $plugin->getLocaleFilename($locale); if (is_scalar($localeFilenames)) { $localeFilenames = array($localeFilenames); } assert(count($localeFilenames) == count($referenceLocaleFilenames)); foreach ($referenceLocaleFilenames as $index => $referenceLocaleFilename) { assert(isset($localeFilenames[$index])); $localeFile = new LocaleFile($locale, $localeFilenames[$index]); $referenceLocaleFile = new LocaleFile($referenceLocale, $referenceLocaleFilename); $errors = array_merge_recursive($errors, $localeFile->testLocale($referenceLocaleFile)); unset($localeFile); unset($referenceLocaleFile); } } unset($plugin); } return $errors; }
/** * Test a locale file against the given reference locale file and * return an array of errorType => array(errors). * @param $referenceLocaleFile object * @return array */ function testLocale(&$referenceLocaleFile) { $errors = array(LOCALE_ERROR_MISSING_KEY => array(), LOCALE_ERROR_EXTRA_KEY => array(), LOCALE_ERROR_SUSPICIOUS_LENGTH => array(), LOCALE_ERROR_DIFFERING_PARAMS => array(), LOCALE_ERROR_MISSING_FILE => array()); if (!$this->isValid()) { $errors[LOCALE_ERROR_MISSING_FILE][] = array('locale' => $this->locale, 'filename' => $this->filename); return $errors; } $localeContents = LocaleFile::load($this->filename); $referenceContents = LocaleFile::load($referenceLocaleFile->filename); foreach ($referenceContents as $key => $referenceValue) { if (!isset($localeContents[$key])) { $errors[LOCALE_ERROR_MISSING_KEY][] = array('key' => $key, 'locale' => $this->locale, 'filename' => $this->filename, 'reference' => $referenceValue); continue; } $value = $localeContents[$key]; // Watch for suspicious lengths. if (!Locale::checkLengths($referenceValue, $value)) { $errors[LOCALE_ERROR_SUSPICIOUS_LENGTH][] = array('key' => $key, 'locale' => $this->locale, 'referenceLocale' => $referenceLocaleFile->locale, 'reference' => $referenceValue, 'value' => $value, 'filename' => $this->filename); } $referenceParams = Locale::getParameterNames($referenceValue); $params = Locale::getParameterNames($value); if (count(array_diff($referenceParams, $params)) > 0) { $errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array('key' => $key, 'locale' => $this->locale, 'mismatch' => array_diff($referenceParams, $params), 'filename' => $this->filename, 'reference' => $referenceValue, 'value' => $value); } // After processing a key, remove it from the list; // this way, the remainder at the end of the loop // will be extra unnecessary keys. unset($localeContents[$key]); } // Leftover keys are extraneous. foreach ($localeContents as $key => $value) { $errors[LOCALE_ERROR_EXTRA_KEY][] = array('key' => $key, 'locale' => $this->locale, 'filename' => $this->filename); } return $errors; }
/** * Test all locale files for the supplied locale against the supplied * reference locale, returning an array of errors. * @param $locale string Name of locale to test * @param $referenceLocale string Name of locale to test against * @return array */ function testLocale($locale, $referenceLocale) { $localeFileNames = Locale::getFilenameComponentMap($locale); $errors = array(); foreach ($localeFileNames as $localeFileName) { $referenceLocaleFileName = str_replace($locale, $referenceLocale, $localeFileName); $localeFile = new LocaleFile($locale, $localeFileName); $referenceLocaleFile = new LocaleFile($referenceLocale, $referenceLocaleFileName); $errors = array_merge_recursive($errors, $localeFile->testLocale($referenceLocaleFile)); unset($localeFile); unset($referenceLocaleFile); } $plugins =& PluginRegistry::loadAllPlugins(); foreach (array_keys($plugins) as $key) { $plugin =& $plugins[$key]; $referenceLocaleFilename = $plugin->getLocaleFilename($referenceLocale); if ($referenceLocaleFilename) { $localeFile = new LocaleFile($locale, $plugin->getLocaleFilename($locale)); $referenceLocaleFile = new LocaleFile($referenceLocale, $referenceLocaleFilename); $errors = array_merge_recursive($errors, $localeFile->testLocale($referenceLocaleFile)); unset($localeFile); unset($referenceLocaleFile); } unset($plugin); } return $errors; }
/** * Constructor * @param $locale string Locale code * @param $filename string Filename */ function __construct($locale, $filename) { parent::__construct($locale, $filename); $this->editableFile = new EditableFile($this->filename); }
function createFile($args) { $this->validate(); $plugin =& $this->plugin; $this->setupTemplate(); $locale = array_shift($args); if (!AppLocale::isLocaleValid($locale)) { Request::redirect(null, null, 'index'); } $filename = urldecode(urldecode(array_shift($args))); if (!TranslatorAction::isLocaleFile($locale, $filename)) { Request::redirect(null, null, 'edit', $locale); } import('lib.pkp.classes.file.FileManager'); $fileManager = new FileManager(); $fileManager->copyFile(TranslatorAction::determineReferenceFilename($locale, $filename), $filename); $localeKeys = LocaleFile::load($filename); import('lib.pkp.classes.file.EditableLocaleFile'); $file = new EditableLocaleFile($locale, $filename); // remove default translations from keys foreach (array_keys($localeKeys) as $key) { $file->update($key, ''); } $file->write(); Request::redirectUrl(Request::getUserVar('redirectUrl')); }