get_translation.lib.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * Library for language translation from Chamilo language files to XML for videoconference
  5. * @uses api.lib.php for api_get_path()
  6. */
  7. /**
  8. * This function reads a Chamilo language file and transforms it into XML,
  9. * then returns the XML string to the caller.
  10. */
  11. function get_language_file_as_xml($language='english')
  12. {
  13. $path = api_get_path(SYS_LANG_PATH).$language.'/';
  14. if(!is_dir($path) or !is_readable($path))
  15. {
  16. if($language != 'english')
  17. {
  18. return get_language_file_as_xml('english');
  19. }
  20. else
  21. {
  22. return '';
  23. }
  24. }
  25. //error_log('Analysing path '.$path);
  26. $file = $path.'videoconf.inc.php';
  27. if(!is_file($file) or !is_readable($file))
  28. {
  29. if($language != 'english')
  30. {
  31. return get_language_file_as_xml('english');
  32. }
  33. else
  34. {
  35. return '';
  36. }
  37. }
  38. /*
  39. $convert = true;
  40. if(substr($language,-7,7) == 'unicode')
  41. {//do not convert if the language ends with 'unicode', which means it's in UTF-8
  42. $convert=false;
  43. }
  44. $list = file($file);
  45. $xml = '';
  46. foreach ( $list as $line )
  47. {
  48. if(substr($line,0,1)=='$')
  49. {
  50. $items = array();
  51. $match = preg_match('/^\$([^\s]*)\s*=\s*"(.*)";$/',$line,$items);
  52. if($match)
  53. {
  54. //todo: The following conversion should only happen for old language files (encoded in ISO-8859-1).
  55. if($convert)
  56. {
  57. $string = api_convert_encoding($items[2],'UTF-8','ISO-8859-1');
  58. }
  59. else
  60. {
  61. $string = $items[2];
  62. }
  63. $xml .= '<labelfield><labelid>'.$items[1].'</labelid><labelvalue>'.stripslashes($string).'</labelvalue></labelfield>'."\n";
  64. }
  65. }
  66. }
  67. */
  68. //---------
  69. $list = file($file);
  70. $xml = '';
  71. foreach ( $list as $line ) {
  72. if(substr($line, 0, 1)=='$') {
  73. $items = array();
  74. $match = preg_match('/^\$([^\s]*)\s*=\s*"(.*)";$/', $line, $items);
  75. if($match) {
  76. $string = $items[2];
  77. if (!api_is_valid_utf8($string)) {
  78. $string = api_html_entity_decode(api_utf8_encode($string), ENT_QUOTES, 'UTF-8');
  79. }
  80. $xml .= '<labelfield><labelid>'.$items[1].'</labelid><labelvalue>'.stripslashes($string).'</labelvalue></labelfield>'."\n";
  81. }
  82. }
  83. }
  84. //---------
  85. if(empty($xml) && $language!='english')
  86. {
  87. return get_language_file_as_xml('english');
  88. }
  89. return $xml;
  90. }
  91. ?>