00001 <?php
00002
00015
00016
00017
00018 require(dirname(__FILE__) . '/includes/cliTool.inc.php');
00019
00020 define('DEFAULT_IN_LOCALE', 'en_US');
00021 define('DEFAULT_OUT_LOCALE', 'te_ST');
00022 define('DEFAULT_OUT_LOCALE_NAME', "Test Lo\xc3\xa7ale");
00023
00024 import('i18n.Locale');
00025
00026 class genTestLocale extends CommandLineTool {
00027
00028 var $inLocale;
00029 var $outLocale;
00030
00037 function genTestLocale($argv = array()) {
00038 parent::CommandLineTool($argv);
00039
00040 if (count($this->argv) == 2) {
00041 $this->inLocale = $this->argv[0];
00042 $this->outLocale = $this->argv[1];
00043
00044 } else {
00045 $this->inLocale = DEFAULT_IN_LOCALE;
00046 $this->outLocale = DEFAULT_OUT_LOCALE;
00047 }
00048
00049 $this->replaceMap = array(
00050 'a' => "\xc3\xa5",
00051 'A' => "\xc3\x86",
00052 'c' => "\xc3\xa7",
00053 'C' => "\xc3\x87",
00054 'd' => "\xc3\xb0",
00055 'D' => "\xc3\x90",
00056 'e' => "\xc3\xa8",
00057 'E' => "\xc3\x89",
00058 'i' => "\xc3\xae",
00059 'I' => "\xc3\x8e",
00060 'n' => "\xc3\xb1",
00061 'N' => "\xc3\x91",
00062 'o' => "\xc3\xb3",
00063 'O' => "\xc3\x92",
00064 's' => "\xc3\xbe",
00065 'S' => "\xc3\x9f",
00066 'u' => "\xc3\xbc",
00067 'U' => "\xc3\x9c",
00068 'y' => "\xc3\xbd",
00069 'Y' => "\xc3\x9d",
00070 '&' => "&"
00071 );
00072 }
00073
00077 function execute() {
00078 Locale::initialize();
00079 $localeFiles =& Locale::getLocaleFiles();
00080 $localeFile = array_shift($localeFiles[$this->inLocale]);
00081 $localeData = LocaleFile::load($localeFile->filename);
00082
00083 if (!isset($localeData)) {
00084 printf('Invalid locale \'%s\'', $this->inLocale);
00085 exit(1);
00086 }
00087
00088 $outFile = sprintf('locale/%s/locale.xml', $this->outLocale);
00089 $fp = fopen($outFile, 'wb');
00090 if (!$fp) {
00091 printf('Failed to write to \'%s\'', $outFile);
00092 exit(1);
00093 }
00094
00095 fwrite($fp,
00096 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
00097 "<!DOCTYPE locale SYSTEM \"../locale.dtd\">\n\n" .
00098 "<!--\n" .
00099 " * locale.xml\n" .
00100 " *\n" .
00101 " * Copyright (c) 2003-2008 John Willinsky\n" .
00102 " * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.\n" .
00103 " *\n" .
00104 sprintf(" * Localization strings for the %s (%s) locale.\n", $this->outLocale, DEFAULT_OUT_LOCALE_NAME) .
00105 " *\n" .
00106 " * \$Id\$\n" .
00107 " -->\n\n" .
00108 sprintf("<locale name=\"%s\" full_name=\"%s\">\n", $this->outLocale, DEFAULT_OUT_LOCALE_NAME)
00109 );
00110
00111 foreach ($localeData as $key => $message) {
00112 $outMessage = $this->fancifyString($message);
00113
00114 if (strstr($outMessage, '<') || strstr($outMessage, '>')) {
00115 $outMessage = '<![CDATA[' . $outMessage . ']]>';
00116 }
00117
00118 fwrite($fp, sprintf("\t<message key=\"%s\">%s</message>\n", $key, $outMessage));
00119 }
00120
00121 fwrite($fp, "</locale>\n");
00122
00123 fclose($fp);
00124 }
00125
00131 function fancifyString($str) {
00132 $inHTML = 0;
00133 $inVar = 0;
00134
00135 $outStr = "";
00136
00137 for ($i = 0, $len = strlen($str); $i < $len; $i++) {
00138 switch ($str[$i]) {
00139 case '{':
00140 $inVar++;
00141 break;
00142 case '}':
00143 $inVar--;
00144 break;
00145 case '<':
00146 $inHTML++;
00147 break;
00148 case '>':
00149 $inHTML--;
00150 break;
00151 }
00152
00153 if ($inHTML == 0 && $inVar == 0) {
00154 $outStr .= strtr($str[$i], $this->replaceMap);
00155 } else {
00156 $outStr .= $str[$i];
00157 }
00158 }
00159
00160 return $outStr;
00161 }
00162
00163 }
00164
00165 $tool = &new genTestLocale(isset($argv) ? $argv : array());
00166 $tool->execute();
00167
00168 ?>