14 require(dirname(dirname(dirname(dirname(__FILE__)))) .
'/tools/bootstrap.inc.php');
31 $this->source = array_shift(
$argv);
32 $this->target = array_shift(
$argv);
35 if (empty($this->source) || !file_exists($this->source)) {
41 if (empty($this->target)) {
42 if (is_file($this->source)) {
52 if (file_exists($this->target) && (is_dir($this->source) != is_dir($this->target))) {
62 echo
"Script to convert XML locale file to PO format\n\n"
63 .
"Usage: {$this->scriptName} input-locale-file.xml output-file.po\n\n"
64 .
"or, to convert all locale files in a specified directory:\n\n"
65 .
"{$this->scriptName} input-path [output-path]\n\n"
66 .
"When specifying a path, the output path is optional. If it is not specified, the input path will be used.\n";
77 $data = $xmlDao->parseStruct($filename, array(
'message'));
80 if (isset($data[
'message'])) {
81 foreach ($data[
'message'] as $messageData) {
82 $localeData[$messageData[
'attributes'][
'key']] = $messageData[
'value'];
95 $localeData = array();
98 if (!$sourceData)
throw new Exception(
'Unable to load source file ' .
$source);
100 $translations = new \Gettext\Translations();
101 foreach ($sourceData as $key => $sourceTranslation) {
102 $translation = new \Gettext\Translation(
'', $key);
103 $translation->setTranslation(
"$sourceTranslation");
104 $translations->append($translation);
107 return $translations->toPoFile(
$target);
114 if (is_dir($this->source)) {
116 import(
'lib.pkp.classes.file.FileManager');
120 $iterator =
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->source));
121 foreach ($iterator as $file) {
122 if ($file->isDir())
continue;
123 $pathname = $file->getPathname();
124 if (strrchr($pathname,
'.') !=
'.xml')
continue;
127 if (!self::parseLocaleFile($pathname))
continue;
130 if (substr($pathname, 0, strlen($this->source)) !== $this->source)
continue;
131 $targetPath = $this->target . dirname(substr($pathname, strlen($this->source)));
132 $targetFile = $targetPath .
'/' . basename($pathname,
'.xml') .
'.po';
135 $fileManager->mkdirtree($targetPath);
137 echo
"$pathname => $targetFile\n";