translate_array_to_po.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'main/inc/global.inc.php';
  4. //Source language do not change
  5. $dir = api_get_path(SYS_CODE_PATH).'lang/english';
  6. //Translate this language
  7. $to_dir = api_get_path(SYS_CODE_PATH).'lang/spanish';
  8. $po_lang = 'es_ES';
  9. $save_path = api_get_path(SYS_PATH).'src/Chamilo/Resources/translations/';
  10. $save_dir_path = $save_path.$po_lang;
  11. //The new po files will be saved in $dir.'/LC_MESSAGES/';
  12. if (!is_dir($save_path)) {
  13. mkdir($save_path);
  14. }
  15. if (!is_dir($save_dir_path)) {
  16. mkdir($save_dir_path);
  17. }
  18. if (!is_dir($save_dir_path).'/LC_MESSAGES') {
  19. mkdir($save_dir_path.'/LC_MESSAGES');
  20. }
  21. if (is_dir($dir)) {
  22. if ($dh = opendir($dir)) {
  23. while (($file = readdir($dh)) !== false) {
  24. $info = pathinfo($file);
  25. if ($info['extension'] != 'php') {
  26. continue;
  27. }
  28. echo "filename: $file : filetype: " . filetype($dir.'/'.$file) . "<br >";
  29. $translations = array();
  30. $filename = $dir.'/'.$file;
  31. $po = file($filename);
  32. if (!file_exists($filename) || !file_exists($to_dir.'/'.$file)) {
  33. continue;
  34. }
  35. foreach ($po as $line) {
  36. $pos = strpos($line, '=');
  37. if ($pos) {
  38. $variable = (substr($line, 1, $pos-1));
  39. $variable = trim($variable);
  40. require $filename;
  41. $my_variable_in_english = $$variable;
  42. require $to_dir.'/'.$file;
  43. $my_variable = $$variable;
  44. $translations[] = array(
  45. 'msgid' => $my_variable_in_english,
  46. 'msgstr' => $my_variable,
  47. );
  48. }
  49. }
  50. //var_dump($translations);
  51. $info['filename'] = explode('.', $info['filename']);
  52. $info['filename'] = $info['filename'][0];
  53. $new_po_file = $save_dir_path.'/LC_MESSAGES/'.$info['filename'].'.po';
  54. var_dump($new_po_file);
  55. $fp = fopen($new_po_file, 'w');
  56. var_dump($fp);
  57. foreach ($translations as $item) {
  58. $line = 'msgid "'.addslashes($item['msgid']).'"'."\n";
  59. $line .= 'msgstr "'.addslashes($item['msgstr']).'"'."\n\n";
  60. fwrite($fp, $line);
  61. }
  62. fclose($fp);
  63. }
  64. closedir($dh);
  65. }
  66. }