tools/genTestLocale.php

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

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7