translate_all_languages_array_to_po.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/';
  6. //Destination
  7. $save_path = api_get_path(SYS_PATH).'resources/locale/';
  8. if (!is_dir(api_get_path(SYS_PATH).'resources')) {
  9. mkdir(api_get_path(SYS_PATH).'resources');
  10. }
  11. //The new po files will be saved in $dir.'/LC_MESSAGES/';
  12. if (!is_dir($save_path)) {
  13. mkdir($save_path);
  14. }
  15. /*
  16. if (!is_dir($save_dir_path).'/LC_MESSAGES') {
  17. mkdir($save_dir_path.'/LC_MESSAGES');
  18. }*/
  19. $englishDir = api_get_path(SYS_CODE_PATH).'lang/english';
  20. $iterator = new FilesystemIterator($dir);
  21. foreach ($iterator as $folder) {
  22. if ($folder->isDir()) {
  23. $langPath = $folder->getPathname();
  24. if ($folder->getBasename() != 'spanish') {
  25. //continue;
  26. }
  27. $langIterator = new FilesystemIterator($langPath);
  28. $filter = new RegexIterator($langIterator, '/\.(php)$/');
  29. foreach ($filter as $phpFile) {
  30. $phpFilePath = $phpFile->getPathname();
  31. $po = file($phpFilePath);
  32. $translations = array();
  33. $englishFile = $englishDir.'/'.$phpFile->getBasename();
  34. foreach ($po as $line) {
  35. $pos = strpos($line, '=');
  36. if ($pos) {
  37. $variable = (substr($line, 1, $pos-1));
  38. $variable = trim($variable);
  39. require $englishFile;
  40. $my_variable_in_english = $variable;
  41. require $phpFilePath;
  42. $my_variable = $$variable;
  43. $translations[] = array('msgid' => $my_variable_in_english, 'msgstr' =>$my_variable);
  44. }
  45. }
  46. $code = api_get_language_isocode($folder->getBasename());
  47. //LC_MESSAGES
  48. $new_po_file = $save_path.$folder->getBasename().'/'.$phpFile->getBasename('.php').'.po';
  49. if (!is_dir($save_path.$folder->getBasename())) {
  50. mkdir($save_path.$folder->getBasename());
  51. }
  52. $fp = fopen($new_po_file, 'w');
  53. foreach ($translations as $item) {
  54. $line = 'msgid "'.addslashes($item['msgid']).'"'."\n";
  55. $line .= 'msgstr "'.addslashes($item['msgstr']).'"'."\n\n";
  56. fwrite($fp, $line);
  57. }
  58. fclose($fp);
  59. }
  60. if ($folder->getBasename() == 'bosnian') {
  61. //exit;
  62. }
  63. }
  64. }