示例#1
0
 /**
  * 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);
 }
示例#2
0
 /**
  * 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);
 }
 /**
  * @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);
 }
示例#4
0
 /**
  * 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;
 }
示例#5
0
 /**
  * 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;
 }
 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'));
 }