Skip to content

Commit

Permalink
Merge pull request #5314 from asmecher/i4779-email-convert
Browse files Browse the repository at this point in the history
#4779 Move email templates to PO structure
  • Loading branch information
asmecher authored Dec 3, 2019
2 parents f2bf64d + 1546054 commit b2c1c70
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 4 deletions.
1 change: 1 addition & 0 deletions classes/i18n/PKPLocale.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
define('LOCALE_COMPONENT_APP_ADMIN', 0x00000105);
define('LOCALE_COMPONENT_APP_DEFAULT', 0x00000106);
define('LOCALE_COMPONENT_APP_API', 0x00000107);
define('LOCALE_COMPONENT_APP_EMAIL', 0x00000108);

class PKPLocale {
static $request;
Expand Down
18 changes: 15 additions & 3 deletions classes/mail/EmailTemplateDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,20 @@ function installEmailTemplateData($templateDataFile, $returnSql = false, $emailK
$data = $xmlDao->parse($templateDataFile, array('email_texts', 'email_text', 'subject', 'body', 'description'));
if (!$data) return false;
$locale = $data->getAttribute('locale');
AppLocale::requireComponents(LOCALE_COMPONENT_APP_EMAIL, $locale);

foreach ($data->getChildren() as $emailNode) {
$subject = $emailNode->getChildValue('subject');
$body = $emailNode->getChildValue('body');
$description = $emailNode->getChildValue('description');

// Translate variable contents
foreach (array(&$subject, &$body, &$description) as &$var) {
$var = preg_replace_callback('{{translate key="([^"]+)"}}', function($matches) {
return __($matches[1]);
}, $var);
}

if ($emailKey && $emailKey != $emailNode->getAttribute('key')) continue;
$dataSource = $this->getDataSource();
$sql[] = 'DELETE FROM email_templates_default_data WHERE email_key = ' . $dataSource->qstr($emailNode->getAttribute('key')) . ' AND locale = ' . $dataSource->qstr($locale);
Expand All @@ -250,9 +262,9 @@ function installEmailTemplateData($templateDataFile, $returnSql = false, $emailK
(' .
$dataSource->qstr($emailNode->getAttribute('key')) . ', ' .
$dataSource->qstr($locale) . ', ' .
$dataSource->qstr($emailNode->getChildValue('subject')) . ', ' .
$dataSource->qstr($emailNode->getChildValue('body')) . ', ' .
$dataSource->qstr($emailNode->getChildValue('description')) .
$dataSource->qstr($subject) . ', ' .
$dataSource->qstr($body) . ', ' .
$dataSource->qstr($description) .
")";
if (!$returnSql) {
$this->update(array_shift($sql));
Expand Down
2 changes: 1 addition & 1 deletion classes/xml/XMLNode.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function &toXml($output = null) {
return $out;
}

function xmlentities($string, $quote_style=ENT_QUOTES) {
static function xmlentities($string, $quote_style=ENT_QUOTES) {
return htmlspecialchars($string, $quote_style, 'UTF-8');
}

Expand Down
126 changes: 126 additions & 0 deletions tools/xmlEmailsToPo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

/**
* @file tools/xmlEmailsToPo.php
*
* Copyright (c) 2014-2019 Simon Fraser University
* Copyright (c) 2003-2019 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class xmlEmailsToPo
* @ingroup tools
*/

require(dirname(dirname(dirname(dirname(__FILE__)))) . '/tools/bootstrap.inc.php');

class xmlEmailsToPo extends CommandLineTool {
/** @var string Name of source XML email file */
protected $sourceXmlFile;

/** @var string Name of target PO file */
protected $poFile;

/**
* Constructor
*/
function __construct($argv = array()) {
parent::__construct($argv);

array_shift($argv); // Shift the tool name off the top

$this->sourceXmlFile = array_shift($argv);
$this->poFile = array_shift($argv);

if (empty($this->sourceXmlFile) || !file_exists($this->sourceXmlFile)) {
$this->usage();
exit(2);
}

if (empty($this->poFile)) {
$this->usage();
exit(3);
}
}

/**
* Print command usage information.
*/
function usage() {
echo "Script to convert XML email file to PO format\n"
. "Usage: {$this->scriptName} input-locale-file.xml output-file.po\n";
}

/**
* Parse a locale file into an array.
* @param $filename string Filename to locale file
* @return array (key => message)
*/
static function parseEmailFile($filename, &$locale) {
$localeData = array();
$xmlDao = new XMLDAO();
$data = $xmlDao->parse($filename, array('email_texts', 'email_text', 'subject', 'body', 'description'));
if (!$data) return null;

$locale = $data->getAttribute('locale');

foreach ($data->getChildren() as $emailNode) {
$key = $emailNode->getAttribute('key');

$subject = $emailNode->getChildValue('subject');
$body = $emailNode->getChildValue('body');
$description = $emailNode->getChildValue('description');

$localeData[$key] = array(
'subject' => $emailNode->getChildValue('subject'),
'body' => $emailNode->getChildValue('body'),
'description' => $emailNode->getChildValue('description'),
);
}

return $localeData;
}

/**
* Convert an XML locale file to a PO file.
*/
function execute() {
$localeData = array();
$locale = null;

$sourceData = self::parseEmailFile($this->sourceXmlFile, $locale);
if (!$sourceData) throw new Exception('Unable to load source file ' . $this->sourceXmlFile);

import('lib.pkp.classes.file.EditableEmailFile');
$editableEmailFile = new EditableEmailFile($locale, $this->sourceXmlFile);

$translations = new \Gettext\Translations();
foreach ($sourceData as $emailKey => $sourceEmailData) {
// Convert EMAIL_KEY_NAME to emailKeyName
$camelEmailKey = str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($emailKey))));
$camelEmailKey[0] = strtolower($camelEmailKey[0]);

foreach (array('subject', 'body', 'description') as $elementName) {
$elementValue = $sourceEmailData[$elementName];
$key = "emails.$camelEmailKey.$elementName";

$translation = new \Gettext\Translation('', $key);
$translation->setTranslation($elementValue);
$translations->append($translation);
}

$editableEmailFile->update(
$emailKey,
"{translate key=\"emails.$camelEmailKey.subject\"}",
"{translate key=\"emails.$camelEmailKey.body\"}",
"{translate key=\"emails.$camelEmailKey.description\"}"
);
}
$editableEmailFile->write();

$translations->toPoFile($this->poFile);
}
}

$tool = new xmlEmailsToPo(isset($argv) ? $argv : array());
$tool->execute();

0 comments on commit b2c1c70

Please sign in to comment.