[GH-ISSUE #592] No complete translations... #427

Open
opened 2026-03-02 15:58:41 +03:00 by kerem · 0 comments
Owner

Originally created by @EV-soft on GitHub (Jul 6, 2021).
Original GitHub issue: https://github.com/prasathmani/tinyfilemanager/issues/592

The translate.json file contains 26 languages, but none are fully translated.
Most are only about 50% translated.
Therefore, I have created a script that generates a completely updated list of text strings to be translated.
Analysis of the existing translations is also performed.

<?php  ## EV-soft - 2021-07-06 - FILE: translatetool.php
       ## to: Create / Update / Maintain list of translate strings by scanning the source-file(s)
       ## Place this file in the same folder as tinyfilemanager.php and read the file in your browser
    if (!isset($compareCode))
        $compareCode= 'es'; ## Adjust this code to the default language you want to analyze (cant use 'en')
    $d = dir("./"); 
    $total= 0;    $buff= array();   $arrStrings= array(); $longest= 0;  $miss= 0;
    $arrTrans= [];  $arrCode= [];   $jsonSource= 'translation.json';
    echo '<meta charset="utf-8">';
    echo '<br><big>TFM-TranslateTool: </big><br>';
 // echo '<br>Create a complete up to date list of strings to translate, by scanning project files for prefix: "lng(\'" in .php/.htm files in current folder,';
    echo '<br>Create a complete up to date list of strings to translate, by scanning projectfile: <i>tinyfilemanager.php</i> for prefix: "lng(\'"<br><br>';
    echo '<br>Reading the languages from file: <i>'.$jsonSource.'</i>';
    echo '<p style="font-family:courier; font-size:15; ">'; 
    $content = file_get_contents($jsonSource);
    if($content !== FALSE) {
        $lng = json_decode($content, TRUE);
        foreach ($lng["language"] as $key => $value) {
            $code = $value["code"];
            $lang_list[$code] = $value["name"];
            $arrTrans[$code] = $value["translation"];
            $arrCode[]= $code;
        }
    }
    echo 'It contains '. count($arrCode). ' languages.
          <form action="#" method="post">
          <label for="lngCode">Select a code:</label>
          <select id="lngCode" name="lngCode">';
    foreach ($arrCode as $code) echo '<option value="'.$code.'" '.($code==$compareCode ? 'selected ' : '').' >'.$code.'</option>';
    echo '</select>
          <input type="submit" name="submit" value="Analyze Selected" /> ( Find new codes here: <a href="https://www.science.co.il/language/Codes.php" >ISO</a> )
          </form>';

    if (isset($_POST['submit'])) $compareCode = $_POST['lngCode'];
    while (false !== ($entry = $d->read())) {
    $dir= $entry.'/';
    if (is_dir($entry) ) {
        $files = scandir($dir);
        if ($files)
        foreach ($files as $source) { $count= 0;  $search= "lng('";
            if (($source!=='.') and ($source!=='..') 
                and ($source=='tinyfilemanager.php') )
             // and (!strpos($source,'.bak')) and (!strpos($source,'translatetool.php')) and (!strpos($source,'.csv')) 
             // and ((strpos($source,'.php')) or (strpos($source,'.htm'))) )    # Only for projects with multible source-files.
                {   $lines = file($dir.$source); 
                    foreach ($lines as $line_num => $line) {
                    $line= ' '.$line;
                    if ($a=strpos($line,$search)) {
                            if ((strpos($source,'.php')) or (strpos($source,'.htm'))) {
                            $str= $line;
                            while (strpos($str,$search)) {
                                    $a= strpos($str,$search);  $str= substr($str,$a+5);  $b= strpos($str,"')");
                                    $str= html_entity_decode($str);
                                    $str= strip_tags($str);
                                    $longest= max($longest,strlen(utf8_decode(substr($str,0,$b))));
                                    $f= substr($str,0,$b);
                                    $arrStrings[] = ['"'.$f.'"'];
                                } 
                            } $count++; $total++;
                        }
                    }
                    $count= substr('000'.$count,-3);
                    if ($count>0) $buff[] = 'Total: '.$count.' found : "<font color=red>'.$search.'</font>" in file <i>'.$dir.'</i><b>'.$source.'</b><br>';
                } 
            }
        }
    }
    echo '</p>';
    $d->close();
    foreach ($buff as $buf) {echo $buf;};
 // echo '<br>Total: '.$total. ' found: <i>'.$search.'</i> in the scanned files<br>';
    $arrStrings= array_unique($arrStrings, SORT_REGULAR);
    sort($arrStrings);
    $translated= count($arrTrans[$compareCode]);
    echo 'Current translated strings: '.$translated.' (<b>'. round(100 * $translated / count($arrStrings)).' %</b>)<br>';
    echo '<br>Sorted list without duplicates for code: <i>'.$compareCode.'</i>';
    echo '<br><p style="font-family:courier; font-size:11; ">';
echo '<hr><pre>### The file-header: ###
{
  "appName": "Tiny File Manager",
  "version": "2.4.6",
  "language": [<hr>### The language analysed: ###
    { "name": "'.$lang_list[$compareCode]. /* english name for the language */
      '",
      "code": "'.$compareCode. /* language 2-letter ISO code [https://www.science.co.il/language/Codes.php] */
      '",
      "native": "unused",'. /* national name for the language */
      '
      "author": "unused",
      "revised": "unused",
      "translation": {';
        $count= count($arrStrings);     $ix= 1;
        foreach ($arrStrings as $string) {
            if (strlen($string[0])>3) {
                echo '<br>'.str_repeat("&nbsp;",8).$string[0].':'.
                str_repeat("&nbsp;",$longest+3-strlen(utf8_decode(substr($string[0],0))));
            if (array_key_exists(trim($string[0],'"'),$arrTrans[$compareCode])) {
                echo '"'.$arrTrans[$compareCode][trim($string[0],'"')].'"'; 
                $miss++;
            }
            else echo '<font color=red>""</font>';  // echo '"<font color=red>Missing_native_translated_string</font>",';
            if ($ix++ < $count) echo ',';
            }};
echo '
      }
    },<hr>### All other languages follows: ###<font color=red>
    {... Insert all other languages here ...}</font>
  ]
}
<hr>### EOF: ###
</pre>';
    echo '</p>RESUME:';
    echo '<br>Total: '.count($arrStrings).' unic strings i the sorted list. Longest string is on  '.$longest.' chars.';
    echo '<br>You can copy - paste this list to your editor.';
    echo '<br><font color=red>Remember to escape the char: " </font>with a slash like this: \" if it occurs on translated text !';
    echo '<br>The lines with ### is comment-lines! It MUST NOT be copyed to the .json file<br>';
    echo '<br>When you have a complete error-free list, please share it on <a href="https://github.com/prasathmani/tinyfilemanager">Github</a><br><br>';
    echo '<hr>Here follows a list that can be used in Google translate:<br>';
    foreach ($arrStrings as $string) { if (strlen($string[0])>3) { echo '<br>'.$string[0]; } };
    echo '<br><br><br>';
?>

You can copy/paste the code to file: translatetool.php and save it in the same folder as tinyfilemanager.php and translation.json

Originally created by @EV-soft on GitHub (Jul 6, 2021). Original GitHub issue: https://github.com/prasathmani/tinyfilemanager/issues/592 The translate.json file contains 26 languages, but none are fully translated. Most are only about 50% translated. Therefore, I have created a script that generates a completely updated list of text strings to be translated. Analysis of the existing translations is also performed. ``` <?php ## EV-soft - 2021-07-06 - FILE: translatetool.php ## to: Create / Update / Maintain list of translate strings by scanning the source-file(s) ## Place this file in the same folder as tinyfilemanager.php and read the file in your browser if (!isset($compareCode)) $compareCode= 'es'; ## Adjust this code to the default language you want to analyze (cant use 'en') $d = dir("./"); $total= 0; $buff= array(); $arrStrings= array(); $longest= 0; $miss= 0; $arrTrans= []; $arrCode= []; $jsonSource= 'translation.json'; echo '<meta charset="utf-8">'; echo '<br><big>TFM-TranslateTool: </big><br>'; // echo '<br>Create a complete up to date list of strings to translate, by scanning project files for prefix: "lng(\'" in .php/.htm files in current folder,'; echo '<br>Create a complete up to date list of strings to translate, by scanning projectfile: <i>tinyfilemanager.php</i> for prefix: "lng(\'"<br><br>'; echo '<br>Reading the languages from file: <i>'.$jsonSource.'</i>'; echo '<p style="font-family:courier; font-size:15; ">'; $content = file_get_contents($jsonSource); if($content !== FALSE) { $lng = json_decode($content, TRUE); foreach ($lng["language"] as $key => $value) { $code = $value["code"]; $lang_list[$code] = $value["name"]; $arrTrans[$code] = $value["translation"]; $arrCode[]= $code; } } echo 'It contains '. count($arrCode). ' languages. <form action="#" method="post"> <label for="lngCode">Select a code:</label> <select id="lngCode" name="lngCode">'; foreach ($arrCode as $code) echo '<option value="'.$code.'" '.($code==$compareCode ? 'selected ' : '').' >'.$code.'</option>'; echo '</select> <input type="submit" name="submit" value="Analyze Selected" /> ( Find new codes here: <a href="https://www.science.co.il/language/Codes.php" >ISO</a> ) </form>'; if (isset($_POST['submit'])) $compareCode = $_POST['lngCode']; while (false !== ($entry = $d->read())) { $dir= $entry.'/'; if (is_dir($entry) ) { $files = scandir($dir); if ($files) foreach ($files as $source) { $count= 0; $search= "lng('"; if (($source!=='.') and ($source!=='..') and ($source=='tinyfilemanager.php') ) // and (!strpos($source,'.bak')) and (!strpos($source,'translatetool.php')) and (!strpos($source,'.csv')) // and ((strpos($source,'.php')) or (strpos($source,'.htm'))) ) # Only for projects with multible source-files. { $lines = file($dir.$source); foreach ($lines as $line_num => $line) { $line= ' '.$line; if ($a=strpos($line,$search)) { if ((strpos($source,'.php')) or (strpos($source,'.htm'))) { $str= $line; while (strpos($str,$search)) { $a= strpos($str,$search); $str= substr($str,$a+5); $b= strpos($str,"')"); $str= html_entity_decode($str); $str= strip_tags($str); $longest= max($longest,strlen(utf8_decode(substr($str,0,$b)))); $f= substr($str,0,$b); $arrStrings[] = ['"'.$f.'"']; } } $count++; $total++; } } $count= substr('000'.$count,-3); if ($count>0) $buff[] = 'Total: '.$count.' found : "<font color=red>'.$search.'</font>" in file <i>'.$dir.'</i><b>'.$source.'</b><br>'; } } } } echo '</p>'; $d->close(); foreach ($buff as $buf) {echo $buf;}; // echo '<br>Total: '.$total. ' found: <i>'.$search.'</i> in the scanned files<br>'; $arrStrings= array_unique($arrStrings, SORT_REGULAR); sort($arrStrings); $translated= count($arrTrans[$compareCode]); echo 'Current translated strings: '.$translated.' (<b>'. round(100 * $translated / count($arrStrings)).' %</b>)<br>'; echo '<br>Sorted list without duplicates for code: <i>'.$compareCode.'</i>'; echo '<br><p style="font-family:courier; font-size:11; ">'; echo '<hr><pre>### The file-header: ### { "appName": "Tiny File Manager", "version": "2.4.6", "language": [<hr>### The language analysed: ### { "name": "'.$lang_list[$compareCode]. /* english name for the language */ '", "code": "'.$compareCode. /* language 2-letter ISO code [https://www.science.co.il/language/Codes.php] */ '", "native": "unused",'. /* national name for the language */ ' "author": "unused", "revised": "unused", "translation": {'; $count= count($arrStrings); $ix= 1; foreach ($arrStrings as $string) { if (strlen($string[0])>3) { echo '<br>'.str_repeat("&nbsp;",8).$string[0].':'. str_repeat("&nbsp;",$longest+3-strlen(utf8_decode(substr($string[0],0)))); if (array_key_exists(trim($string[0],'"'),$arrTrans[$compareCode])) { echo '"'.$arrTrans[$compareCode][trim($string[0],'"')].'"'; $miss++; } else echo '<font color=red>""</font>'; // echo '"<font color=red>Missing_native_translated_string</font>",'; if ($ix++ < $count) echo ','; }}; echo ' } },<hr>### All other languages follows: ###<font color=red> {... Insert all other languages here ...}</font> ] } <hr>### EOF: ### </pre>'; echo '</p>RESUME:'; echo '<br>Total: '.count($arrStrings).' unic strings i the sorted list. Longest string is on '.$longest.' chars.'; echo '<br>You can copy - paste this list to your editor.'; echo '<br><font color=red>Remember to escape the char: " </font>with a slash like this: \" if it occurs on translated text !'; echo '<br>The lines with ### is comment-lines! It MUST NOT be copyed to the .json file<br>'; echo '<br>When you have a complete error-free list, please share it on <a href="https://github.com/prasathmani/tinyfilemanager">Github</a><br><br>'; echo '<hr>Here follows a list that can be used in Google translate:<br>'; foreach ($arrStrings as $string) { if (strlen($string[0])>3) { echo '<br>'.$string[0]; } }; echo '<br><br><br>'; ?> ``` You can copy/paste the code to file: translatetool.php and save it in the same folder as tinyfilemanager.php and translation.json
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tinyfilemanager#427
No description provided.