lib.l10n.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # DotClear is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. function __($str)
  23. {
  24. return (!empty($GLOBALS['__l10n'][$str])) ? $GLOBALS['__l10n'][$str] : $str;
  25. }
  26. class l10n
  27. {
  28. function init()
  29. {
  30. $GLOBALS['__l10n'] = array();
  31. $GLOBALS['__l10n_files'] = array();
  32. }
  33. function set($file)
  34. {
  35. $lang_file = $file.'.lang';
  36. $po_file = $file.'.po';
  37. $php_file = $file.'.lang.php';
  38. if (file_exists($php_file))
  39. {
  40. require $php_file;
  41. }
  42. /*elseif (($tmp = l10n::getPoFile($po_file)) !== false)
  43. {
  44. $GLOBALS['__l10n_files'][] = $lang_file;
  45. $GLOBALS['__l10n'] = array_merge($GLOBALS['__l10n'],$tmp);
  46. }*/
  47. elseif (($tmp = l10n::getLangFile($lang_file)) !== false)
  48. {
  49. $GLOBALS['__l10n_files'][] = $lang_file;
  50. $GLOBALS['__l10n'] = array_merge($GLOBALS['__l10n'],$tmp);
  51. }
  52. else
  53. {
  54. return false;
  55. }
  56. }
  57. function getLangFile($file)
  58. {
  59. if (!file_exists($file)) {
  60. //trigger_error('l10n file not found',E_USER_NOTICE);
  61. return false;
  62. }
  63. $f = file($file);
  64. $res = array();
  65. for ($i=0; $i<count($f); $i++) {
  66. if (substr($f[$i],0,1) == ';' && !empty($f[$i+1])) {
  67. $res[trim(substr($f[$i],1))] = trim($f[$i+1]);
  68. $i++;
  69. }
  70. }
  71. return $res;
  72. }
  73. function getPoFile($file)
  74. {
  75. if (!file_exists($file)) {
  76. //trigger_error('l10n file not found',E_USER_NOTICE);
  77. return false;
  78. }
  79. $fc = implode('',file($file));
  80. $res = array();
  81. $matched = preg_match_all('/(msgid\s+("([^"]|\\\\")*?"\s*)+)\s+'.
  82. '(msgstr\s+("([^"]|\\\\")*?"\s*)+)/',
  83. $fc, $matches);
  84. if (!$matched) {
  85. return false;
  86. }
  87. for ($i=0; $i<$matched; $i++)
  88. {
  89. $msgid = preg_replace('/\s*msgid\s*"(.*)"\s*/s','\\1',$matches[1][$i]);
  90. $msgstr= preg_replace('/\s*msgstr\s*"(.*)"\s*/s','\\1',$matches[4][$i]);
  91. $res[l10n::poString($msgid)] = l10n::poString($msgstr);
  92. }
  93. if (!empty($res[''])) {
  94. $meta = $res[''];
  95. unset($res['']);
  96. }
  97. return $res;
  98. }
  99. function poString($string,$reverse=false)
  100. {
  101. if ($reverse) {
  102. $smap = array('"', "\n", "\t", "\r");
  103. $rmap = array('\\"', '\\n"' . "\n" . '"', '\\t', '\\r');
  104. return (string) str_replace($smap, $rmap, $string);
  105. } else {
  106. $smap = array('/"\s+"/', '/\\\\n/', '/\\\\r/', '/\\\\t/', '/\\"/');
  107. $rmap = array('', "\n", "\r", "\t", '"');
  108. return (string) preg_replace($smap, $rmap, $string);
  109. }
  110. }
  111. function lang2po($file)
  112. {
  113. $map = l10n::getLangFile($file);
  114. if ($map === false) {
  115. return false;
  116. }
  117. $res = '';
  118. foreach ($map as $k => $v)
  119. {
  120. $res .= "\n".'msgid "'.l10n::poString($k,true).'"'."\n";
  121. $res .= 'msgstr "'.l10n::poString($v,true).'"'."\n";
  122. }
  123. return $res;
  124. }
  125. function getHtmlFile($dir,$file,$lang,$encoding)
  126. {
  127. if ($encoding == 'UTF-8') {
  128. $lang .= '-utf8';
  129. }
  130. $f = $dir.'/'.$lang.'/'.$file;
  131. if (!file_exists($f)) {
  132. $f = $dir.'/en/'.$file;
  133. }
  134. if (!file_exists($f)) {
  135. return '<p>No file.</p>';
  136. } else {
  137. return implode('',file($f));
  138. }
  139. }
  140. function getISOcodes($flip=false)
  141. {
  142. $res = array(
  143. 'aa' => 'Afar',
  144. 'ab' => 'Abkhazian',
  145. 'af' => 'Afrikaans',
  146. 'am' => 'Amharic',
  147. 'ar' => 'Arabic',
  148. 'as' => 'Assamese',
  149. 'ay' => 'Aymara',
  150. 'az' => 'Azerbaijani',
  151. 'ba' => 'Bashkir',
  152. 'be' => 'Byelorussian',
  153. 'bg' => 'Bulgarian',
  154. 'bh' => 'Bihari',
  155. 'bi' => 'Bislama',
  156. 'bn' => 'Bengali',
  157. 'bo' => 'Tibetan',
  158. 'br' => 'Breton',
  159. 'ca' => 'Catalan',
  160. 'co' => 'Corsican',
  161. 'cs' => 'Czech',
  162. 'cy' => 'Welsh',
  163. 'da' => 'Danish',
  164. 'de' => 'German',
  165. 'dz' => 'Bhutani',
  166. 'el' => 'Greek',
  167. 'en' => 'English',
  168. 'eo' => 'Esperanto',
  169. 'es' => 'Spanish',
  170. 'et' => 'Estonian',
  171. 'eu' => 'Basque',
  172. 'fa' => 'Persian',
  173. 'fi' => 'Finnish',
  174. 'fj' => 'Fiji',
  175. 'fo' => 'Faeroese',
  176. 'fr' => 'French',
  177. 'fy' => 'Frisian',
  178. 'ga' => 'Irish',
  179. 'gd' => 'Gaelic',
  180. 'gl' => 'Galician',
  181. 'gn' => 'Guarani',
  182. 'gu' => 'Gujarati',
  183. 'ha' => 'Hausa',
  184. 'hi' => 'Hindi',
  185. 'hr' => 'Croatian',
  186. 'hu' => 'Hungarian',
  187. 'hy' => 'Armenian',
  188. 'ia' => 'Interlingua',
  189. 'ie' => 'Interlingue',
  190. 'ik' => 'Inupiak',
  191. 'in' => 'Indonesian',
  192. 'is' => 'Icelandic',
  193. 'it' => 'Italian',
  194. 'iw' => 'Hebrew',
  195. 'ja' => 'Japanese',
  196. 'ji' => 'Yiddish',
  197. 'jw' => 'Javanese',
  198. 'ka' => 'Georgian',
  199. 'kk' => 'Kazakh',
  200. 'kl' => 'Greenlandic',
  201. 'km' => 'Cambodian',
  202. 'kn' => 'Kannada',
  203. 'ko' => 'Korean',
  204. 'ks' => 'Kashmiri',
  205. 'ku' => 'Kurdish',
  206. 'ky' => 'Kirghiz',
  207. 'la' => 'Latin',
  208. 'ln' => 'Lingala',
  209. 'lo' => 'Laothian',
  210. 'lt' => 'Lithuanian',
  211. 'lv' => 'Latvian',
  212. 'mg' => 'Malagasy',
  213. 'mi' => 'Maori',
  214. 'mk' => 'Macedonian',
  215. 'ml' => 'Malayalam',
  216. 'mn' => 'Mongolian',
  217. 'mo' => 'Moldavian',
  218. 'mr' => 'Marathi',
  219. 'ms' => 'Malay',
  220. 'mt' => 'Maltese',
  221. 'my' => 'Burmese',
  222. 'na' => 'Nauru',
  223. 'ne' => 'Nepali',
  224. 'nl' => 'Dutch',
  225. 'no' => 'Norwegian',
  226. 'oc' => 'Occitan',
  227. 'om' => 'Oromo',
  228. 'or' => 'Oriya',
  229. 'pa' => 'Punjabi',
  230. 'pl' => 'Polish',
  231. 'ps' => 'Pashto',
  232. 'pt' => 'Portuguese',
  233. 'qu' => 'Quechua',
  234. 'rm' => 'Rhaeto-Romance',
  235. 'rn' => 'Kirundi',
  236. 'ro' => 'Romanian',
  237. 'ru' => 'Russian',
  238. 'rw' => 'Kinyarwanda',
  239. 'sa' => 'Sanskrit',
  240. 'sd' => 'Sindhi',
  241. 'sg' => 'Sangro',
  242. 'sh' => 'Serbo-Croatian',
  243. 'si' => 'Singhalese',
  244. 'sk' => 'Slovak',
  245. 'sl' => 'Slovenian',
  246. 'sm' => 'Samoan',
  247. 'sn' => 'Shona',
  248. 'so' => 'Somali',
  249. 'sq' => 'Albanian',
  250. 'sr' => 'Serbian',
  251. 'ss' => 'Siswati',
  252. 'st' => 'Sesotho',
  253. 'su' => 'Sudanese',
  254. 'sv' => 'Swedish',
  255. 'sw' => 'Swahili',
  256. 'ta' => 'Tamil',
  257. 'te' => 'Tegulu',
  258. 'tg' => 'Tajik',
  259. 'th' => 'Thai',
  260. 'ti' => 'Tigrinya',
  261. 'tk' => 'Turkmen',
  262. 'tl' => 'Tagalog',
  263. 'tn' => 'Setswana',
  264. 'to' => 'Tonga',
  265. 'tr' => 'Turkish',
  266. 'ts' => 'Tsonga',
  267. 'tt' => 'Tatar',
  268. 'tw' => 'Twi',
  269. 'uk' => 'Ukrainian',
  270. 'ur' => 'Urdu',
  271. 'uz' => 'Uzbek',
  272. 'vi' => 'Vietnamese',
  273. 'vo' => 'Volapuk',
  274. 'wo' => 'Wolof',
  275. 'xh' => 'Xhosa',
  276. 'yo' => 'Yoruba',
  277. 'zh' => 'Chinese',
  278. 'zu' => 'Zulu'
  279. );
  280. if ($flip) {
  281. $res = array_flip($res);
  282. ksort($res);
  283. }
  284. return $res;
  285. }
  286. }
  287. ?>