PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/vendor/symfony/src/Symfony/Component/Locale/Resources/data/update-data.php

https://github.com/jackbravo/symfony-sandbox
PHP | 324 lines | 198 code | 70 blank | 56 comment | 30 complexity | 9f7d6f41ab6de1f8d7a590cf3f829444 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. function bailout($message)
  11. {
  12. die($message."\n");
  13. }
  14. function check_dir($source)
  15. {
  16. if (!file_exists($source)) {
  17. bailout('The directory '.$source.' does not exist');
  18. }
  19. if (!is_dir($source)) {
  20. bailout('The file '.$source.' is not a directory');
  21. }
  22. }
  23. function check_command($command)
  24. {
  25. exec('which '.$command, $output, $result);
  26. if ($result !== 0) {
  27. bailout('The command "'.$command.'" is not installed');
  28. }
  29. }
  30. function clear_directory($directory)
  31. {
  32. $iterator = new \DirectoryIterator($directory);
  33. foreach ($iterator as $file) {
  34. if (!$file->isDot()) {
  35. if ($file->isDir()) {
  36. clear_directory($file->getPathname());
  37. } else {
  38. unlink($file->getPathname());
  39. }
  40. }
  41. }
  42. }
  43. function make_directory($directory)
  44. {
  45. if (!file_exists($directory)) {
  46. mkdir($directory);
  47. }
  48. if (!is_dir($directory)) {
  49. bailout('The file '.$directory.' already exists but is no directory');
  50. }
  51. }
  52. function list_files($directory, $extension)
  53. {
  54. $files = array();
  55. $iterator = new \DirectoryIterator($directory);
  56. foreach ($iterator as $file) {
  57. if (!$file->isDot() && substr($file->getFilename(), -strlen($extension)) === $extension) {
  58. $files[] = substr($file->getFilename(), 0, -strlen($extension));
  59. }
  60. }
  61. return $files;
  62. }
  63. function genrb($source, $target)
  64. {
  65. exec('genrb -d '.$target.' '.$source.DIRECTORY_SEPARATOR.'*.txt', $output, $result);
  66. if ($result !== 0) {
  67. bailout('genrb failed');
  68. }
  69. }
  70. function load_resource_bundle($locale, $directory)
  71. {
  72. $bundle = new \ResourceBundle($locale, $directory);
  73. if (null === $bundle) {
  74. bailout('The resource bundle for locale '.$locale.' could not be loaded from directory '.$directory);
  75. }
  76. return $bundle;
  77. }
  78. if ($GLOBALS['argc'] !== 2) {
  79. bailout(<<<MESSAGE
  80. Usage: php update-data.php [icu-data-directory]
  81. Updates the ICU resources in Symfony2 from the given ICU data directory. You
  82. can checkout the ICU data directory via SVN:
  83. $ svn co http://source.icu-project.org/repos/icu/icu/trunk/source/data icu-data
  84. MESSAGE
  85. );
  86. }
  87. // Verify that all required directories exist
  88. $source = $GLOBALS['argv'][1];
  89. check_dir($source);
  90. $source = realpath($source);
  91. check_dir($source.DIRECTORY_SEPARATOR.'lang');
  92. check_dir($source.DIRECTORY_SEPARATOR.'locales');
  93. check_dir($source.DIRECTORY_SEPARATOR.'region');
  94. check_command('genrb');
  95. // Convert the *.txt resource bundles to *.res files
  96. $target = __DIR__;
  97. $langDir = $target.DIRECTORY_SEPARATOR.'lang';
  98. $localesDir = $target.DIRECTORY_SEPARATOR.'locales';
  99. $namesDir = $target.DIRECTORY_SEPARATOR.'names';
  100. $namesGeneratedDir = $namesDir.DIRECTORY_SEPARATOR.'generated';
  101. $regionDir = $target.DIRECTORY_SEPARATOR.'region';
  102. make_directory($langDir);
  103. clear_directory($langDir);
  104. genrb($source.DIRECTORY_SEPARATOR.'lang', $langDir);
  105. make_directory($localesDir);
  106. clear_directory($localesDir);
  107. genrb($source.DIRECTORY_SEPARATOR.'locales', $localesDir);
  108. make_directory($regionDir);
  109. clear_directory($regionDir);
  110. genrb($source.DIRECTORY_SEPARATOR.'region', $regionDir);
  111. make_directory($namesDir);
  112. clear_directory($namesDir);
  113. make_directory($namesGeneratedDir);
  114. clear_directory($namesGeneratedDir);
  115. // Discover the list of supported locales, which are the names of the resource
  116. // bundles in the "locales" directory
  117. $supportedLocales = list_files($localesDir, '.res');
  118. sort($supportedLocales);
  119. // Delete unneeded locales
  120. foreach ($supportedLocales as $key => $supportedLocale) {
  121. // Delete all aliases from the list
  122. // i.e., "az_AZ" is an alias for "az_Latn_AZ"
  123. $localeBundleOrig = file_get_contents($source.DIRECTORY_SEPARATOR.'locales'.DIRECTORY_SEPARATOR.$supportedLocale.'.txt');
  124. // The key "%%ALIAS" is not accessible through the \ResourceBundle class
  125. if (strpos($localeBundleOrig, '%%ALIAS') !== false) {
  126. unset($supportedLocales[$key]);
  127. }
  128. // Delete locales that have no content (i.e. only "Version" key)
  129. $localeBundle = load_resource_bundle($supportedLocale, $localesDir);
  130. // There seems to be no other way for identifying all keys in this specific
  131. // resource bundle
  132. $bundleKeys = array();
  133. foreach ($localeBundle as $bundleKey => $_) {
  134. $bundleKeys[] = $bundleKey;
  135. }
  136. if ($bundleKeys === array('Version')) {
  137. unset($supportedLocales[$key]);
  138. }
  139. }
  140. // Discover the list of locales for which individual language/region names
  141. // exist. This list contains for example "de" and "de_CH", but not "de_DE" which
  142. // is equal to "de"
  143. $translatedLocales = array_unique(array_merge(
  144. list_files($langDir, '.res'),
  145. list_files($regionDir, '.res')
  146. ));
  147. sort($translatedLocales);
  148. // For each translated locale, generate a list of locale names
  149. // Each locale name has the form: "Language (Script, Region, Variant1, ...)
  150. // Script, Region and Variants are optional. If none of them is available,
  151. // the braces are not printed.
  152. foreach ($translatedLocales as $translatedLocale) {
  153. // Don't include ICU's root resource bundle
  154. if ($translatedLocale === 'root') {
  155. continue;
  156. }
  157. $langBundle = load_resource_bundle($translatedLocale, $langDir);
  158. $regionBundle = load_resource_bundle($translatedLocale, $regionDir);
  159. $localeNames = array();
  160. foreach ($supportedLocales as $supportedLocale) {
  161. // Don't include ICU's root resource bundle
  162. if ($supportedLocale === 'root') {
  163. continue;
  164. }
  165. $lang = \Locale::getPrimaryLanguage($supportedLocale);
  166. $script = \Locale::getScript($supportedLocale);
  167. $region = \Locale::getRegion($supportedLocale);
  168. $variants = \Locale::getAllVariants($supportedLocale);
  169. // Currently the only available variant is POSIX, which we don't want
  170. // to include in the list
  171. if (count($variants) > 0) {
  172. continue;
  173. }
  174. $langName = $langBundle->get('Languages')->get($lang);
  175. $extras = array();
  176. // Some languages are simply not translated
  177. // Example: "az" (Azerbaijani) has no translation in "af" (Afrikaans)
  178. if (!$langName) {
  179. continue;
  180. }
  181. // "af" (Afrikaans) has no "Scripts" block
  182. if (!$langBundle->get('Scripts')) {
  183. continue;
  184. }
  185. // "as" (Assamese) has no "Variants" block
  186. if (!$langBundle->get('Variants')) {
  187. continue;
  188. }
  189. // Discover the name of the script part of the locale
  190. // i.e. in zh_Hans_MO, "Hans" is the script
  191. if ($script) {
  192. // Some languages are translated together with their script,
  193. // i.e. "zh_Hans" is translated as "Simplified Chinese"
  194. if ($langBundle->get('Languages')->get($lang.'_'.$script)) {
  195. $langName = $langBundle->get('Languages')->get($lang.'_'.$script);
  196. // If the script is appended in braces, extract it
  197. // i.e. "zh_Hans" is translated as "Chinesisch (vereinfacht)"
  198. // in "de"
  199. if (strpos($langName, '(') !== false) {
  200. list($langName, $scriptName) = preg_split('/[\s()]/', $langName, null, PREG_SPLIT_NO_EMPTY);
  201. $extras[] = $scriptName;
  202. }
  203. } else {
  204. $scriptName = $langBundle->get('Scripts')->get($script);
  205. // Some scripts are not translated into every language
  206. if (!$scriptName) {
  207. continue;
  208. }
  209. $extras[] = $scriptName;
  210. }
  211. }
  212. // Discover the name of the region part of the locale
  213. // i.e. in de_AT, "AT" is the region
  214. if ($region) {
  215. // Some languages are translated together with their region,
  216. // i.e. "en_GB" is translated as "British English"
  217. // we don't include these languages though because they mess up
  218. // the locale sorting
  219. // if ($langBundle->get('Languages')->get($lang.'_'.$region)) {
  220. // $langName = $langBundle->get('Languages')->get($lang.'_'.$region);
  221. // } else {
  222. $regionName = $regionBundle->get('Countries')->get($region);
  223. // Some regions are not translated into every language
  224. if (!$regionName) {
  225. continue;
  226. }
  227. $extras[] = $regionName;
  228. // }
  229. }
  230. if (count($extras) > 0) {
  231. $langName .= ' (' . implode(', ', $extras) . ')';
  232. }
  233. $localeNames[$supportedLocale] = $langName;
  234. }
  235. // If no names could be generated for the current locale, skip it
  236. if (count($localeNames) === 0) {
  237. continue;
  238. }
  239. echo "Generating $translatedLocale...\n";
  240. $file = fopen($namesGeneratedDir.DIRECTORY_SEPARATOR.$translatedLocale.'.txt', 'w');
  241. fwrite($file, "$translatedLocale{\n");
  242. fwrite($file, " Locales{\n");
  243. foreach ($localeNames as $supportedLocale => $langName) {
  244. fwrite($file, " $supportedLocale{\"$langName\"}\n");
  245. }
  246. fwrite($file, " }\n");
  247. fwrite($file, "}\n");
  248. fclose($file);
  249. }
  250. // Convert generated files to binary format
  251. genrb($namesGeneratedDir, $namesDir);
  252. // Clean up
  253. clear_directory($namesGeneratedDir);
  254. rmdir($namesGeneratedDir);