Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
genTestLocale.php
1 <?php
2 
18 require(dirname(__FILE__) . '/bootstrap.inc.php');
19 
20 define('DEFAULT_IN_LOCALE', 'en_US');
21 define('DEFAULT_OUT_LOCALE', 'te_ST');
22 define('DEFAULT_OUT_LOCALE_NAME', "Test Lo\xc3\xa7ale");
23 
24 import('classes.i18n.AppLocale');
25 
27 
28  var $inLocale;
29  var $outLocale;
30 
37  function genTestLocale($argv = array()) {
38  parent::CommandLineTool($argv);
39 
40  if (count($this->argv) == 2) {
41  $this->inLocale = $this->argv[0];
42  $this->outLocale = $this->argv[1];
43 
44  } else {
45  $this->inLocale = DEFAULT_IN_LOCALE;
46  $this->outLocale = DEFAULT_OUT_LOCALE;
47  }
48 
49  $this->replaceMap = array(
50  'a' => "\xc3\xa5",
51  'A' => "\xc3\x86",
52  'c' => "\xc3\xa7",
53  'C' => "\xc3\x87",
54  'd' => "\xc3\xb0",
55  'D' => "\xc3\x90",
56  'e' => "\xc3\xa8",
57  'E' => "\xc3\x89",
58  'i' => "\xc3\xae",
59  'I' => "\xc3\x8e",
60  'n' => "\xc3\xb1",
61  'N' => "\xc3\x91",
62  'o' => "\xc3\xb3",
63  'O' => "\xc3\x92",
64  's' => "\xc3\xbe",
65  'S' => "\xc3\x9f",
66  'u' => "\xc3\xbc",
67  'U' => "\xc3\x9c",
68  'y' => "\xc3\xbd",
69  'Y' => "\xc3\x9d",
70  '&' => "&amp;"
71  );
72  }
73 
77  function execute() {
78  AppLocale::initialize();
79  $localeFiles = AppLocale::makeComponentMap($this->inLocale);
80 
81  foreach($localeFiles as $localeFilePath) {
82  $localeFile = basename($localeFilePath);
83  $outFile = dirname(dirname($localeFilePath)) . '/' . $this->outLocale . '/' . $localeFile;
84  $this->generateLocaleFile($localeFile, $localeFilePath, $outFile);
85  }
86  }
87 
94  function generateLocaleFile($localeFile, $localeFilePath, $outFile) {
95  $localeData = LocaleFile::load($localeFilePath);
96 
97  if (!isset($localeData)) {
98  printf('Invalid locale \'%s\'', $this->inLocale);
99  exit(1);
100  }
101 
102  $destDir = dirname($outFile);
103  if (!file_exists($destDir)) {
104  import('lib.pkp.classes.file.FileManager');
105  $fileManager = new FileManager();
106  if (!$fileManager->mkdir($destDir)) {
107  printf('Failed to createDirectory \'%s\'', $destDir);
108  exit(1);
109  }
110  }
111 
112  $fp = fopen($outFile, 'wb');
113  if (!$fp) {
114  printf('Failed to write to \'%s\'', $outFile);
115  exit(1);
116  }
117 
118  $dtdLocation = substr($localeFilePath, 0, 3) == 'lib' ? '../../dtd/locale.dtd' : '../../lib/pkp/dtd/locale.dtd';
119 
120  fwrite($fp,
121  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
122  "<!DOCTYPE locale SYSTEM \"$dtdLocation\">\n\n" .
123  "<!--\n" .
124  " * $localeFile\n" .
125  " *\n" .
126  " * Copyright (c) 2003-2013 John Willinsky\n" .
127  " * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.\n" .
128  " *\n" .
129  sprintf(" * Localization strings for the %s (%s) locale.\n", $this->outLocale, DEFAULT_OUT_LOCALE_NAME) .
130  " *\n" .
131  " -->\n\n" .
132  sprintf("<locale name=\"%s\" full_name=\"%s\">\n", $this->outLocale, DEFAULT_OUT_LOCALE_NAME)
133  );
134 
135  foreach ($localeData as $key => $message) {
136  $outMessage = $this->fancifyString($message);
137 
138  if (strstr($outMessage, '<') || strstr($outMessage, '>')) {
139  $outMessage = '<![CDATA[' . $outMessage . ']]>';
140  }
141 
142  fwrite($fp, sprintf("\t<message key=\"%s\">%s</message>\n", $key, $outMessage));
143  }
144 
145  fwrite($fp, "</locale>\n");
146 
147  fclose($fp);
148  }
149 
155  function fancifyString($str) {
156  $inHTML = 0;
157  $inVar = 0;
158 
159  $outStr = "";
160 
161  for ($i = 0, $len = strlen($str); $i < $len; $i++) {
162  switch ($str[$i]) {
163  case '{':
164  $inVar++;
165  break;
166  case '}':
167  $inVar--;
168  break;
169  case '<':
170  $inHTML++;
171  break;
172  case '>':
173  $inHTML--;
174  break;
175  }
176 
177  if ($inHTML == 0 && $inVar == 0) {
178  $outStr .= strtr($str[$i], $this->replaceMap);
179  } else {
180  $outStr .= $str[$i];
181  }
182  }
183 
184  return $outStr;
185  }
186 
187 }
188 
189 $tool = new genTestLocale(isset($argv) ? $argv : array());
190 $tool->execute();
191 
192 ?>
Initialization code for command-line scripts.
Definition: CliTool.inc.php:44
CLI tool to generate a test locale file by munging the message strings of a real locale file...
generateLocaleFile($localeFile, $localeFilePath, $outFile)
genTestLocale($argv=array())
Class defining basic operations for file management.
& load($filename)
static makeComponentMap($locale)