Subversion Repositories oidplus

Rev

Rev 635 | 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. // 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. $dir = __DIR__ . '/../../';
  25.  
  26. // ---
  27.  
  28. $langs = array();
  29. $tmp = glob($dir.'/plugins/'.'*'.'/language/'.'*'.'/messages.xml');
  30. foreach ($tmp as $tmp2) {
  31.         $tmp3 = explode('/', $tmp2);
  32.         $lang = $tmp3[count($tmp3)-2];
  33.         if ($lang == 'enus') continue; // ignore base lang
  34.         $langs[] = $lang;
  35. }
  36.  
  37. // ---
  38.  
  39. $all_strings = array();
  40.  
  41. $it = new RecursiveDirectoryIterator($dir);
  42. foreach(new RecursiveIteratorIterator($it) as $file) {
  43.         if (strpos(str_replace('\\','/',realpath($file)),'/vendor/') !== false) continue; // ignore third-party-code
  44.         if (strpos(str_replace('\\','/',realpath($file)),'/dev/') !== false) continue; // ignore development utilities
  45.         if ($file->getExtension() == 'php') {
  46.                 $cont = file_get_contents($file);
  47.                 $cont = str_replace('function _L($str, ...$sprintfArgs) {', '', $cont);
  48.                 $strings = get_php_L_strings($cont);
  49.                 $all_strings = array_merge($all_strings, $strings);
  50.         }
  51.         if ($file->getExtension() == 'js') {
  52.                 $cont = file_get_contents($file);
  53.                 $cont = str_replace('function _L()', '', $cont);
  54.                 $strings = get_js_L_strings($cont);
  55.                 $all_strings = array_merge($all_strings, $strings);
  56.         }
  57. }
  58.  
  59. $all_strings = array_unique($all_strings);
  60. sort($all_strings);
  61.  
  62. file_put_contents(__DIR__.'/.proof_enus.txt', implode("\r\n\r\n", $all_strings));
  63. echo "Done: enus\n";
  64.  
  65. // ---
  66.  
  67. foreach ($langs as $lang) {
  68.         $all_strings = array();
  69.         $translation_files = glob($dir.'/plugins/'.'*'.'/language/'.$lang.'/messages.xml');
  70.         $translation_file = count($translation_files) > 0 ? $translation_files[0] : null;
  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. /**
  98.  * @param string $cont
  99.  * @return array
  100.  */
  101. function get_js_L_strings(string $cont): array {
  102.         // Works with JavaScript and PHP
  103.         $cont = preg_replace('@/\\*.+\\*/@ismU', '', $cont);
  104.         $cont = str_replace('\\"', chr(1), $cont);
  105.         $cont = str_replace("\\'", chr(2), $cont);
  106.         $cont = str_replace("\\\\", "\\", $cont);
  107.         $m = array();
  108.         preg_match_all('@[^_A-Za-z0-9]_L\\(.*(["\'])(.+)\\1@ismU', $cont, $m);
  109.         foreach ($m[2] as &$x) {
  110.                 $x = str_replace(chr(1), '"', $x);
  111.                 $x = str_replace(chr(2), "'", $x);
  112.         }
  113.         return $m[2];
  114. }
  115.  
  116. /**
  117.  * @param string $cont
  118.  * @return array
  119.  */
  120. function get_php_L_strings(string $cont): array {
  121.         // Works only with PHP
  122.         $out = array();
  123.         $tokens = token_get_all($cont);
  124.         $activated = 0;
  125.         foreach ($tokens as $token) {
  126.                 if (is_array($token)) {
  127.                         if (($token[0] == T_STRING) && ($token[1] == '_L')) {
  128.                                 $activated = 1;
  129.                         } else if (($activated == 1) && ($token[0] == T_CONSTANT_ENCAPSED_STRING)) {
  130.                                 $tmp = stripcslashes($token[1]);
  131.                                 $out[] = substr($tmp,1,strlen($tmp)-2);
  132.                                 $activated = 0;
  133.                         }
  134.                 }
  135.         }
  136.         return $out;
  137. }
  138.