From 2a5b9f37a094a1da596dff2d68525fb1d1885aa7 Mon Sep 17 00:00:00 2001 From: Alec Smecher Date: Mon, 2 Dec 2019 16:20:00 -0800 Subject: [PATCH] pkp/pkp-lib#4779 Add XML email to PO convert tool --- tools/xmlEmailsToPo.php | 126 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tools/xmlEmailsToPo.php diff --git a/tools/xmlEmailsToPo.php b/tools/xmlEmailsToPo.php new file mode 100644 index 00000000000..dd95108667f --- /dev/null +++ b/tools/xmlEmailsToPo.php @@ -0,0 +1,126 @@ +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(); +