PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/sugarcrm/SugarCE-Full-5.0.0/translate/inc/merge_dico.inc

http://twpug.googlecode.com/
PHP | 208 lines | 125 code | 26 blank | 57 comment | 50 complexity | 6f11d25b8ec799f68eb06ac5aaa21615 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. //function for translating each term from reference to traduction
  3. //The function make a look up in the loaded dictionary (some useful approximation and automatic translation can be performed (30% of the text) )
  4. //The records that can not be translated will be added to the to translate content.
  5. function handle_lang_rec($lang_rec) {
  6. global $idx_dico,$dico,$totrx,$perform_approx,$old_totrx,$empty;
  7. global $found,$notfound,$auto,$approx_count,$default_key;
  8. //text to translate
  9. $text = $lang_rec->text;
  10. //logic for default keys ...
  11. if (ereg('include/language',$lang_rec->file)){
  12. if (ereg('default_key$',$lang_rec->label_id) || ereg('^default_.+_key$',$lang_rec->label_id) || ereg('_default$',$lang_rec->label_id) ) {
  13. insert_in_sugar_terms($lang_rec,$text);
  14. $default_key++;$found++;
  15. return;
  16. }
  17. }
  18. //if empty, prepare an empty record to be inserted
  19. if (strlen($text) == 0) {
  20. insert_in_sugar_terms($lang_rec,"");$empty++;$found++;
  21. return;
  22. }
  23. //Text index that will be use to lookup in the indexed dico
  24. //provide some performance enhancement for large dictionaries
  25. $ind = strtoupper(strlen($text) > 1 ? $text{0}.$text{1} : $text{0});
  26. //if the text is already in the translation dictionary
  27. //insert the translated context as a new term
  28. if (isset($idx_dico[$ind]) && array_key_exists($text, $idx_dico[$ind]) ) {
  29. insert_in_sugar_terms($lang_rec,$dico[$text]);
  30. $found++;
  31. //if we are performing approximation, verify if the text can be automatically translated
  32. //it must be a unique word composed of uppercase character or symbols
  33. } else if ($perform_approx && strtoupper($text) == $text && (ereg("^([0-9A-Z()._*:%])*$",$text)) ) {
  34. insert_in_sugar_terms($lang_rec,$text);
  35. $auto++;$found++;
  36. //otherwise
  37. } else {
  38. $approx = trim(str_replace(":","",$text ));
  39. //if the text can be approximated we will insert the approximated text
  40. //approximated text are the one that ends with ':' and spaces at the start or the end
  41. //other approximation rules could be added below
  42. if ($perform_approx && $text != $approx && (trim($text) == $approx .":" || trim($text) == $approx )) {
  43. //verify if the approximation can be found in the dictionary
  44. if (array_key_exists($approx, $dico)) {
  45. $pref = "";
  46. if (ereg(":$",$text)) $add = ":";
  47. else if (ereg(": $",$text)) $add = ": ";
  48. else if (ereg(" $",$text)) $add = " ";
  49. else if (ereg("^ ",$text)) { $add = ""; $pref = " "; }
  50. insert_in_sugar_terms($lang_rec,$pref. $dico[$approx] . $add);
  51. $approx_count++;
  52. $found++;
  53. } else {
  54. //otherwise insert the approximation in the To translate content
  55. if (! in_array($approx,$totrx) && ! in_array($approx,$old_totrx)) $totrx[] = $approx;
  56. $notfound++;
  57. }
  58. } else {
  59. //text that can not be approximated inserted
  60. if (! in_array($text,$totrx) && ! in_array($text,$old_totrx)) {$totrx[] = $text;}
  61. $notfound++;
  62. }
  63. }
  64. }
  65. //this function is used to prepare the sugar_terms record corresponding at the translated package
  66. //values will be inserted in bulk into the sugar_terms table
  67. function insert_in_sugar_terms($sugar_lang_rec,$text) {
  68. global $translationKey,$version_id,$sql_values;
  69. //Escape the test to be inserted
  70. $text_insert = mysql_real_escape_string($text);
  71. //replace the file name by the translation file name
  72. $file_name = ereg_replace("[^\\./]+.lang.php$", $translationKey.".lang.php", $sugar_lang_rec->file);
  73. //values are the same key
  74. $query = "'" .$version_id . "','".$file_name ."','".$sugar_lang_rec->array_id."','" .
  75. $sugar_lang_rec->sub_array_id."','".$sugar_lang_rec->third_array_id."','" .
  76. $sugar_lang_rec->label_id . "','" . $sugar_lang_rec->level . "','TRX','".
  77. $text_insert."'";
  78. //Add the values to be inserted to the queries array
  79. $sql_values[] = $query;
  80. }
  81. //default is to perform approximation
  82. if (!isset($perform_approx)) $perform_approx= true;
  83. //error_log("VERSION : $version_id, PERFORM APPROX ".($perform_approx ? "Y" : "N"));
  84. //Step 0:Initialization
  85. //Delete all existing translated terms for the given version
  86. //As well as terms that where left for translation
  87. mysql_query("DELETE FROM sugar_terms WHERE lang = 'TRX' and version_id='$version_id' ");
  88. mysql_query("DELETE FROM sugar_lang_trx where trx = ''");
  89. //Step 1: Load Dictionay
  90. //Retrieve the content of the dictionaty
  91. $sugar_lang_dico = query_db("SELECT * FROM sugar_lang_dico");
  92. //two dictionaries are used one already indexed for faster search
  93. $idx_dico = array();
  94. $dico = array();
  95. for ($i = 0 ; $i < count($sugar_lang_dico) ; $i++) {
  96. $key = $sugar_lang_dico[$i]->ref;
  97. $value = $sugar_lang_dico[$i]->trx;
  98. //indexing is based on the two first letter of the term
  99. if (strlen($key) > 0) {
  100. $ind = strtoupper(strlen($key) > 1 ? $key{0}.$key{1} : $key{0});
  101. if (! isset($idx_dico[$ind])) $idx_dico[$ind] = array();
  102. $idx_dico[$ind][$sugar_lang_dico[$i]->ref] = $sugar_lang_dico[$i]->trx;
  103. $dico[$sugar_lang_dico[$i]->ref] = $sugar_lang_dico[$i]->trx;
  104. }
  105. }
  106. //error_log("Cardinality dictionary ".count($idx_dico));
  107. //Step 2: Loading translation that have not been commited to the dictionary
  108. //Before performing the translation we load the on-going work
  109. $count = query_db("SELECT count(*) num FROM sugar_lang_trx where trx != ''",true);
  110. $old_totrx = array();
  111. if ($count->num > 0) {
  112. msg("($count) translated values have been found in the 'To Translate' (sugar_lang_trx) table.");
  113. msg("You should commit or clear the existing values in the <A href=totrx.php>To Translate module</A>.");
  114. $arr_old_to_trx = query_db("SELECT ref FROM sugar_lang_trx where trx != ''");
  115. for ($i = 0; $i < count($arr_old_to_trx);$i++) $old_totrx[] = $arr_old_to_trx[$i]->ref;
  116. }
  117. //Step 3: Translation
  118. //Retrieve the terms to be translated
  119. $sugar_terms = query_db("SELECT * FROM sugar_terms WHERE lang = 'REF' and version_id='$version_id' order by id");
  120. //Array containing the terms that will be left for translation
  121. $totrx = array();
  122. //counters initialization
  123. $found = 0;$approx_count = 0;$notfound = 0;$auto = 0;$empty = 0; $default_key = 0;
  124. //Array for containing the set of values to insert
  125. $sql_values = array();
  126. //Loop through the terms to translate each sugar_terms record
  127. for ($i = 0 ; $i < count($sugar_terms) ; $i++) {
  128. /*if ($i % 300 == 0) {
  129. error_log (round((microtime(true) - $now) * 1000) . " : 300 Terms done " . $approx_count . " " . $notfound);
  130. $now = microtime(true);
  131. }*/
  132. handle_lang_rec($sugar_terms[$i],true);
  133. }
  134. //Perform the real insert
  135. //The insert statement is actually enormous but ensure good performance for the mysql database
  136. //It is the concatenation of all values in a form prefered by mysql
  137. //eg : insert into tabl (col,col,...) VALUES(val,val,...),(val,val,...) ...
  138. if (count($sql_values) > 0) {
  139. $query = "INSERT INTO sugar_terms (version_id,file,array_id,sub_array_id,third_array_id,label_id,level,lang,text) VALUES(";
  140. $query .= implode("),(", $sql_values).")";
  141. mysql_query($query) or die("<SPAN class=msg-err>A mysql error occured : ".mysql_error()."</SPAN>");
  142. }
  143. //Updates for the charset and the language pack name based on the manifest and the current application charset
  144. $lang_pack_name = $trxMnf->get("name");
  145. mysql_query("UPDATE sugar_terms SET text = '$html_charset' where version_id = $version_id and lang = 'TRX' and array_id = 'app_strings' and label_id = 'LBL_CHARSET'");
  146. mysql_query("UPDATE sugar_terms SET text = '$lang_pack_name' where version_id = $version_id and lang = 'TRX' and array_id = 'app_list_strings' and label_id = 'language_pack_name'");
  147. //Step 4:Load the terms that should be translated
  148. //flush the query array
  149. $sql_values = array();
  150. for ($i = 0; $i < count($totrx); $i++) {
  151. //fills it with the values that will have to be translated
  152. $sql_values[] = "'" .mysql_real_escape_string($totrx[$i])."',''";
  153. }
  154. //same mysql bulking here
  155. if (count($sql_values) > 0) {
  156. $query = "INSERT INTO sugar_lang_trx (ref,trx) VALUES(";
  157. $query .= implode("),(", $sql_values).")";
  158. mysql_query($query) or die("<SPAN class=msg-err>A mysql error occured : ".mysql_error()."</SPAN>");
  159. }
  160. $pct_total = (count($sugar_terms) == 0 ? 0 : round(($found/count($sugar_terms))* 100));
  161. $pct_approx = ($found == 0 ? 0 : round((($approx_count + $auto) / $found) * 100));
  162. $pct_totrx = ($notfound == 0 ? 0 : round(($notfound - count($totrx)) / $notfound * 100));
  163. //Step 5: Print the results;
  164. print "<TABLE align=center cellspacing=0 width='500px' class='tabform' border=1>";
  165. print "<TR class=title><TD colspan=2>Results</TD></TR>";
  166. print "<TR><TD>".count($sugar_terms)."</TD><TD>Count of referenced terms to translate</TD></TR>";
  167. print "<TR><TD>$found</TD><TD class=green>Translated term found</TD></TR>";
  168. print "<TR><TD>$notfound</TD><TD class=red>Referenced term not found</TD></TR>";
  169. print "<TR><TD>$pct_total %</TD><TD class=red>Progression</TD></TR>";
  170. print "</TABLE><BR>";
  171. print "<TABLE align=center cellspacing=0 width='500px' class='tabform' border=1>";
  172. print "<TR class=title><TD colspan=2>Details</TD></TR>";
  173. print "<TR><TD>".count($dico)."</TD><TD>Dictionary size</TD></TR>";
  174. print "<TR><TD>$empty</TD><TD>Empty terms</TD></TR>";
  175. print "<TR><TD>$auto</TD><TD>Terms automatically translated</TD></TR>";
  176. print "<TR><TD>$default_key</TD><TD>Default Keys found (automatically translated)</TD></TR>";
  177. print "<TR><TD>$approx_count</TD><TD>Approximated terms</TD></TR>";
  178. print "<TR><TD>$pct_approx %</TD><TD>Approximation Gain (Percentage of terms approximated)</TD></TR>";
  179. print "<TR><TD>".count($totrx)."</TD><TD>Terms left to translate</TD></TR>";
  180. print "<TR><TD>$pct_totrx %</TD><TD>Translation Gain (Percentage of term that will not have to be translated)</TD></TR>";
  181. print "</TABLE>";
  182. ?>