/** * Export the locale files to the browser as a tarball. * Requires tar for operation (configured in config.inc.php). * @param $locale string Locale code for exported locale */ function export($locale) { // Construct the tar command $tarBinary = Config::getVar('cli', 'tar'); if (empty($tarBinary) || !file_exists($tarBinary)) { // We can use fatalError() here as we already have a user // friendly way of dealing with the missing tar on the // index page. fatalError('The tar binary must be configured in config.inc.php\'s cli section to use the export function of this plugin!'); } $command = $tarBinary . ' cz'; $localeFilesList = TranslatorAction::getLocaleFiles($locale); $localeFilesList = array_merge($localeFilesList, TranslatorAction::getMiscLocaleFiles($locale)); $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); $localeFilesList[] = $emailTemplateDao->getMainEmailTemplateDataFilename($locale); foreach (array_values(TranslatorAction::getEmailFileMap($locale)) as $emailFile) { } // Include locale files (main file and plugin files) foreach ($localeFilesList as $file) { if (file_exists($file)) { $command .= ' ' . escapeshellarg($file); } } header('Content-Type: application/x-gtar'); header("Content-Disposition: attachment; filename=\"{$locale}.tar.gz\""); header('Cache-Control: private'); // Workarounds for IE weirdness passthru($command); }
/** * @copydoc SetupListbuilderHandler::initialize() */ function initialize($request) { parent::initialize($request); $context = $request->getContext(); $this->setTitle('plugins.generic.translator.localeFileContents'); $this->setInstructions('plugins.generic.translator.localeFileContentsDescription'); // Get and validate the locale and filename parameters $this->locale = $request->getUserVar('locale'); if (!AppLocale::isLocaleValid($this->locale)) { fatalError('Invalid locale.'); } $this->filename = $request->getUserVar('filename'); if (!in_array($this->filename, TranslatorAction::getLocaleFiles($this->locale))) { fatalError('Invalid locale file specified!'); } // Basic configuration $this->setSourceType(LISTBUILDER_SOURCE_TYPE_TEXT); $this->setSaveType(LISTBUILDER_SAVE_TYPE_EXTERNAL); $this->setSaveFieldName('localeKeys'); self::$plugin->import('controllers.listbuilder.LocaleFileListbuilderGridCellProvider'); $cellProvider = new LocaleFileListbuilderGridCellProvider($this->locale); // Key column $this->addColumn(new ListbuilderGridColumn($this, 'key', 'plugins.generic.translator.localeKey', null, self::$plugin->getTemplatePath() . 'localeFileKeyGridCell.tpl', $cellProvider, array('tabIndex' => 1))); // Value column (custom template displays English text) $this->addColumn(new ListbuilderGridColumn($this, 'value', 'plugins.generic.translator.localeKeyValue', null, self::$plugin->getTemplatePath() . 'localeFileValueGridCell.tpl', $cellProvider, array('tabIndex' => 2, 'width' => 70, 'alignment' => COLUMN_ALIGNMENT_LEFT))); }
function isLocaleFile($locale, $filename) { if (in_array($filename, TranslatorAction::getLocaleFiles($locale))) { return true; } if (in_array($filename, TranslatorAction::getMiscLocaleFiles($locale))) { return true; } if ($filename == Locale::getEmailTemplateFilename($locale)) { return true; } return false; }
/** * Test the emails in the supplied locale against those in the supplied * reference locale. * @param $locale string * @param $referenceLocale string * @return array List of errors */ function testEmails($locale, $referenceLocale) { $errors = array(); $emails = TranslatorAction::getEmailTemplates($locale); $referenceEmails = TranslatorAction::getEmailTemplates($referenceLocale); // Pass 1: For all translated emails, check that they match // against reference translations. foreach ($emails as $emailKey => $email) { // Check if a matching reference email was found. if (!isset($referenceEmails[$emailKey])) { $errors[EMAIL_ERROR_EXTRA_EMAIL][] = array('key' => $emailKey); continue; } // We've successfully found a matching reference email. // Compare it against the translation. $bodyParams = AppLocale::getParameterNames($email['body']); $referenceBodyParams = AppLocale::getParameterNames($referenceEmails[$emailKey]['body']); $diff = array_diff($bodyParams, $referenceBodyParams); if (!empty($diff)) { $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array('key' => $emailKey, 'mismatch' => $diff); } $subjectParams = AppLocale::getParameterNames($email['subject']); $referenceSubjectParams = AppLocale::getParameterNames($referenceEmails[$emailKey]['subject']); $diff = array_diff($subjectParams, $referenceSubjectParams); if (!empty($diff)) { $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array('key' => $emailKey, 'mismatch' => $diff); } $matchedReferenceEmails[] = $emailKey; unset($email); unset($referenceEmail); } // Pass 2: Make sure that there are no missing translations. foreach ($referenceEmails as $emailKey => $email) { // Extract the fields from the email to be tested. if (!isset($emails[$emailKey])) { $errors[EMAIL_ERROR_MISSING_EMAIL][] = array('key' => $emailKey); } } return $errors; }
function saveEmail($args, $request) { $this->validate(); $plugin =& $this->plugin; $this->setupTemplate($request); $locale = array_shift($args); if (!AppLocale::isLocaleValid($locale)) { $request->redirect(null, null, 'index'); } $emails = TranslatorAction::getEmailTemplates($locale); $referenceEmails = TranslatorAction::getEmailTemplates(MASTER_LOCALE); $emailKey = array_shift($args); $targetFilename = str_replace(MASTER_LOCALE, $locale, $referenceEmails[$emailKey]['templateDataFile']); // FIXME: Ugly. if (!in_array($emailKey, array_keys($emails))) { // If it's not a reference or translation email, bail. if (!in_array($emailKey, array_keys($referenceEmails))) { $request->redirect(null, null, 'index'); } // If it's a reference email but not a translated one, // create a blank file. FIXME: This is ugly. if (!file_exists($targetFilename)) { $dir = dirname($targetFilename); if (!file_exists($dir)) { mkdir($dir); } file_put_contents($targetFilename, '<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE email_texts SYSTEM "../../../../../lib/pkp/dtd/emailTemplateData.dtd"> <!-- * emailTemplateData.xml * * Copyright (c) 2003-2013 John Willinsky * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING. * * Localized email templates XML file. * --> <email_texts locale="' . $locale . '"> </email_texts>'); } } import('lib.pkp.classes.file.EditableEmailFile'); $file = new EditableEmailFile($locale, $targetFilename); $subject = $this->correctCr($request->getUserVar('subject')); $body = $this->correctCr($request->getUserVar('body')); $description = $this->correctCr($request->getUserVar('description')); if (!$file->update($emailKey, $subject, $body, $description)) { $file->insert($emailKey, $subject, $body, $description); } $file->write(); if ($request->getUserVar('returnToCheck') == 1) { $request->redirect(null, null, 'check', $locale); } else { $request->redirect(null, null, 'edit', $locale); } }
/** * Get the (validated) filename for the current request. * @param $request PKPRequest * @return string Filename */ protected function _getFilename($request) { $filename = $request->getUserVar('filename'); if (!in_array($filename, TranslatorAction::getLocaleFiles($this->locale))) { fatalError('Invalid locale file specified!'); } return $filename; }
/** * Export the locale files to the browser as a tarball. * Requires tar (configured in config.inc.php) for operation. * @param $args array Parameters. * @param $request PKPRequest Request object. */ function export($args, $request) { $locale = $request->getUserVar('locale'); if (!AppLocale::isLocaleValid($locale)) { fatalError('Invalid locale.'); } // Construct the tar command $tarBinary = Config::getVar('cli', 'tar'); if (empty($tarBinary) || !file_exists($tarBinary)) { fatalError('cli.tar binary not properly configured.'); } $command = $tarBinary . ' cz'; $emailTemplateDao = DAORegistry::getDAO('EmailTemplateDAO'); $localeFilesList = array_merge(TranslatorAction::getLocaleFiles($locale), TranslatorAction::getMiscLocaleFiles($locale), array($emailTemplateDao->getMainEmailTemplateDataFilename($locale)), array_values(TranslatorAction::getEmailFileMap($locale))); // Include locale files (main file and plugin files) foreach ($localeFilesList as $file) { if (file_exists($file)) { $command .= ' ' . escapeshellarg($file); } } header('Content-Type: application/x-gtar'); header("Content-Disposition: attachment; filename=\"{$locale}.tar.gz\""); header('Cache-Control: private'); // Workarounds for IE weirdness passthru($command); }
function saveEmail($args) { list($plugin) = TranslatorHandler::validate(); TranslatorHandler::setupTemplate(); $locale = array_shift($args); if (!Locale::isLocaleValid($locale)) { Request::redirect(null, null, 'index'); } $emails = TranslatorAction::getEmailTemplates($locale); $referenceEmails = TranslatorAction::getEmailTemplates(MASTER_LOCALE); $emailKey = array_shift($args); if (!in_array($emailKey, array_keys($referenceEmails)) && !in_array($emailKey, array_keys($emails))) { Request::redirect(null, null, 'index'); } import('i18n.EditableEmailFile'); $file =& new EditableEmailFile($locale, Locale::getEmailTemplateFilename($locale)); $subject = TranslatorHandler::correctCr(Request::getUserVar('subject')); $body = TranslatorHandler::correctCr(Request::getUserVar('body')); $description = TranslatorHandler::correctCr(Request::getUserVar('description')); if (!$file->update($emailKey, $subject, $body, $description)) { $file->insert($emailKey, $subject, $body, $description); } $file->write(); if (Request::getUserVar('returnToCheck') == 1) { Request::redirect(null, null, 'check', $locale); } else { Request::redirect(null, null, 'edit', $locale); } }
/** * Get a list of email templates for the supplied locale. Returns data in the * following data structure: * array( * 'emailKey' => array( * 'subject' => 'Localized Subject', * 'body' => 'Localized Body', * 'description' => 'Localized Description', * 'templateFile' => '/path/to/template-file.xml', * 'templateDataFile' => '/path/to/localized/data-file.xml' * ), ... * ) * @param $locale string Locale code * @return array Email template data */ static function _getEmailTemplates($locale) { $files = TranslatorAction::getEmailFileMap($locale); $returner = array(); foreach ($files as $templateFile => $templateDataFile) { if (!file_exists($templateFile)) { continue; } $xmlParser = new XMLParser(); $data = $xmlParser->parse($templateFile); foreach ($data->getChildren() as $emailNode) { $returner[$emailNode->getAttribute('key')] = array('subject' => null, 'body' => null, 'description' => null, 'templateFile' => $templateFile, 'statusCode' => 'doesNotExist'); } if (!file_exists($templateDataFile)) { continue; } $localeData = $xmlParser->parse($templateDataFile); if ($localeData) { foreach ($localeData->getChildren() as $emailNode) { $key = $emailNode->getAttribute('key'); if (isset($returner[$key])) { $returner[$key] = array_merge($returner[$key], array('subject' => $emailNode->getChildValue('subject'), 'body' => $emailNode->getChildValue('body'), 'description' => $emailNode->getChildValue('description'), 'templateDataFile' => $templateDataFile, 'statusCode' => 'exists')); } } } } // Fill in status if ($locale != MASTER_LOCALE) { $masterLocale = self::_getEmailTemplates(MASTER_LOCALE); } foreach ($returner as $key => &$emailData) { switch ($emailData['statusCode']) { case 'doesNotExist': $emailData['status'] = __('plugins.generic.translator.email.doesNotExist'); break; case 'exists': $emailData['status'] = __('plugins.generic.translator.email.complete'); if ($locale != MASTER_LOCALE) { $referenceSubject = $masterLocale[$key]['subject']; $referenceBody = $masterLocale[$key]['body']; if (0 != count(array_diff(AppLocale::getParameterNames($emailData['subject']), AppLocale::getParameterNames($referenceSubject)))) { $emailData['status'] = __('plugins.generic.translator.errors.EMAIL_ERROR_DIFFERING_PARAMS.title'); } if (0 != count(array_diff(AppLocale::getParameterNames($emailData['body']), AppLocale::getParameterNames($referenceBody)))) { $emailData['status'] = __('plugins.generic.translator.errors.LOCALE_ERROR_DIFFERING_PARAMS.title'); } } break; } } return $returner; }