PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/build/gengo/gengo-import.php

https://github.com/evo42/Aloha-Editor
PHP | 290 lines | 159 code | 70 blank | 61 comment | 17 complexity | 57bff18fb4baa1052f5cd6a514c774af MD5 | raw file
  1. <?php
  2. require_once 'nls.php';
  3. /* todos
  4. - str replace " or '
  5. */
  6. // start test data
  7. $data = '{fu:"bar",baz:["bat"]}';
  8. $data = 'define({"button.strong.tooltip":"Strong","button.em.tooltip":"Emphasize","button.b.tooltip":"Fett","button.i.tooltip":"Kursiv","button.u.tooltip":"Unterstrichen","button.cite.tooltip":"Zitat","button.q.tooltip":"","button.code.tooltip":"Code","button.abbr.tooltip":"Abk\u00fcrzung","button.del.tooltip":"Durchgestrichen","button.sub.tooltip":"Tiefgestellt","button.sup.tooltip":"Hochgestellt","button.p.tooltip":"Absatz","button.h1.tooltip":"\u00dcberschrift 1","button.h2.tooltip":"\u00dcberschrift 2","button.h3.tooltip":"\u00dcberschrift 3","button.h4.tooltip":"\u00dcberschrift 4","button.h5.tooltip":"\u00dcberschrift 5","button.h6.tooltip":"\u00dcberschrift 6","button.pre.tooltip":"Vorformatierter text","button.title.tooltip":"Titel","button.removeFormat.tooltip":"Formatierung entfernen","button.removeFormat.text":"Formatierung entfernen","GENTICS_button_p":"GENTICS_button_p_de","GENTICS_button_h1":"GENTICS_button_h1_de","GENTICS_button_h2":"GENTICS_button_h2_de","GENTICS_button_h3":"GENTICS_button_h3_de","GENTICS_button_h4":"GENTICS_button_h4_de","GENTICS_button_h5":"GENTICS_button_h5_de","GENTICS_button_h6":"GENTICS_button_h6_de","GENTICS_button_pre":"GENTICS_button_pre_de","GENTICS_button_title":"GENTICS_button_title_de"});';
  9. $data = "define( {
  10. 'root': {
  11. 'plugin.abbr.floatingmenu.tab.abbr': 'Abbreviation',
  12. 'floatingmenu.tab.format': 'Format',
  13. 'floatingmenu.tab.insert': 'Insert',
  14. 'yes': 'Yes',
  15. 'no': 'No',
  16. 'cancel': 'Cancel',
  17. 'repository.no_item_found': 'No item found.',
  18. 'repository.loading': 'Loading',
  19. 'repository.no_items_found_yet': 'No items found yet...'
  20. },
  21. 'de': true,
  22. 'eo': true,
  23. 'fi': true,
  24. 'fr': true,
  25. 'it': true,
  26. 'pl': true,
  27. 'ru': true
  28. } );";
  29. $data = '{"button.strong.tooltip":"Strong","button.em.tooltip":"Emphasize","button.b.tooltip":"Bold","button.i.tooltip":"Italic","button.u.tooltip":"Underline","button.cite.tooltip":"Cite","button.q.tooltip":"","button.code.tooltip":"Code","button.abbr.tooltip":"Abbreviation","button.del.tooltip":"Strikethrough","button.sub.tooltip":"Subscript","button.sup.tooltip":"Superscript","button.p.tooltip":"Paragraph","button.h1.tooltip":"Heading 1","button.h2.tooltip":"Heading 2","button.h3.tooltip":"Heading 3","button.h4.tooltip":"Heading 4","button.h5.tooltip":"Heading 5","button.h6.tooltip":"Heading 6","button.pre.tooltip":"Pre formated text","button.title.tooltip":"Title","button.removeFormat.tooltip":"Remove formatting","button.removeFormat.text":"Remove formatting","GENTICS_button_p":"GENTICS_button_p","GENTICS_button_h1":"GENTICS_button_h1","GENTICS_button_h2":"GENTICS_button_h2","GENTICS_button_h3":"GENTICS_button_h3","GENTICS_button_h4":"GENTICS_button_h4","GENTICS_button_h5":"GENTICS_button_h5","GENTICS_button_h6":"GENTICS_button_h6","GENTICS_button_pre":"GENTICS_button_pre","GENTICS_button_title":"GENTICS_button_title","formatBold":"Ctrl+b","formatItalic":"Ctrl+i","formatUnderline":"Ctrl+u","formatParagraph":"Alt+Ctrl+0","formatH1":"Alt+Ctrl+1","formatH2":"Alt+Ctrl+2","formatH3":"Alt+Ctrl+3","formatH4":"Alt+Ctrl+4","formatH5":"Alt+Ctrl+5","formatH6":"Alt+Ctrl+6","formatPre":"Alt+Ctrl+P","formatDel":"Ctrl+\\","formatSub":"Ctrl+,","formatSup":"Ctrl+."}';
  30. // end test data
  31. $importDir = './import/';
  32. //$command = "rm -r $importDir*";
  33. //system($command);
  34. $section = 'lib.aloha';
  35. $language = 'en';
  36. $language = 'de';
  37. //convert_nls_file($section, $language, $data);
  38. // aloha core
  39. $path = '../../src/lib/aloha/'; // nls
  40. read_nls_dir($path, false);
  41. // aloha plugins
  42. $path = '../../src/plugins/common/';
  43. read_nls_dir($path);
  44. $path = '../../src/plugins/extra/';
  45. read_nls_dir($path);
  46. function read_nls_dir($root_dir, $plugin = true) {
  47. // get aloha core translation
  48. if ($plugin != true) {
  49. echo '################################# lib.aloha';
  50. $dir = $root_dir.'nls/';
  51. // convert master
  52. $section = 'lib.aloha';
  53. // master
  54. $language = 'en';
  55. $data = file_get_contents($dir.'i18n.js');
  56. convert_nls_file($section, $language, $data);
  57. // convert other languages
  58. $languages = get_directories($dir);
  59. foreach ($languages as $language) {
  60. $path = $dir.$language.'/i18n.js';
  61. if (is_file($path)) {
  62. $data = file_get_contents($path);
  63. convert_nls_file($section, $language, $data);
  64. } else {
  65. echo "[notice] file $path not found\n";
  66. }
  67. }
  68. } else {
  69. echo "\n read root $root_dir \n";
  70. $plugins = get_directories($root_dir);
  71. print_r($plugins);
  72. //$section = str_replace('/', '.', $root_dir);
  73. $section = str_replace(array('/', 'src.'), array('.', ''), $root_dir);
  74. $section = preg_replace("/[\.]{2,}/", ".", $section);
  75. //$section = preg_replace(array("/[\.]{2,}/", ".src."), array(".", ""), $section);
  76. if (preg_match('/^\.(.*)/', $section)) {
  77. $section = substr($section, 1);
  78. }
  79. if (preg_match('/(.*)\.$/', $section)) {
  80. $section = substr($section, 0, -1);
  81. }
  82. $section_tpl = $section;
  83. foreach ($plugins as $plugin) {
  84. $section = $section_tpl.'.'.$plugin;
  85. echo "\n section $section \n";
  86. // master
  87. $language = 'en';
  88. $dir = $root_dir.$plugin.'/nls/';
  89. $path = $dir.'i18n.js';
  90. echo "\n read nls $path \n";
  91. echo 'check for file... ';
  92. if (is_file($path)) {
  93. echo 'is file... ';
  94. $data = file_get_contents($path);
  95. echo 'got contents... ';
  96. if (!empty($data)) { // do we need this?
  97. echo 'convert nls file... ';
  98. convert_nls_file($section, $language, $data, $plugin);
  99. } else {
  100. print_r($data);
  101. }
  102. echo 'done... ';
  103. } else {
  104. echo "[notice] file $path not found\n";
  105. }
  106. // convert other languages
  107. $plugin_path = $root_dir.$plugin.'/nls/';
  108. $languages = get_directories($plugin_path);
  109. //print_r($languages);
  110. foreach ($languages as $language) {
  111. $path = $dir.$language.'/i18n.js';
  112. echo "\n read nls sub $path \n";
  113. if (is_file($path)) {
  114. $data = file_get_contents($path);
  115. convert_nls_file($section, $language, $data, $plugin);
  116. } else {
  117. echo "[notice] file $path not found\n";
  118. }
  119. }
  120. }
  121. }
  122. }
  123. function convert_nls_file($section, $language, $data, $plugin = false) {
  124. $import_dir = './import/';
  125. // remove comments
  126. $data = preg_replace('(//.+)', '', $data);
  127. // clean file input
  128. $data = str_replace('define(', '', $data);
  129. $data = str_replace(');', '', $data);
  130. // singe quotes to double quotes (maybe not so good ...)
  131. $data = str_replace('\'', '"', $data);
  132. $data = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $data);
  133. $data = preg_replace("/[ ]{2,}/", " ", $data);
  134. $data = trim($data);
  135. //$data = str_replace(array("{ ", " }"), array("{", "}"), $data);
  136. // typo stuff
  137. $data = str_replace(array("} ,",
  138. "{ root:",
  139. "\", \"",
  140. "{ ", " }",
  141. "src."
  142. ),
  143. array("},",
  144. "{root:",
  145. "\",\"",
  146. "{", "}",
  147. ""
  148. ), $data);
  149. // cleanup master file
  150. //preg_match('/^{ "root": \{(.*)\},(.*)/', $data, $found);
  151. //preg_match('/^{ \'root\': \{(.*)\},(.*)/', $data, $found);
  152. preg_match('/^{[\'|"]{1}root[\'|"]{1}: \{(.*)\},(.*)/', $data, $found);
  153. //print_r($found);
  154. if (count($found) < 3) {
  155. preg_match('/^\{root: \{(.*)\},(.*)/', $data, $found);
  156. }
  157. if (count($found) == 3) {
  158. echo "\n is master file \n";
  159. $data = '{'.trim($found[1]).'}';
  160. }
  161. /*
  162. echo "\n data found \n";
  163. //print_r($found);
  164. //echo "\n\n";
  165. echo "\n data to parse \n";
  166. print_r($data);
  167. echo "\n\n";
  168. */
  169. $parsed = array();
  170. //parse_jsobj($data, $parsed);
  171. try {
  172. echo 'try ...';
  173. // hex encoded stuff
  174. $data = str_replace(array("\\"), "#@#", $data);
  175. $data = preg_replace_callback('/#@#u([0-9a-f]{4})/i', 'replace_unicode_escape_sequence', $data);
  176. parse_jsobj($data, $parsed);
  177. echo 'parsing ok ...';
  178. } catch (Exception $e) {
  179. echo 'Exception: ', $e->getMessage(), "\n";
  180. //echo "\n data to parse \n";
  181. //print_r($data);
  182. //echo "\n\n";
  183. echo 'parsing ERROR ...';
  184. }
  185. //print_r($parsed);
  186. $out = "<?php\n\n";
  187. foreach ($parsed as $key => $value) {
  188. //echo $key.' -- '.$value."\n";
  189. $out .= "\$lang['$key'] = '$value';\n";
  190. }
  191. $out .= "\n?>";
  192. $path = $import_dir.$language.'/';
  193. if (!is_dir($path)) {
  194. mkdir($path);
  195. }
  196. $path .= $section.'.php';
  197. //echo $path."\n";
  198. echo "\n write data to path: $path \n";
  199. //echo $out;
  200. //echo "\n\n";
  201. file_put_contents($path, $out);
  202. // generate old file format
  203. /*
  204. if (empty($plugin)) {
  205. $old = 'aloha';
  206. } else {
  207. // @todo $plugin_path_name with array replacement (camelcase some times)
  208. //$old = 'com.gentics.aloha.plugins.'.$plugin_path_name;
  209. $old = 'Com.gentics.aloha.plugins.'.ucfirst($plugin);
  210. }
  211. $path = $import_dir.$language.'/'.$old.'.php';
  212. echo "\nwrite data to old path: $path \n";
  213. file_put_contents($path, $out);
  214. // */
  215. }
  216. function replace_unicode_escape_sequence($match) {
  217. return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
  218. }