PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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