German ( de_DE ) "; foreach (glob(dirname(__FILE__) . '/locale/*_*') as $localename) { if (strpos($langopt, "'" . basename($localename) . "'") === false) { $langopt.=""; } } $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=" "; $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" . explode('_await', explode(' msgid "', $lines[$ilines])[1])[0] . "" . "\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.=""; } } 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="
"; // 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()) . "
"; } 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) . "
"; } echo " 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(''); document.getElementById('sellang').innerHTML+=''; 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+=''; document.getElementById('selword').value=newloc.split('~')[0]; } } } }

Using gettext with PHP

RJM Programming - August, 2024

" . $defdiv . "


Language FromLanguage ToWord/Phrase to Translate
English ( en_GB )
"; 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 "
"; echo gettext("via"); echo "
"; $n = 3; printf(ngettext("%d comment", "%d comments", $n), $n); ?>