Open Journal Systems  3.3.0
xmlEmailsToPo.php
1 <?php
2 
14 require(dirname(dirname(dirname(dirname(__FILE__)))) . '/tools/bootstrap.inc.php');
15 
18  protected $sourceXmlFile;
19 
21  protected $poFile;
22 
26  function __construct($argv = array()) {
27  parent::__construct($argv);
28 
29  array_shift($argv); // Shift the tool name off the top
30 
31  $this->sourceXmlFile = array_shift($argv);
32  $this->poFile = array_shift($argv);
33 
34  if (empty($this->sourceXmlFile) || !file_exists($this->sourceXmlFile)) {
35  $this->usage();
36  exit(2);
37  }
38 
39  if (empty($this->poFile)) {
40  $this->usage();
41  exit(3);
42  }
43  }
44 
48  function usage() {
49  echo "Script to convert XML email file to PO format\n"
50  . "Usage: {$this->scriptName} input-locale-file.xml output-file.po\n";
51  }
52 
58  static function parseEmailFile($filename, &$locale) {
59  $localeData = array();
60  $xmlDao = new XMLDAO();
61  $data = $xmlDao->parse($filename, array('email_texts', 'email_text', 'subject', 'body', 'description'));
62  if (!$data) return null;
63 
64  $locale = $data->getAttribute('locale');
65 
66  foreach ($data->getChildren() as $emailNode) {
67  $key = $emailNode->getAttribute('key');
68 
69  $subject = $emailNode->getChildValue('subject');
70  $body = $emailNode->getChildValue('body');
71  $description = $emailNode->getChildValue('description');
72 
73  $localeData[$key] = array(
74  'subject' => $emailNode->getChildValue('subject'),
75  'body' => $emailNode->getChildValue('body'),
76  'description' => $emailNode->getChildValue('description'),
77  );
78  }
79 
80  return $localeData;
81  }
82 
86  function execute() {
87  $localeData = array();
88  $locale = null;
89 
90  $sourceData = self::parseEmailFile($this->sourceXmlFile, $locale);
91  if (!$sourceData) throw new Exception('Unable to load source file ' . $this->sourceXmlFile);
92 
93  import('lib.pkp.classes.file.EditableEmailFile');
94  $editableEmailFile = new EditableEmailFile($locale, $this->sourceXmlFile);
95 
96  $translations = new \Gettext\Translations();
97  foreach ($sourceData as $emailKey => $sourceEmailData) {
98  // Convert EMAIL_KEY_NAME to emailKeyName
99  $camelEmailKey = str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($emailKey))));
100  $camelEmailKey[0] = strtolower($camelEmailKey[0]);
101 
102  foreach (array('subject', 'body', 'description') as $elementName) {
103  $elementValue = $sourceEmailData[$elementName];
104  $key = "emails.$camelEmailKey.$elementName";
105 
106  $translation = new \Gettext\Translation('', $key);
107  $translation->setTranslation($elementValue);
108  $translations->append($translation);
109  }
110 
111  $editableEmailFile->update(
112  $emailKey,
113  "{translate key=\"emails.$camelEmailKey.subject\"}",
114  "{translate key=\"emails.$camelEmailKey.body\"}",
115  "{translate key=\"emails.$camelEmailKey.description\"}"
116  );
117  }
118  $editableEmailFile->write();
119 
120  $translations->toPoFile($this->poFile);
121  }
122 }
123 
124 $tool = new xmlEmailsToPo(isset($argv) ? $argv : array());
125 $tool->execute();
126 
xmlEmailsToPo\__construct
__construct($argv=array())
Definition: xmlEmailsToPo.php:32
XMLDAO
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
xmlEmailsToPo\$sourceXmlFile
$sourceXmlFile
Definition: xmlEmailsToPo.php:21
xmlEmailsToPo\execute
execute()
Definition: xmlEmailsToPo.php:92
CommandLineTool
Initialization code for command-line scripts.
Definition: CliTool.inc.php:44
$tool
$tool
Definition: mergeCoverageReportTool.php:120
xmlEmailsToPo\$poFile
$poFile
Definition: xmlEmailsToPo.php:27
xmlEmailsToPo\parseEmailFile
static parseEmailFile($filename, &$locale)
Definition: xmlEmailsToPo.php:64
xmlEmailsToPo\usage
usage()
Definition: xmlEmailsToPo.php:54
xmlEmailsToPo
Definition: xmlEmailsToPo.php:16
CommandLineTool\$argv
$argv
Definition: CliTool.inc.php:53