strip_language_file.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. * strip_language_file.php
  5. *
  6. * (c) 2014 by FromDual GmbH, licensed under GPL v2
  7. *
  8. * Compares a secondary language translation file with its primary
  9. * language file and strips redundant translations.
  10. *
  11. * Todo: Check if it works with multi byte (mb_*) character sets!
  12. *
  13. * Usage:
  14. * cd htdocs/langs
  15. * ../../dev/translation/strip_language_file.php <primary_lang_dir> <secondary_lang_dir> <languagefile.lang>
  16. *
  17. * Parameters:
  18. * 1 - Primary Language
  19. * 2 - Secondary Language
  20. *
  21. * Rules:
  22. * secondary string == primary string -> strip
  23. * secondary string redundant -> strip and warning
  24. * secondary string not in primary -> strip and warning
  25. * secondary string has no value -> strip and warning
  26. * secondary string != primary string -> secondary.lang.delta
  27. *
  28. * To rename all .delta fils, you can do
  29. * for fic in `ls *.delta`; do f=`echo $fic | sed -e 's/\.delta//'`; echo $f; mv $f.delta $f; done
  30. */
  31. /**
  32. * \file dev/translation/strip_language_file.php
  33. * \ingroup dev
  34. * \brief This script clean sub-languages from duplicate keys-values
  35. */
  36. $sapi_type = php_sapi_name();
  37. $script_file = basename(__FILE__);
  38. $path=dirname(__FILE__).'/';
  39. // Test if batch mode
  40. if (substr($sapi_type, 0, 3) == 'cgi') {
  41. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  42. exit;
  43. }
  44. $rc = 0;
  45. // Get and check arguments
  46. $lPrimary = isset($argv[1])?$argv[1]:'';
  47. $lSecondary = isset($argv[2])?$argv[2]:'';
  48. $filesToProcess = isset($argv[3])?$argv[3]:'';
  49. if (empty($lPrimary) || empty($lSecondary) || empty($filesToProcess))
  50. {
  51. $rc = 1;
  52. $msg = '***** Script to clean language files *****'."\n";
  53. $msg.= 'Usage: ../../dev/translation/strip_language_file.php xx_XX xx_YY [file.lang|all]'."\n";
  54. print $msg . "(rc=$rc).\n";
  55. exit($rc);
  56. }
  57. $aPrimary = array();
  58. $aSecondary = array();
  59. // Define array $filesToProcess
  60. if ($filesToProcess == 'all')
  61. {
  62. $dir = new DirectoryIterator($lPrimary);
  63. while($dir->valid()) {
  64. if(!$dir->isDot() && $dir->isFile() && ! preg_match('/^\./',$dir->getFilename())) {
  65. $files[] = $dir->getFilename();
  66. }
  67. $dir->next();
  68. }
  69. $filesToProcess=$files;
  70. }
  71. else $filesToProcess=explode(',',$filesToProcess);
  72. // Arguments should be OK here.
  73. // Loop on each file
  74. foreach($filesToProcess as $fileToProcess)
  75. {
  76. $lPrimaryFile = $lPrimary.'/'.$fileToProcess;
  77. $lSecondaryFile = $lSecondary.'/'.$fileToProcess;
  78. $output = $lSecondaryFile . '.delta';
  79. print "---- Process language file ".$lSecondaryFile."\n";
  80. if ( ! is_readable($lPrimaryFile) ) {
  81. $rc = 2;
  82. $msg = "Cannot read primary language file $lPrimaryFile.";
  83. print $msg . " (rc=$rc).\n";
  84. exit($rc);
  85. }
  86. if ( ! is_readable($lSecondaryFile) ) {
  87. $rc = 3;
  88. $msg = "Cannot read secondary language file $lSecondaryFile. We discard this file.";
  89. print $msg . "\n";
  90. continue;
  91. }
  92. // Start reading and parsing Secondary
  93. if ( $handle = fopen($lSecondaryFile, 'r') )
  94. {
  95. print "Read Secondary File $lSecondaryFile:\n";
  96. $cnt = 0;
  97. while (($line = fgets($handle)) !== false)
  98. {
  99. $cnt++;
  100. // strip comments
  101. if ( preg_match("/^\w*#/", $line) ) {
  102. continue;
  103. }
  104. // strip empty lines
  105. if ( preg_match("/^\w*$/", $line) ) {
  106. continue;
  107. }
  108. $a = mb_split('=', trim($line), 2);
  109. if ( count($a) != 2 ) {
  110. print "ERROR in file $lSecondaryFile, line $cnt: " . trim($line) . "\n";
  111. continue;
  112. }
  113. list($key, $value) = $a;
  114. // key is redundant
  115. if ( array_key_exists($key, $aSecondary) ) {
  116. print "Key $key is redundant in file $lSecondaryFile (line: $cnt).\n";
  117. continue;
  118. }
  119. // String has no value
  120. if ( $value == '' ) {
  121. print "Key $key has no value in file $lSecondaryFile (line: $cnt).\n";
  122. continue;
  123. }
  124. $aSecondary[$key] = trim($value);
  125. }
  126. if ( ! feof($handle) )
  127. {
  128. $rc = 5;
  129. $msg = "Unexpected fgets() fail";
  130. print $msg . " (rc=$rc).\n";
  131. exit($rc);
  132. }
  133. fclose($handle);
  134. }
  135. else {
  136. $rc = 6;
  137. $msg = "Cannot open file $lSecondaryFile";
  138. print $msg . " (rc=$rc).\n";
  139. exit($rc);
  140. }
  141. // Start reading and parsing Primary. See rules in header!
  142. $arrayofkeytoalwayskeep=array('DIRECTION','FONTFORPDF','FONTSIZEFORPDF','SeparatorDecimal','SeparatorThousand');
  143. if ( $handle = fopen($lPrimaryFile, 'r') )
  144. {
  145. if ( ! $oh = fopen($output, 'w') )
  146. {
  147. print "ERROR in writing to file $output\n";
  148. exit;
  149. }
  150. print "Read Primary File $lPrimaryFile and write ".$output.":\n";
  151. fwrite($oh, "# Dolibarr language file - Source file is en_US - ".(preg_replace('/\.lang$/','',$fileToProcess))."\n");
  152. $cnt = 0;
  153. while (($line = fgets($handle)) !== false)
  154. {
  155. $cnt++;
  156. // strip comments
  157. if ( preg_match("/^\w*#/", $line) ) {
  158. continue;
  159. }
  160. // strip empty lines
  161. if ( preg_match("/^\w*$/", $line) ) {
  162. continue;
  163. }
  164. $a = mb_split('=', trim($line), 2);
  165. if ( count($a) != 2 ) {
  166. print "ERROR in file $lPrimaryFile, line $cnt: " . trim($line) . "\n";
  167. continue;
  168. }
  169. list($key, $value) = $a;
  170. // key is redundant
  171. if ( array_key_exists($key, $aPrimary) ) {
  172. print "Key $key is redundant in file $lPrimaryFile (line: $cnt) - Already found into ".$fileFirstFound[$key]." (line: ".$lineFirstFound[$key].").\n";
  173. continue;
  174. }
  175. else
  176. {
  177. $fileFirstFound[$key] = $fileToProcess;
  178. $lineFirstFound[$key] = $cnt;
  179. }
  180. // String has no value
  181. if ( $value == '' ) {
  182. print "Key $key has no value in file $lPrimaryFile (line: $cnt).\n";
  183. continue;
  184. }
  185. $aPrimary[$key] = trim($value);
  186. $fileFirstFound[$key] = $fileToProcess;
  187. $lineFirstFound[$key] = $cnt;
  188. // ----- Process output now -----
  189. // Key not in other file
  190. if (in_array($key, $arrayofkeytoalwayskeep) || preg_match('/^FormatDate/',$key) || preg_match('/^FormatHour/',$key))
  191. {
  192. //print "Key $key is a key we always want to see into secondary file (line: $cnt).\n";
  193. }
  194. else if ( ! array_key_exists($key, $aSecondary))
  195. {
  196. //print "Key $key does NOT exist in secondary language (line: $cnt).\n";
  197. continue;
  198. }
  199. // String exists in both files and does not match
  200. if ((! empty($aSecondary[$key]) && $aSecondary[$key] != $aPrimary[$key]) || in_array($key, $arrayofkeytoalwayskeep) || preg_match('/^FormatDate/',$key) || preg_match('/^FormatHour/',$key))
  201. {
  202. //print "Key $key differs so we add it into new secondary language (line: $cnt).\n";
  203. fwrite($oh, $key."=".(empty($aSecondary[$key])?$aPrimary[$key]:$aSecondary[$key])."\n");
  204. }
  205. }
  206. if ( ! feof($handle) ) {
  207. $rc = 7;
  208. $msg = "Unexpected fgets() fail";
  209. print $msg . " (rc=$rc).\n";
  210. exit($rc);
  211. }
  212. fclose($oh);
  213. fclose($handle);
  214. }
  215. else {
  216. $rc = 8;
  217. $msg = "Cannot open file $lPrimaryFile";
  218. print $msg . " (rc=$rc).\n";
  219. exit($rc);
  220. }
  221. print "Output can be found at $output.\n";
  222. }
  223. return 0;