<?php
// get_text.php
// RJM Programming - August, 2024
// Thanks to brice/axice/be's blurb on https://www.php.net/manual/en/function.setlocale.php

function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return "_await_moderation_" . str_replace(".", "_", str_replace(":", "_", $rma));
}

$whereplace=shell_exec("ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'");
if (strpos(($whereplace . ' ' . $_SERVER['SERVER_ADDR']), '65.254.92.213') !== false) {
     header('Location: https://65.254.95.247/PHP/gettext/get_text.php'); 
     exit; 
}

$onl="";
$langopt="<option value='de_DE'>German ( de_DE )</option>
<option value='fr_FR'>French ( fr_FR )</option>
<option value='it_IT'>Italian ( it_TR )</option>
<option value='es_ES'>Spanish ( es_ES )</option>
";

            foreach (glob(dirname(__FILE__) . '/locale/*_*') as $localename) {
              if (strpos($langopt, "'" . basename($localename) . "'") === false) {
                $langopt.="<option value='" . basename($localename) . "'>" . basename($localename) . "</option>";
              }
            }

$pohead="# Translation of 4.1.x in English (Australia)
# This file is distributed under the same license as the 4.1.x package.
msgid \"\"
msgstr \"\"
\"PO-Revision-Date: 2015-05-19 10:42+1000" . "\\n\"
\"MIME-Version: 1.0" . "\\n\"
\"Content-Type: text/plain; charset=UTF-8" . "\\n\"
\"Content-Transfer-Encoding: 8bit" . "\\n\"
\"Plural-Forms: nplurals=2; plural=(n != 1);" . "\\n\"
\"X-Generator: Poedit 1.7.6" . "\\n\"
\"Project-Id-Version: 4.1.x" . "\\n\"
\"POT-Creation-Date: " . "\\n\"
\"Last-Translator: " . "\\n\"
\"Language-Team: " . "\\n\"
\"Language: de_DE" . "\\n\"

";

$wordopt="<option value='Sidebar'>Sidebar</option>
<option value='Not able to create XMLHttpRequest ... sadly.'>Not able to create XMLHttpRequest ... sadly.</option>
<option value='Similar Meaning'>Similar Meaning</option>
<option value='Opposite Meaning'>Opposite Meaning</option>
<option value='best to tab or click/touch out'>best to tab or click/touch out</option>
<option value='best to finish with keyboard button, not Go button'>best to finish with keyboard button, not Go button</option>
<option value='via'>via</option>
<option value='Results go below'>Results go below</option>
<option value='English Word'>English Word</option>
<option value='French Word'>French Word</option>
<option value='German Word'>German Word</option>
<option value='Italian Word'>Italian Word</option>
<option value='Spanish Word'>Spanish Word</option>
<option value='to Translate to English Word'>to Translate to English Word</option>
<option value='to Translate to French Word'>to Translate to French Word</option>
<option value='to Translate to German Word'>to Translate to German Word</option>
<option value='to Translate to Italian Word'>to Translate to Italian Word</option>
<option value='to Translate to Spanish Word'>to Translate to Spanish Word</option>
<option value='But know the word in ...'>But know the word in ...</option>
<option value='Search'>Search</option>
<option value='Thesaurus'>Thesaurus</option>
<option value='Thesaurus Search'>Thesaurus Search</option>
<option value='English'>English</option>
<option value='French'>French</option>
<option value='German'>German</option>
<option value='Italian'>Italian</option>
<option value='Spanish'>Spanish</option>
<option value='English Word to Translate to English'>English Word to Translate to English</option>
<option value='English Word to Translate to French'>English Word to Translate to French</option>
<option value='English Word to Translate to German'>English Word to Translate to German</option>
<option value='English Word to Translate to Italian'>English Word to Translate to Italian</option>
<option value='English Word to Translate to Spanish'>English Word to Translate to Spanish</option>
<option value='French Word to Translate to French'>French Word to Translate to French</option>
<option value='French Word to Translate to English'>French Word to Translate to English</option>
<option value='French Word to Translate to German'>French Word to Translate to German</option>
<option value='French Word to Translate to Italian'>French Word to Translate to Italian</option>
<option value='French Word to Translate to Spanish'>French Word to Translate to Spanish</option>
<option value='German Word to Translate to French'>German Word to Translate to French</option>
<option value='German Word to Translate to English'>German Word to Translate to English</option>
<option value='German Word to Translate to German'>German Word to Translate to German</option>
<option value='German Word to Translate to Italian'>German Word to Translate to Italian</option>
<option value='German Word to Translate to Spanish'>German Word to Translate to Spanish</option>
<option value='Italian Word to Translate to French'>Italian Word to Translate to French</option>
<option value='Italian Word to Translate to English'>Italian Word to Translate to English</option>
<option value='Italian Word to Translate to German'>Italian Word to Translate to German</option>
<option value='Italian Word to Translate to Italian'>Italian Word to Translate to Italian</option>
<option value='Italian Word to Translate to Spanish'>Italian Word to Translate to Spanish</option>
<option value='Spanish Word to Translate to French'>Spanish Word to Translate to French</option>
<option value='Spanish Word to Translate to English'>Spanish Word to Translate to English</option>
<option value='Spanish Word to Translate to German'>Spanish Word to Translate to German</option>
<option value='Spanish Word to Translate to Italian'>Spanish Word to Translate to Italian</option>
<option value='Spanish Word to Translate to Spanish'>Spanish Word to Translate to Spanish</option>
";

$codeset = "UTF8";  // warning ! not UTF-8 with dash '-'

$newpo='';
if (isset($_GET['msgin']) && isset($_GET['msgstr'])) {
 if (strlen($_GET['msgin']) > 0) {
  $newpo="\n\n msgid \"" . str_replace('+',' ',urldecode($_GET['msgin'])) . server_remote_addr() . "\"\n msgstr \"" . str_replace('+',' ',urldecode($_GET['msgstr'])) . "\"\n\n";
 }
}

$lastfile="";
if (file_exists('/tmp/get_text_moderation.txt')) {
  $lines=explode("\n", file_get_contents('/tmp/get_text_moderation.txt'));
  for ($ilines=0; $ilines<sizeof($lines); $ilines++) {
    if (strpos($lines[$ilines], DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR) !== false) {
      $lastfile=$lines[$ilines];
    } else if (strpos($lines[$ilines], ' msgid ') !== false && strpos($lines[$ilines], '_await') !== false) {
      if (strpos(file_get_contents($lastfile), " msgid " . explode('_await', explode(' msgid ', $lines[$ilines])[1])[0] . '"'  ) !== false) {
       $wordopt.="<option value='" . explode('_await', explode(' msgid "', $lines[$ilines])[1])[0] . "'>" . explode('_await', explode(' msgid "', $lines[$ilines])[1])[0] . "</option>" . "\n";
      }
    }    
  }
}

// for windows compatibility (e.g. xampp) : theses 3 lines are useless for linux systems 

$lang="de_DE";
if (isset($_GET['langin'])) {
  $lang=str_replace('+',' ',urldecode($_GET['langin']));
  if (strpos($langopt, "'" . explode('.', $lang)[0] . "'") === false) {
    if (sizeof(explode('_', $lang)) > 1 && sizeof(explode('_', $lang)) <= 3) {
       if (explode('_', $lang)[0] == strtolower(explode('_', $lang)[0]) && explode('_', $lang)[1] == strtoupper(explode('_', $lang)[1])) {
          if (!file_exists(dirname(__FILE__) . '/locale/' . explode('.', $lang)[0])) {
            exec('mkdir ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . '; ' . 'mkdir ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES');
            //echo " a ";
            file_put_contents(dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po', str_replace('de_DE',explode('.', $lang)[0],$pohead . $newpo));
            //echo " b ";
            //exit;
            if ($newpo != '') { 
             $filepre=dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po' . "\n";
             if (!file_exists('/tmp/get_text_moderation.txt')) {
               file_put_contents('/tmp/get_text_moderation.txt', $filepre . " msgid " . str_replace("\n","",explode(' msgstr ',explode(' msgid ', $newpo)[1])[0]));
             } else {
               $pres=explode(DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR, file_get_contents('/tmp/get_text_moderation.txt'));
               if (strpos(substr($pres[-2 + sizeof($pres)] . DIRECTORY_SEPARATOR,-20,20), DIRECTORY_SEPARATOR . explode('.', $lang)[0] . DIRECTORY_SEPARATOR  ) !== false) {
                 $filepre="";
               }
               file_put_contents('/tmp/get_text_moderation.txt', file_get_contents('/tmp/get_text_moderation.txt') . "\n " . $filepre . " msgid " . str_replace("\n","",explode(' msgstr ',explode(' msgid ', $newpo)[1])[0]));
             }
             $newpo=''; 
            }
            if (file_exists('/usr/local/bin/msgfmt')) {
            exec('/usr/local/bin/msgfmt ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po -o ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.mo 2> x555.x555');
            } else {
            exec('/usr/bin/msgfmt ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po -o ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.mo 2> x555.x555');
            }
            //echo " g ";
            //exit;
            foreach (glob(dirname(__FILE__) . '/locale/*_*') as $localename) {
              if (strpos($langopt, "'" . basename($localename) . "'") === false) {
                $langopt.="<option value='" . basename($localename) . "'>" . basename($localename) . "</option>";
              }
            }
            exit;
          } else if ($newpo != '') {
            //echo " cc " . $newpo;
            //exit;
    file_put_contents(dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po', file_get_contents(dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po') . $newpo);
            //echo " e ";
            
            if (file_exists('/usr/local/bin/msgfmt')) {
            
    exec('/usr/local/bin/msgfmt ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po -o ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.mo 2> x55.x55');
            } else {
    exec('/usr/bin/msgfmt ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po -o ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.mo 2> x55.x55');
            }
            //echo " f ";
            //exit;
             $filepre=dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po' . "\n";
             if (!file_exists('/tmp/get_text_moderation.txt')) {
               file_put_contents('/tmp/get_text_moderation.txt', $filepre . " msgid " . str_replace("\n","",explode(' msgstr ',explode(' msgid ', $newpo)[1])[0]));
             } else {
               $pres=explode(DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR, file_get_contents('/tmp/get_text_moderation.txt'));
               if (strpos(substr($pres[-2 + sizeof($pres)] . DIRECTORY_SEPARATOR,-20,20), DIRECTORY_SEPARATOR . explode('.', $lang)[0] . DIRECTORY_SEPARATOR  ) !== false) {
                 $filepre="";
               }
               file_put_contents('/tmp/get_text_moderation.txt', file_get_contents('/tmp/get_text_moderation.txt') . "\n " . $filepre . " msgid " . str_replace("\n","",explode(' msgstr ',explode(' msgid ', $newpo)[1])[0]));
             }
    $newpo='';
    exit;
  }
       }
    }
  } else if ($newpo != '') {
            //echo " c " . $newpo;
//echo "herex";
//exit;
    file_put_contents(dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po', file_get_contents(dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po') . $newpo);
           // echo " e ";
            
            if (file_exists('/usr/local/bin/msgfmt')) {
            
    exec('/usr/local/bin/msgfmt ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po -o ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.mo 2> x5.x5');
            } else {
    exec('/usr/bin/msgfmt ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po -o ' . dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.mo 2> x5.x5');
            }
            //echo " f ";
            //exit;
             $filepre=dirname(__FILE__) . '/locale/' . explode('.', $lang)[0] . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'messages.po' . "\n";
             if (!file_exists('/tmp/get_text_moderation.txt')) {
               file_put_contents('/tmp/get_text_moderation.txt', $filepre . " msgid " . str_replace("\n","",explode(' msgstr ',explode(' msgid ', $newpo)[1])[0]));
             } else {
               $pres=explode(DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR, file_get_contents('/tmp/get_text_moderation.txt'));
               if (strpos(substr($pres[-2 + sizeof($pres)] . DIRECTORY_SEPARATOR,-20,20), DIRECTORY_SEPARATOR . explode('.', $lang)[0] . DIRECTORY_SEPARATOR  ) !== false) {
                 $filepre="";
               }
               file_put_contents('/tmp/get_text_moderation.txt', file_get_contents('/tmp/get_text_moderation.txt') . "\n " . $filepre . " msgid " . str_replace("\n","",explode(' msgstr ',explode(' msgid ', $newpo)[1])[0]));
             }
    $newpo='';
    exit;
  }
}

putenv('LANG='.$lang.'.'.$codeset);

putenv('LANGUAGE='.$lang.'.'.$codeset);

bind_textdomain_codeset('messages', $codeset);

// set locale

bindtextdomain('messages', dirname(__FILE__) . '/locale/');

setlocale(LC_ALL, $lang.'.'.$codeset);

textdomain('messages');

$word="Sidebar";
if (isset($_GET['wordin'])) {
  $word=str_replace('+',' ',urldecode($_GET['wordin']));
}

$defdiv="<div>";
// Print a test message
if (gettext($word . server_remote_addr()) != ($word . server_remote_addr())) {
if ($word == gettext($word . server_remote_addr())) {
  $onl=" onload=\"if (window.self !== window.top) { moreget=moreget; } else { window.open('//translate.google.com/?sl=en&tl=' + '" . substr($lang,0,2) . "' + '&text=' + encodeURIComponent('" . $word . "') + '&op=translate','_blank','top=100,left=700,width=600,height=600'); } \"";
}
$defdiv.=$word . " translates to $lang as " . gettext($word . server_remote_addr()) . "<br>";
} else {
// Or use the alias _() for gettext()
if ($word == _($word)) {
  $onl=" onload=\"if (window.self !== window.top) { moreget=moreget; } else { window.open('//translate.google.com/?sl=en&tl=' + '" . substr($lang,0,2) . "' + '&text=' + encodeURIComponent('" . $word . "') + '&op=translate','_blank','top=100,left=700,width=600,height=600'); } \"";
}
$defdiv.=$word . " translates to $lang as " . _($word) . "<br></div>";
}

echo "<html>
<head>
<scr" . "ipt type=text/javascript>
 var moreget='';
 
 function addlocale() {
   var newloc=prompt('Please enter a new locale for English translation to ( eg. ja_JP ).', '');
   if (newloc != null) {
     if (newloc.indexOf('_') != -1) {
     //alert(document.URL.split('?')[0].split('#')[0] + '?langin=' + encodeURIComponent(newloc));
        document.getElementById('myloc').src=document.URL.split('?')[0].split('#')[0] + '?langin=' + encodeURIComponent(newloc);
        if (document.getElementById('sellang').innerHTML.toLowerCase().replace('eg. ' + newloc.toLowerCase(),'').indexOf(newloc.toLowerCase()) == -1) {
     //alert('<option value=\"' + newloc + '\">' + newloc + '</option>');
          document.getElementById('sellang').innerHTML+='<option value=\"' + newloc + '\">' + newloc + '</option>';
          document.getElementById('sellang').value=newloc;
        } //else {
        //alert(document.getElementById('sellang').innerHTML);
        //}
     }
   }
 }
 
 function addwordphrase() {
   var newloc=prompt('Please enter a new word/phrase for English translation then ~ then translation to ' + document.getElementById('sellang').value, '');
   if (newloc != null) {
     if (newloc.indexOf('~') != -1) {
     //alert(document.URL.split('?')[0].split('#')[0] + '?msgin=' + encodeURIComponent(newloc).replace(encodeURIComponent('~'), '&langin=' + encodeURIComponent(document.getElementById('sellang').value) + '&msgstr='));
        if (1 == 11) {
        window.open(document.URL.split('?')[0].split('#')[0] + '?msgin=' + encodeURIComponent(newloc).replace(encodeURIComponent('~'), '&langin=' + encodeURIComponent(document.getElementById('sellang').value) + '&msgstr='), '_blank');
        } else {
        document.getElementById('myloc').src=document.URL.split('?')[0].split('#')[0] + '?msgin=' + encodeURIComponent(newloc).replace(encodeURIComponent('~'), '&langin=' + encodeURIComponent(document.getElementById('sellang').value) + '&msgstr=');
        }
        if (document.getElementById('selword').innerHTML.toLowerCase().indexOf('>' + newloc.split('~')[0].toLowerCase() + '<') == -1) {
          document.getElementById('selword').innerHTML+='<option value=\"' + newloc.split('~')[0] + '\">' + newloc.split('~')[0] + '</option>';
          document.getElementById('selword').value=newloc.split('~')[0];
        }
     }
   }
}
 
</scr" . "ipt>
</head>
<body" . $onl . ">
<h1>Using gettext with PHP</h1>
<h3>RJM Programming - August, 2024</h3>
" . $defdiv . "
<br><br><br><table cellpadding=10 border=5>
<tr><th>Language From</th><th>Language To</th><th>Word/Phrase to Translate</th></tr>
<tr><td>English ( en_GB )</td><td>
<select id=sellang onchange=\"if (this.value == ' ') {  addlocale();  } if (this.value.trim().length > 0 && document.getElementById('selword').value.length != 0) { location.href=document.URL.split('?')[0].split('#')[0] + ('?wordin=' + document.getElementById('selword').value + '&langin=').replace('=&','=Sidebar&') + this.value + moreget; } \">
<option value=''>Language To ...</option>
<option value=' '>Your Locale ( eg. ja_JP )</option>
" . $langopt . "</select>
</td><td>
<select id=selword onchange=\"if (this.value == ' ') {  addwordphrase();  } if (this.value.trim().length > 0 && document.getElementById('sellang').value.length != 0) { location.href=document.URL.split('?')[0].split('#')[0] + ('?langin=' + document.getElementById('sellang').value + '&wordin=').replace('=&','=de_DE&') + this.value + moreget; } \">
<option value=''>Word/Phrase from ...</option>
<option value=' '>Your Word/Phrase</option>
" . $wordopt . "</select>
</td></tr>
</table>
<iframe id=myloc style=display:none; src=./get_text.php></iframe>
</body>
</html>";

exit;

putenv('LC_ALL=de_DE');
setlocale(LC_ALL, 'de_DE');
// Specify location of translation tables
bindtextdomain("messages", "/home/rjmprogr/public_html/PHP/gettext/locale/de_DE/LC_MESSAGES");
// Choose domain
textdomain("messages");
// Translation is looking for in ./locale/de_DE/LC_MESSAGES/messages.mo now
// Print a test message
echo gettext("Sidebar");
// Or use the alias _() for gettext()
echo _("Sidebar");
exit;

require_once("localization.php"); 
$results = gettext("Sidebar");
if ($results === "Sidebar") {
    echo "Original English was returned. Something wrong ... \n";
}
echo $results . "\n";
$results = gettext("Similar Meaning");
if ($results === "Similar Meaning") {
    echo "Original English was returned. Something wrong ... \n";
}
echo $results . "\n";
echo "<br>";
echo gettext("via");
echo "<br>";
$n = 3;
printf(ngettext("%d comment", "%d comments", $n), $n);
?>
