• Main Page
  • Modules
  • Classes
  • Files
  • File List

tools/poToCountries.php

00001 <?php
00002 
00018 require(dirname(__FILE__) . '/bootstrap.inc.php');
00019 
00020 define('PO_TO_CSV_TOOL', '/usr/bin/po2csv');
00021 
00022 class poToCountries extends CommandLineTool {
00024    var $locale;
00025 
00027    var $translationFile;
00028 
00032    function poToCountries($argv = array()) {
00033       parent::CommandLineTool($argv);
00034 
00035       $toolName = array_shift($argv);
00036 
00037       $this->locale = array_shift($argv);
00038       $this->translationFile = array_shift($argv);
00039 
00040       if (  !preg_match('/^[a-z]{2}_[A-Z]{2}$/', $this->locale) ||
00041          empty($this->translationFile) ||
00042          !file_exists($this->translationFile)
00043       ) {
00044          $this->usage();
00045          exit(1);
00046       }
00047    }
00048 
00052    function usage() {
00053       echo "Script to convert PO file to OMP's ISO3166 XML format\n"
00054          . "Usage: {$this->scriptName} locale /path/to/translation.po\n";
00055    }
00056 
00060    function execute() {
00061       // Read the translated file as a map from English => Whatever
00062       $ih = popen(PO_TO_CSV_TOOL . ' ' . escapeshellarg($this->translationFile), 'r');
00063       if (!$ih) die ('Unable to read ' . $this->translationFile . ' using ' . PO_TO_CSV_TOOL . "\n");
00064 
00065       $translationMap = array();
00066       while ($row = fgetcsv($ih)) {
00067          if (count($row) != 3) continue;
00068          list($comment, $english, $translation) = $row;
00069          $translationMap[$english] = $translation;
00070       }
00071       fclose($ih);
00072 
00073       // Get the English map
00074       $countryDao =& DAORegistry::getDAO('CountryDAO');
00075       $countries =& $countryDao->getCountries();
00076 
00077       // Generate a map of code => translation
00078       $outputMap = array();
00079       foreach ($countries as $code => $english) {
00080          if (!isset($translationMap[$english])) {
00081             echo "WARNING: Unknown country \"$english\"! Using English as default.\n";
00082             $outputMap[$code] = $english;
00083          } else {
00084             $outputMap[$code] = $translationMap[$english];
00085             unset($translationMap[$english]);
00086          }
00087       }
00088 
00089       // Use the map to convert the country list to the new locale
00090       $ofn = 'registry/locale/' . $this->locale . '/countries.xml';
00091       $oh = fopen($ofn, 'w');
00092       if (!$oh) die ("Unable to $ofn for writing.\n");
00093 
00094       fwrite($oh, '<?xml version="1.0" encoding="UTF-8"?>
00095 
00096 <!--
00097   * countries.xml
00098   *
00099   * Copyright (c) 2003-2012 John Willinsky
00100   * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
00101   *
00102   * Localized list of countries.
00103   -->
00104 
00105 <!DOCTYPE countries [
00106    <!ELEMENT countries (country+)>
00107    <!ELEMENT country EMPTY>
00108       <!ATTLIST country
00109          code CDATA #REQUIRED
00110          name CDATA #REQUIRED>
00111 ]>
00112 
00113 <countries>
00114 ');
00115       foreach ($outputMap as $code => $translation) {
00116          fwrite($oh, "  <country name=\"$translation\" code=\"$code\"/>\n");
00117       }
00118 
00119       fwrite($oh, "</countries>");
00120       fclose($oh);
00121    }
00122 }
00123 
00124 $tool = new poToCountries(isset($argv) ? $argv : array());
00125 $tool->execute();
00126 
00127 ?>

Generated on Mon Sep 17 2012 13:58:56 for Open Monograph Press by  doxygen 1.7.1