Subversion Repositories oidplus

Rev

Rev 386 | Rev 506 | 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 2020 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.  
  31. $dir = __DIR__ . '/../../';
  32.  
  33. // ---
  34.  
  35. $langs = array();
  36. $tmp = glob($dir.'/plugins/language/*/messages.xml');
  37. foreach ($tmp as $tmp2) {
  38.         $tmp3 = explode('/', $tmp2);
  39.         $lang = $tmp3[count($tmp3)-2];
  40.         if ($lang == 'enus') continue; // ignore base lang
  41.         $langs[] = $lang;
  42. }
  43.  
  44. // ---
  45.  
  46. $all_strings = array();
  47.  
  48. $it = new RecursiveDirectoryIterator($dir);
  49. foreach(new RecursiveIteratorIterator($it) as $file) {
  50.         if (strpos(str_replace('\\','/',realpath($file)),'/3p/') !== false) continue; // ignore third-party-code
  51.         if (strpos(str_replace('\\','/',realpath($file)),'/dev/') !== false) continue; // ignore development utilities
  52.         if ($file->getExtension() == 'php') {
  53.                 $cont = file_get_contents($file);
  54.                 $cont = phpRemoveComments($cont);
  55.                 $cont = str_replace('function _L($str, ...$sprintfArgs) {', '', $cont);
  56.                 $strings = get_php_L_strings($cont);
  57.                 $strings_test = get_js_L_strings($cont);
  58.  
  59.                 if (serialize($strings) != serialize($strings_test)) {
  60.                         echo "Attention: File ".realpath($file)." ambiguous _L() functions\n";
  61.                 }
  62.  
  63.                 $all_strings = array_merge($all_strings, $strings);
  64.         }
  65.         if ($file->getExtension() == 'js') {
  66.                 $cont = file_get_contents($file);
  67.                 $cont = str_replace('function _L()', '', $cont);
  68.                 $strings = get_js_L_strings($cont);
  69.                 $all_strings = array_merge($all_strings, $strings);
  70.         }
  71. }
  72.  
  73. foreach ($all_strings as $str) {
  74.         test_missing_placeholder($str);
  75. }
  76.  
  77. $all_strings = array_unique($all_strings);
  78. sort($all_strings);
  79.  
  80. // ---
  81.  
  82. foreach ($langs as $lang) {
  83.         $translation_array = array();
  84.         $translation_file = $dir.'/plugins/language/'.$lang.'/messages.xml';
  85.         if (file_exists($translation_file)) {
  86.         $xml = simplexml_load_string(file_get_contents($translation_file));
  87.         if (!$xml) {
  88.                 echo "STOP: Cannot load $translation_file\n";
  89.                 continue;
  90.         }
  91.         foreach ($xml->message as $msg) {
  92.                         $src = trim($msg->source->__toString());
  93.                         $dst = trim($msg->target->__toString());
  94.                         $translation_array[$src] = $dst;
  95.                 }
  96.         }
  97.  
  98.         // ---
  99.  
  100.         echo "Processing ".realpath($translation_file)." ...\n";
  101.  
  102.         $stats_total = 0;
  103.         $stats_translated = 0;
  104.         $stats_not_translated = 0;
  105.  
  106.         $cont = '';
  107.         $cont .= '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";
  108.         $cont .= '<translation'."\n";
  109.         $cont .= '      xmlns="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.4.1"'."\n";
  110.         $cont .= '      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n";
  111.         $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";
  112.         $cont .= "\n";
  113.         foreach ($all_strings as $string) {
  114.                 $stats_total++;
  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.                         $stats_translated++;
  121.                         if (substr_count($string,'%') != substr_count($translation_array[$string],'%')) {
  122.                                 echo "\tAttention: Number of %-Replacements differs at translation of message '$string'\n";
  123.                         }
  124.                         $cont .= "              <target><![CDATA[\n";
  125.                         $cont .= "              ".$translation_array[$string]."\n";
  126.                         $cont .= "              ]]></target>\n";
  127.                         test_missing_placeholder($translation_array[$string]);
  128.                 } else {
  129.                         $stats_not_translated++;
  130.                         $cont .= "              <target><![CDATA[\n";
  131.                         $cont .= "              ]]></target><!-- TODO: TRANSLATE -->\n";
  132.                 }
  133.                 $cont .= "      </message>\n";
  134.         }
  135.         $cont .= "</translation>\n";
  136.         file_put_contents($translation_file, $cont);
  137.  
  138.         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";
  139.         echo "\tDone...";
  140. }
  141.  
  142. if (count($langs) > 0) {
  143.         echo "All done!\n";
  144. } else {
  145.         echo "Attention: No language plugins found!\n";
  146. }
  147.  
  148. # ---
  149.  
  150. function get_js_L_strings($cont) {
  151.         // Works with JavaScript and PHP
  152.         $cont = preg_replace('@/\\*.+\\*/@ismU', '', $cont);
  153.         $cont = str_replace('\\"', chr(1), $cont);
  154.         $cont = str_replace("\\'", chr(2), $cont);
  155.         $cont = str_replace("\\\\", "\\", $cont);
  156.         $m = array();
  157.         preg_match_all('@[^_A-Za-z0-9]_L\\(.*(["\'])(.+)\\1@ismU', $cont, $m);
  158.         foreach ($m[2] as &$x) {
  159.                 $x = str_replace(chr(1), '"', $x);
  160.                 $x = str_replace(chr(2), "'", $x);
  161.         }
  162.         return $m[2];
  163. }
  164.  
  165. function get_php_L_strings($cont) {
  166.         // Works only with PHP
  167.         $out = array();
  168.         $tokens = token_get_all($cont);
  169.         $activated = 0;
  170.         foreach ($tokens as $token) {
  171.                 if (is_array($token)) {
  172.                         if (($token[0] == T_STRING) && ($token[1] == '_L')) {
  173.                                 $activated = 1;
  174.                         } else if (($activated == 1) && ($token[0] == T_CONSTANT_ENCAPSED_STRING)) {
  175.                                 $tmp = stripcslashes($token[1]);
  176.                                 $out[] = substr($tmp,1,strlen($tmp)-2);
  177.                                 $activated = 0;
  178.                         }
  179.                 }
  180.         }
  181.         return $out;
  182. }
  183.  
  184. function test_missing_placeholder($test) {
  185.         $max = -1;
  186.         for ($i=99; $i>=1; $i--) {
  187.                 if (strpos($test, '%'.$i) !== false) {
  188.                         $max = $i;
  189.                         break;
  190.                 }
  191.         }
  192.  
  193.         for ($i=1; $i<=$max; $i++) {
  194.                 if (strpos($test, '%'.$i) === false) {
  195.                         echo "Attention: %$i is missing in string '$test'!\n";
  196.                         $max = $i;
  197.                         break;
  198.                 }
  199.         }
  200.  
  201.         $test = preg_replace('@%([1-9][0-9]|%)*@ism', '', $test);
  202.         if (strpos($test,'%') !== false) {
  203.                 echo "Attention: Wrong percentage sign in '$test'!\n";
  204.         }
  205. }
  206.  
  207. # ---
  208.  
  209. function phpRemoveComments($fileStr) {
  210.  
  211.         // https://stackoverflow.com/questions/503871/best-way-to-automatically-remove-comments-from-php-code
  212.  
  213.         $newStr  = '';
  214.  
  215.         $commentTokens = array(T_COMMENT);
  216.  
  217.         if (defined('T_DOC_COMMENT')) $commentTokens[] = T_DOC_COMMENT; // PHP 5
  218.         if (defined('T_ML_COMMENT'))  $commentTokens[] = T_ML_COMMENT;  // PHP 4
  219.  
  220.         $tokens = token_get_all($fileStr);
  221.  
  222.         foreach ($tokens as $token) {
  223.                 if (is_array($token)) {
  224.                         if (in_array($token[0], $commentTokens)) continue;
  225.                         $token = $token[1];
  226.                 }
  227.                 $newStr .= $token;
  228.         }
  229.  
  230.         return $newStr;
  231.  
  232. }
  233.