0

I try to write a script for the translation of language files into new languages. My script can read any type of language files into the form and switch to any available "source language". There are texareas for each string. But I don't know how to save the entire translation (at once or only partially translated) into a new language file. There are various types of language files and there is a way how to read any of them. But this is on TODO only after this first step is solved. The demo shows my intention.

<?php 
//Initializing variable
$new_lang = ""; 
$new_lang = isset($_GET['new_lang']) ? $_GET['new_lang'] : '';
$new_lang = !empty($_GET['new_lang']) ? $_GET['new_lang'] : ''; 
$dir_flag = '';

// check what language files are available
function langList(){
 $dir_langs = 'languages/';
 $dir_flag = './core/flags/';
 // Open a directory, and read its contents
 if(is_dir($dir_langs)){
  if($opendirectory = opendir($dir_langs)){
   while(($lang_file = readdir($opendirectory)) !== false){
    if($lang_file != '.' && $lang_file != '..' && $lang_file != '.php'){
    echo '<a href="?lang='.pathinfo($lang_file)['filename'].'"><img src='.$dir_flag.pathinfo($lang_file)['filename'].'.gif></a> ';
    }
   }
   closedir($opendirectory);
  }
 }
}

// parse language files and assign translation
function lang($string){
  global $base_lang;
  if(isset($base_lang) AND $base_lang!="en" AND file_exists("languages/{$base_lang}.php") && strlen($base_lang) <=3) {
    include("languages/{$base_lang}.php");
      if (isset($lang[$string]) && !empty($lang[$string])) {
          return $lang[$string];
      }
  }
    return $string;
} 
// switch language
$base_lang="en"; // Set the default language
  if(isset($_COOKIE["lang"])){
   $base_lang=$_COOKIE["lang"]; // Get language from cookie
  }

  if(isset($_GET["lang"])){
    setcookie("lang",strip_tags($_GET["lang"]),strtotime('+30 days'),'/', NULL, 0);
    $base_lang=strip_tags($_GET["lang"]); // Or set cookie and new language
  }
   
// write the new_language file
// echo pathinfo($new_lang)['filename'];
$new_lang_file = pathinfo($new_lang)['filename'];
if(!isset($_GET['new_lang'])){
 $msg = "<div class='msg error'>SPECIFY NEW TARGET LANGUAGE FOR TRANSLATION!</div>";
 }else{
 $msg = "<div class='msg success'>THE FILE <span>".$new_lang_file.".php</span> IS READY.</div>";
 }
 if(isset($_GET['submit'])){
 $new_lang = fopen("./languages/".$new_lang_file.".php", 'w') or die("Failed to create file");
  $content= '<?php
     // Name: '.$new_lang.'.php
      $lang = array (';
       foreach($lang as $key => $translat) {
       echo '$lang[\''.$key.'\'] = \''.$translat.'\'; 
      );';
  fwrite($new_lang, $content) or die("Could not write to file"); 
  fclose($new_lang); 
  $msg = "<div class='msg success'>THE FILE <span>".$new_lang_file.".php</span> HAS BEEN WRITTEN.</div>";
 }   
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="./core/stylesheet.css">
<style> pre{font-family:courier; color:#888; font-style: italic}</style>
</head>
<body>
 <div class="wrapper">
  <div class="header">
   <div class="logo">LOGO</div>
   <div class="title">TITLE</div>
   <div class="break"></div>
  </div>
  <div class="break"></div>
   <form action="" name="translator" method="get">
    <fieldset>
    <table border="0">
     <tr><td colspan="3"><?php echo $msg;?></td></tr>
     <tr>
      <td class="key">Select source language:</td>
      <td class="string"><?php echo langList();?></td>
      <td class="trans">Select translatin TO language:<input type="text" name="new_lang" placeholder="??" value="" required></td>
     </tr>
    </table>
    </fieldset>
    <fieldset>

     <?php
     // load language file and put it into a table 
     // with the textarea for translation in the third column
     $data = require 'languages/'.$base_lang.'.php';
     echo "
     <!-- TABLE HEADER -->
     <table border='0'>
     <tr><th class='key'>key</th><th class='string'>string</th><th class='trans'>translate</th></tr>";
     foreach($lang as $key => $string) {
     echo "
     <tr>
     <td>" . $key . "</td><td>". $string . "</td><td><textarea name='".$key."' placeholder='translate'></textarea>
     </tr>";
     }
     echo "</table>";
     ?>
    </fieldset>
    <fieldset>
    <table border="0">
     <tr>
      <td class="buttons">
       <div class="button">
        <input class="button" type="reset" name="reset" value="Reset">
       </div>
       <div class="button">
        <input class="button" type="submit" name="submit" value="Submit">
       </div>
      </td>
     </tr>
    </table>
    </fieldset>
   </form>
  </div>
 </body>
</html>
  • Unrelated to the question: You don't have to check both `isset()` and `!empty()`. https://stackoverflow.com/questions/4559925/why-check-both-isset-and-empty – Barmar Feb 04 '20 at 22:52
  • THX, corrected on localhost. Also uploaded the script with the country code format validation. – Martin Sereday Feb 04 '20 at 23:06

0 Answers0