translate_array_to_po.php 2.4 KB

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