forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkp#4779 Add XML email to PO convert tool
- Loading branch information
Showing
1 changed file
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|