Subversion Repositories oidplus

Rev

Rev 506 | Rev 557 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. /*
  5.  * OIDplus 2.0
  6.  * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  */
  20.  
  21. // This script updates the language message files (adding new entries and
  22. // removing entries that are not existing anymore).
  23. // It requires that PHP scripts are using following syntax for translations:
  24. //             _L('hello world',optionalParams) <recommended>
  25. //             _L("hello world",optionalParams)
  26. // and JS files:
  27. //             _L('hello world',optionalParams)
  28. //             _L("hello world",optionalParams) <recommended>
  29.  
  30. $dir = __DIR__ . '/../../';
  31.  
  32. // ---
  33.  
  34. $langs = array();
  35. $tmp = glob($dir.'/plugins/language/*/messages.xml');
  36. foreach ($tmp as $tmp2) {
  37.         $tmp3 = explode('/', $tmp2);
  38.         $lang = $tmp3[count($tmp3)-2];
  39.         if ($lang == 'enus') continue; // ignore base lang
  40.         $langs[] = $lang;
  41. }
  42.  
  43. // ---
  44.  
  45. $all_strings = array();
  46.  
  47. $it = new RecursiveDirectoryIterator($dir);
  48. foreach(new RecursiveIteratorIterator($it) as $file) {
  49.         if (strpos(str_replace('\\','/',realpath($file)),'/3p/') !== false) continue; // ignore third-party-code
  50.         if (strpos(str_replace('\\','/',realpath($file)),'/dev/') !== false) continue; // ignore development utilities
  51.         if ($file->getExtension() == 'php') {
  52.                 $cont = file_get_contents($file);
  53.                 $cont = phpRemoveComments($cont);
  54.                 $cont = str_replace('function _L($str, ...$sprintfArgs) {', '', $cont);
  55.                 $strings = get_php_L_strings($cont);
  56.                 $strings_test = get_js_L_strings($cont);
  57.  
  58.                 if (serialize($strings) != serialize($strings_test)) {
  59.                         echo "Attention: File ".realpath($file)." ambiguous _L() functions\n";
  60.                 }
  61.  
  62.                 $all_strings = array_merge($all_strings, $strings);
  63.         }
  64.         if ($file->getExtension() == 'js') {
  65.                 $cont = file_get_contents($file);
  66.                 $cont = str_replace('function _L()', '', $cont);
  67.                 $strings = get_js_L_strings($cont);
  68.                 $all_strings = array_merge($all_strings, $strings);
  69.         }
  70. }
  71.  
  72. foreach ($all_strings as $str) {
  73.         test_missing_placeholder($str);
  74. }
  75.  
  76. $all_strings = array_unique($all_strings);
  77. sort($all_strings);
  78.  
  79. // ---
  80.  
  81. foreach ($langs as $lang) {
  82.         $translation_array = array();
  83.         $translation_file = $dir.'/plugins/language/'.$lang.'/messages.xml';
  84.         if (file_exists($translation_file)) {
  85.         $xml = simplexml_load_string(file_get_contents($translation_file));
  86.         if (!$xml) {
  87.                 echo "STOP: Cannot load $translation_file\n";
  88.                 continue;
  89.         }
  90.         foreach ($xml->message as $msg) {
  91.                         $src = trim($msg->source->__toString());
  92.                         $dst = trim($msg->target->__toString());
  93.                         $translation_array[$src] = $dst;
  94.                 }
  95.         }
  96.  
  97.         // ---
  98.  
  99.         echo "Processing ".realpath($translation_file)." ...\n";
  100.  
  101.         $stats_total = 0;
  102.         $stats_translated = 0;
  103.         $stats_not_translated = 0;
  104.  
  105.         $cont = '';
  106.         $cont .= '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";
  107.         $cont .= '<translation'."\n";
  108.         $cont .= '      xmlns="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.4.1"'."\n";
  109.         $cont .= '      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n";
  110.         $cont .= '      xsi:schemaLocation="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.4.1 https://oidplus.viathinksoft.com/oidplus/plugins/messages.xsd">'."\n";
  111.         $cont .= "\n";
  112.         foreach ($all_strings as $string) {
  113.                 $stats_total++;
  114.                 $string = trim($string);
  115.                 $cont .= "      <message>\n";
  116.                 $cont .= "              <source><![CDATA[\n";
  117.                 $cont .= "              $string\n";
  118.                 $cont .= "              ]]></source>\n";
  119.                 if (isset($translation_array[$string]) && !empty($translation_array[$string])) {
  120.                         $translation_array[$string] = trim($translation_array[$string]);
  121.                         $stats_translated++;
  122.                         if (substr_count($string,'%') != substr_count($translation_array[$string],'%')) {
  123.                                 echo "\tAttention: Number of %-Replacements differs at translation of message '$string'\n";
  124.                         }
  125.                         $cont .= "              <target><![CDATA[\n";
  126.                         $cont .= "              ".$translation_array[$string]."\n";
  127.                         $cont .= "              ]]></target>\n";
  128.                         test_missing_placeholder($translation_array[$string]);
  129.                 } else {
  130.                         $stats_not_translated++;
  131.                         $cont .= "              <target><![CDATA[\n";
  132.                         $cont .= "              ]]></target><!-- TODO: TRANSLATE -->\n";
  133.                 }
  134.                 $cont .= "      </message>\n";
  135.         }
  136.         $cont .= "</translation>\n";
  137.         file_put_contents($translation_file, $cont);
  138.  
  139.         echo "\t$stats_total total messages, $stats_translated already translated (".round($stats_translated/$stats_total*100,2)."%), $stats_not_translated not translated (".round($stats_not_translated/$stats_total*100,2)."%)\n";
  140.         echo "\tDone...";
  141. }
  142.  
  143. if (count($langs) > 0) {
  144.         echo "All done!\n";
  145. } else {
  146.         echo "Attention: No language plugins found!\n";
  147. }
  148.  
  149. # ---
  150.  
  151. function get_js_L_strings($cont) {
  152.         // Works with JavaScript and PHP
  153.         $cont = preg_replace('@/\\*.+\\*/@ismU', '', $cont);
  154.         $cont = str_replace('\\"', chr(1), $cont);
  155.         $cont = str_replace("\\'", chr(2), $cont);
  156.         $cont = str_replace("\\\\", "\\", $cont);
  157.         $m = array();
  158.         preg_match_all('@[^_A-Za-z0-9]_L\\(.*(["\'])(.+)\\1@ismU', $cont, $m);
  159.         foreach ($m[2] as &$x) {
  160.                 $x = str_replace(chr(1), '"', $x);
  161.                 $x = str_replace(chr(2), "'", $x);
  162.         }
  163.         return $m[2];
  164. }
  165.  
  166. function get_php_L_strings($cont) {
  167.         // Works only with PHP
  168.         $out = array();
  169.         $tokens = token_get_all($cont);
  170.         $activated = 0;
  171.         foreach ($tokens as $token) {
  172.                 if (is_array($token)) {
  173.                         if (($token[0] == T_STRING) && ($token[1] == '_L')) {
  174.                                 $activated = 1;
  175.                         } else if (($activated == 1) && ($token[0] == T_CONSTANT_ENCAPSED_STRING)) {
  176.                                 $tmp = stripcslashes($token[1]);
  177.                                 $out[] = substr($tmp,1,strlen($tmp)-2);
  178.                                 $activated = 0;
  179.                         }
  180.                 }
  181.         }
  182.         return $out;
  183. }
  184.  
  185. function test_missing_placeholder($test) {
  186.         $max = -1;
  187.         for ($i=99; $i>=1; $i--) {
  188.                 if (strpos($test, '%'.$i) !== false) {
  189.                         $max = $i;
  190.                         break;
  191.                 }
  192.         }
  193.  
  194.         for ($i=1; $i<=$max; $i++) {
  195.                 if (strpos($test, '%'.$i) === false) {
  196.                         echo "Attention: %$i is missing in string '$test'!\n";
  197.                         $max = $i;
  198.                         break;
  199.                 }
  200.         }
  201.  
  202.         $test = preg_replace('@%([1-9][0-9]|%)*@ism', '', $test);
  203.         if (strpos($test,'%') !== false) {
  204.                 echo "Attention: Wrong percentage sign in '$test'!\n";
  205.         }
  206. }
  207.  
  208. # ---
  209.  
  210. function phpRemoveComments($fileStr) {
  211.  
  212.         // https://stackoverflow.com/questions/503871/best-way-to-automatically-remove-comments-from-php-code
  213.  
  214.         $newStr  = '';
  215.  
  216.         $commentTokens = array(T_COMMENT);
  217.  
  218.         if (defined('T_DOC_COMMENT')) $commentTokens[] = T_DOC_COMMENT; // PHP 5
  219.         if (defined('T_ML_COMMENT'))  $commentTokens[] = T_ML_COMMENT;  // PHP 4
  220.  
  221.         $tokens = token_get_all($fileStr);
  222.  
  223.         foreach ($tokens as $token) {
  224.                 if (is_array($token)) {
  225.                         if (in_array($token[0], $commentTokens)) continue;
  226.                         $token = $token[1];
  227.                 }
  228.                 $newStr .= $token;
  229.         }
  230.  
  231.         return $newStr;
  232.  
  233. }
  234.