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