Subversion Repositories oidplus

Rev

Rev 366 | Rev 511 | 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. // Use this script to create files containing all strings of a specific language
  22. // These files can be used for proofing (e.g. using Microsoft Word)
  23.  
  24.  
  25. $dir = __DIR__ . '/../../';
  26.  
  27. // ---
  28.  
  29. $langs = array();
  30. $tmp = glob($dir.'/plugins/language/*/messages.xml');
  31. foreach ($tmp as $tmp2) {
  32.         $tmp3 = explode('/', $tmp2);
  33.         $lang = $tmp3[count($tmp3)-2];
  34.         if ($lang == 'enus') continue; // ignore base lang
  35.         $langs[] = $lang;
  36. }
  37.  
  38. // ---
  39.  
  40. $all_strings = array();
  41.  
  42. $it = new RecursiveDirectoryIterator($dir);
  43. foreach(new RecursiveIteratorIterator($it) as $file) {
  44.         if (strpos(str_replace('\\','/',realpath($file)),'/3p/') !== false) continue; // ignore third-party-code
  45.         if (strpos(str_replace('\\','/',realpath($file)),'/dev/') !== false) continue; // ignore development utilities
  46.         if ($file->getExtension() == 'php') {
  47.                 $cont = file_get_contents($file);
  48.                 $cont = str_replace('function _L($str, ...$sprintfArgs) {', '', $cont);
  49.                 $strings = get_php_L_strings($cont);
  50.                 $all_strings = array_merge($all_strings, $strings);
  51.         }
  52.         if ($file->getExtension() == 'js') {
  53.                 $cont = file_get_contents($file);
  54.                 $cont = str_replace('function _L()', '', $cont);
  55.                 $strings = get_js_L_strings($cont);
  56.                 $all_strings = array_merge($all_strings, $strings);
  57.         }
  58. }
  59.  
  60. $all_strings = array_unique($all_strings);
  61. sort($all_strings);
  62.  
  63. file_put_contents(__DIR__.'/.proof_enus.txt', implode("\r\n\r\n", $all_strings));
  64. echo "Done: enus\n";
  65.  
  66. // ---
  67.  
  68. foreach ($langs as $lang) {
  69.         $all_strings = array();
  70.         $translation_file = $dir.'/plugins/language/'.$lang.'/messages.xml';
  71.         if (file_exists($translation_file)) {
  72.         $xml = simplexml_load_string(file_get_contents($translation_file));
  73.         if (!$xml) {
  74.                 echo "STOP: Cannot load $translation_file\n";
  75.                 continue;
  76.         }
  77.         foreach ($xml->message as $msg) {
  78.                         $dst = trim($msg->target->__toString());
  79.                         $all_strings[] = $dst;
  80.                 }
  81.         }
  82.  
  83.         sort($all_strings);
  84.  
  85.         file_put_contents(__DIR__.'/.proof_'.$lang.'.txt', implode("\r\n\r\n", $all_strings));
  86.         echo "Done: $lang\n";
  87. }
  88.  
  89. if (count($langs) > 0) {
  90.         echo "All done!\n";
  91. } else {
  92.         echo "Attention: No language plugins found!\n";
  93. }
  94.  
  95. # ---
  96.  
  97. function get_js_L_strings($cont) {
  98.         // Works with JavaScript and PHP
  99.         $cont = preg_replace('@/\\*.+\\*/@ismU', '', $cont);
  100.         $cont = str_replace('\\"', chr(1), $cont);
  101.         $cont = str_replace("\\'", chr(2), $cont);
  102.         $cont = str_replace("\\\\", "\\", $cont);
  103.         $m = array();
  104.         preg_match_all('@[^_A-Za-z0-9]_L\\(.*(["\'])(.+)\\1@ismU', $cont, $m);
  105.         foreach ($m[2] as &$x) {
  106.                 $x = str_replace(chr(1), '"', $x);
  107.                 $x = str_replace(chr(2), "'", $x);
  108.         }
  109.         return $m[2];
  110. }
  111.  
  112. function get_php_L_strings($cont) {
  113.         // Works only with PHP
  114.         $out = array();
  115.         $tokens = token_get_all($cont);
  116.         $activated = 0;
  117.         foreach ($tokens as $token) {
  118.                 if (is_array($token)) {
  119.                         if (($token[0] == T_STRING) && ($token[1] == '_L')) {
  120.                                 $activated = 1;
  121.                         } else if (($activated == 1) && ($token[0] == T_CONSTANT_ENCAPSED_STRING)) {
  122.                                 $tmp = stripcslashes($token[1]);
  123.                                 $out[] = substr($tmp,1,strlen($tmp)-2);
  124.                                 $activated = 0;
  125.                         }
  126.                 }
  127.         }
  128.         return $out;
  129. }
  130.