/tools/poToCountries.php

https://github.com/mbehiels/omp · PHP · 127 lines · 92 code · 15 blank · 20 comment · 1 complexity · c1ca6d8ed92175d6165020585c72bef9 MD5 · raw file

  1. <?php
  2. /**
  3. * @file poToCountries.php
  4. *
  5. * Copyright (c) 2003-2011 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class poToCountries
  9. * @ingroup tools
  10. *
  11. * @brief CLI tool to convert a .PO file for ISO3166 into the countries.xml format
  12. * supported by the PKP suite.
  13. */
  14. require(dirname(__FILE__) . '/bootstrap.inc.php');
  15. define('PO_TO_CSV_TOOL', '/usr/bin/po2csv');
  16. class poToCountries extends CommandLineTool {
  17. /** @var $locale string */
  18. var $locale;
  19. /** @var $translationFile string */
  20. var $translationFile;
  21. /**
  22. * Constructor
  23. */
  24. function poToCountries($argv = array()) {
  25. parent::CommandLineTool($argv);
  26. $toolName = array_shift($argv);
  27. $this->locale = array_shift($argv);
  28. $this->translationFile = array_shift($argv);
  29. if ( !preg_match('/^[a-z]{2}_[A-Z]{2}$/', $this->locale) ||
  30. empty($this->translationFile) ||
  31. !file_exists($this->translationFile)
  32. ) {
  33. $this->usage();
  34. exit(1);
  35. }
  36. }
  37. /**
  38. * Print command usage information.
  39. */
  40. function usage() {
  41. echo "Script to convert PO file to OMP's ISO3166 XML format\n"
  42. . "Usage: {$this->scriptName} locale /path/to/translation.po\n";
  43. }
  44. /**
  45. * Rebuild the search index for all monographs in all presses.
  46. */
  47. function execute() {
  48. // Read the translated file as a map from English => Whatever
  49. $ih = popen(PO_TO_CSV_TOOL . ' ' . escapeshellarg($this->translationFile), 'r');
  50. if (!$ih) die ('Unable to read ' . $this->translationFile . ' using ' . PO_TO_CSV_TOOL . "\n");
  51. $translationMap = array();
  52. while ($row = fgetcsv($ih)) {
  53. if (count($row) != 3) continue;
  54. list($comment, $english, $translation) = $row;
  55. $translationMap[$english] = $translation;
  56. }
  57. fclose($ih);
  58. // Get the English map
  59. $countryDao =& DAORegistry::getDAO('CountryDAO');
  60. $countries =& $countryDao->getCountries();
  61. // Generate a map of code => translation
  62. $outputMap = array();
  63. foreach ($countries as $code => $english) {
  64. if (!isset($translationMap[$english])) {
  65. echo "WARNING: Unknown country \"$english\"! Using English as default.\n";
  66. $outputMap[$code] = $english;
  67. } else {
  68. $outputMap[$code] = $translationMap[$english];
  69. unset($translationMap[$english]);
  70. }
  71. }
  72. // Use the map to convert the country list to the new locale
  73. $ofn = 'registry/locale/' . $this->locale . '/countries.xml';
  74. $oh = fopen($ofn, 'w');
  75. if (!$oh) die ("Unable to $ofn for writing.\n");
  76. fwrite($oh, '<?xml version="1.0" encoding="UTF-8"?>
  77. <!--
  78. * countries.xml
  79. *
  80. * Copyright (c) 2003-2011 John Willinsky
  81. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  82. *
  83. * Localized list of countries.
  84. -->
  85. <!DOCTYPE countries [
  86. <!ELEMENT countries (country+)>
  87. <!ELEMENT country EMPTY>
  88. <!ATTLIST country
  89. code CDATA #REQUIRED
  90. name CDATA #REQUIRED>
  91. ]>
  92. <countries>
  93. ');
  94. foreach ($outputMap as $code => $translation) {
  95. fwrite($oh, " <country name=\"$translation\" code=\"$code\"/>\n");
  96. }
  97. fwrite($oh, "</countries>");
  98. fclose($oh);
  99. }
  100. }
  101. $tool = new poToCountries(isset($argv) ? $argv : array());
  102. $tool->execute();
  103. ?>