get_translation.lib.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. $non_utf8_encoding = api_get_non_utf8_encoding($language);
  70. $list = file($file);
  71. $xml = '';
  72. foreach ( $list as $line ) {
  73. if(substr($line, 0, 1)=='$') {
  74. $items = array();
  75. $match = preg_match('/^\$([^\s]*)\s*=\s*"(.*)";$/', $line, $items);
  76. if($match) {
  77. $string = $items[2];
  78. if (!api_is_valid_utf8($string)) {
  79. $string = api_html_entity_decode(api_utf8_encode($string, $non_utf8_encoding), ENT_QUOTES, 'UTF-8');
  80. }
  81. $xml .= '<labelfield><labelid>'.$items[1].'</labelid><labelvalue>'.stripslashes($string).'</labelvalue></labelfield>'."\n";
  82. }
  83. }
  84. }
  85. //---------
  86. if(empty($xml) && $language!='english')
  87. {
  88. return get_language_file_as_xml('english');
  89. }
  90. return $xml;
  91. }
  92. ?>