Subversion Repositories oidplus

Rev

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

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