PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpfreechat/src/pfci18n.class.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 196 lines | 78 code | 14 blank | 104 comment | 22 complexity | 2efb64722ce4be49d51d92cbe6a4bb06 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. * pfci18n.class.php
  4. *
  5. * Copyright ďż˝ 2006 Stephane Gully <stephane.gully@gmail.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301 USA
  21. */
  22. require_once (dirname(__FILE__) . "/pfctools.php");
  23. function _pfc()
  24. {
  25. $args = func_get_args();
  26. $serverid = isset($GLOBALS['serverid']) ? $GLOBALS['serverid'] : 0; // serverid is used to avoid conflicts with external code using same 'i18n' key
  27. $args[0] = isset($GLOBALS[$serverid]["i18n"][$args[0]]) && $GLOBALS[$serverid]["i18n"][$args[0]] != "" ? ($GLOBALS["output_encoding"] == "UTF-8" ? $GLOBALS[$serverid]["i18n"][$args[0]] : iconv("UTF-8", $GLOBALS["output_encoding"], $GLOBALS[$serverid]["i18n"][$args[0]])) : "_" . $args[0] . "_";
  28. return call_user_func_array('sprintf', $args);
  29. }
  30. /**
  31. * Just like _pfc but just return the raw translated string, keeping the %s into it
  32. * (used by the javascript resources (i18n) class)
  33. */
  34. function _pfc2()
  35. {
  36. $args = func_get_args();
  37. $serverid = isset($GLOBALS['serverid']) ? $GLOBALS['serverid'] : 0; // serverid is used to avoid conflicts with external code using same 'i18n' key
  38. $args[0] = isset($GLOBALS[$serverid]["i18n"][$args[0]]) && $GLOBALS[$serverid]["i18n"][$args[0]] != "" ? ($GLOBALS["output_encoding"] == "UTF-8" ? $GLOBALS[$serverid]["i18n"][$args[0]] : iconv("UTF-8", $GLOBALS["output_encoding"], $GLOBALS[$serverid]["i18n"][$args[0]])) : "_" . $args[0] . "_";
  39. return $args[0];
  40. }
  41. class pfcI18N
  42. {
  43. function Init($language, $type = "main")
  44. {
  45. if ($type == "admin")
  46. if (! in_array($language, pfcI18N :: GetAcceptedLanguage("admin")))
  47. $language = pfcI18N :: GetDefaultLanguage();
  48. if (! in_array($language, pfcI18N :: GetAcceptedLanguage()))
  49. $language = pfcI18N :: GetDefaultLanguage();
  50. if ($type == "admin")
  51. require_once (dirname(__FILE__) . "/../i18n/" . $language . "/admin.php");
  52. else
  53. require_once (dirname(__FILE__) . "/../i18n/" . $language . "/main.php");
  54. $serverid = isset($GLOBALS['serverid']) ? $GLOBALS['serverid'] : 0; // serverid is used to avoid conflicts with external code using same 'i18n' key
  55. $GLOBALS[$serverid]['i18n'] = $GLOBALS['i18n']; // do not pass by reference because $GLOBALS['i18n'] is maybe used by unknown external code
  56. $GLOBALS["output_encoding"] = "UTF-8"; // by default client/server communication is utf8 encoded
  57. }
  58. /**
  59. * Switch output encoding in order to write the right characteres in the web page
  60. */
  61. function SwitchOutputEncoding($oe = "")
  62. {
  63. if ($oe == "")
  64. {
  65. $GLOBALS["output_encoding"] = $GLOBALS["old_output_encoding"];
  66. unset($GLOBALS["old_output_encoding"]);
  67. }
  68. else
  69. {
  70. if (isset($GLOBALS["old_output_encoding"]))
  71. die("old_output_encoding must be empty (" . $GLOBALS["old_output_encoding"] . ")");
  72. $GLOBALS["old_output_encoding"] = $GLOBALS["output_encoding"];
  73. $GLOBALS["output_encoding"] = $oe;
  74. }
  75. }
  76. /**
  77. * Return the default language : "en"
  78. */
  79. function GetDefaultLanguage()
  80. {
  81. return "en_US";
  82. }
  83. /**
  84. * Return the language list supported bye i18n system
  85. * (content of the i18n directory)
  86. */
  87. function GetAcceptedLanguage($type = "main")
  88. {
  89. return /*<GetAcceptedLanguage>*/array('nl_NL', 'ko_KR', 'nl_BE', 'tr_TR', 'pt_PT', 'en_US', 'eo', 'hr_HR', 'vi_VN', 'es_ES', 'zh_TW',
  90. 'nn_NO', 'ru_RU', 'id_ID', 'hu_HU', 'th_TH', 'hy_AM', 'oc_FR', 'da_DK', 'de_DE-formal', 'uk_RO', 'nb_NO',
  91. 'fr_FR', 'it_IT', 'sv_SE', 'uk_UA', 'sr_CS', 'ar_LB', 'bg_BG', 'pt_BR', 'ba_BA', 'bn_BD', 'el_GR',
  92. 'zh_CN', 'gl_ES', 'pl_PL', 'de_DE-informal', 'ja_JP'); /*</GetAcceptedLanguage>*/
  93. }
  94. /**
  95. * Parse the source-code and update the i18n ressources files
  96. */
  97. function UpdateMessageRessources()
  98. {
  99. // first of all, update the GetAcceptedLanguage list
  100. $i18n_basepath = dirname(__FILE__) . '/../i18n';
  101. $i18n_accepted_lang = array();
  102. $dh = opendir($i18n_basepath);
  103. while (false !== ($file = readdir($dh)))
  104. {
  105. // skip . and .. generic files, skip also .svn directory
  106. if ($file == "." || $file == ".." || strpos($file, ".") === 0)
  107. continue;
  108. if (file_exists($i18n_basepath . '/' . $file . '/main.php'))
  109. $i18n_accepted_lang[] = $file;
  110. }
  111. closedir($dh);
  112. $i18n_accepted_lang_str = "array('" . implode("','", $i18n_accepted_lang) . "');";
  113. $data = file_get_contents_flock(__FILE__);
  114. $data = preg_replace("/(\/\*<GetAcceptedLanguage>\*\/)(.*)(\/\*<\/GetAcceptedLanguage>\*\/)/", "$1" . $i18n_accepted_lang_str . "$3", $data);
  115. //file_put_contents(__FILE__, $data, LOCK_EX);
  116. file_put_contents(__FILE__, $data);
  117. // Now scan the source code in order to find "_pfc" patterns
  118. $files = array();
  119. $files = array_merge($files, glob(dirname(__FILE__) . "/*.php"));
  120. $files = array_merge($files, glob(dirname(__FILE__) . "/commands/*.php"));
  121. $files = array_merge($files, glob(dirname(__FILE__) . "/containers/*.php"));
  122. $files = array_merge($files, glob(dirname(__FILE__) . "/proxies/*.php"));
  123. $files = array_merge($files, glob(dirname(__FILE__) . "/client/*.php"));
  124. $files = array_merge($files, glob(dirname(__FILE__) . "/../themes/default/*.php"));
  125. $res = array();
  126. foreach ($files as $src_filename)
  127. {
  128. $lines = file($src_filename);
  129. $line_nb = 1;
  130. foreach ($lines as $l)
  131. {
  132. // the labels server side
  133. if (preg_match_all('/_pfc\("([^\"]+)"/', $l, $matches))
  134. {
  135. foreach ($matches[1] as $label)
  136. {
  137. echo "line: " . $line_nb . "\t- " . $label . "\n";
  138. $res[$label] = "// line " . $line_nb . " in " . basename($src_filename);
  139. }
  140. }
  141. // the labels client side (JS)
  142. if (preg_match_all('/"([^"]*)",\s\/\/\s_pfc/', $l, $matches))
  143. {
  144. echo "line: " . $line_nb . "\t- " . $matches[1][0] . "\n";
  145. $res[$matches[1][0]] = "// line " . $line_nb . " in " . basename($src_filename);
  146. }
  147. $line_nb ++;
  148. }
  149. }
  150. $dst_filenames = array();
  151. foreach ($i18n_accepted_lang as $lg)
  152. $dst_filenames[] = dirname(__FILE__) . "/../i18n/" . $lg . "/main.php";
  153. foreach ($dst_filenames as $dst_filename)
  154. {
  155. // filter lines to keep, line to add
  156. $old_content = file_get_contents_flock($dst_filename);
  157. // remove php tags to keep only real content
  158. $old_content = preg_replace("/^\<\?php/", "", $old_content);
  159. $old_content = preg_replace("/\?\>$/", "", $old_content);
  160. // save into the file
  161. $new_content = "";
  162. foreach ($res as $str => $com)
  163. {
  164. //echo "com=".$com."\n";
  165. //echo "str=".$str."\n";
  166. if (preg_match("/" . preg_quote($str, '/') . "/", $old_content) == 0)
  167. $new_content .= $com . "\n\$GLOBALS[\"i18n\"][\"" . $str . "\"] = \"\";\n\n";
  168. }
  169. $content = "<?php" . $old_content . $new_content . "?>";
  170. //echo $content;
  171. //file_put_contents($dst_filename, $content, LOCK_EX);
  172. file_put_contents($dst_filename, $content);
  173. }
  174. }
  175. }
  176. ?>