Language translation using PHP

时间秒杀一切 提交于 2019-12-01 01:00:35

AS per me you can try this method.This method is already implemented in our system and it is working properly.

Make php file of each language and define all the variables and use those variables in pages.

for e.g For english

english.php

$hello="Hello";

persian.php

$hello=html_entity_decode(htmlentities("سلام"));

Now use this variable to page like this.

your_page.php

<label><?php echo $hello; ?></label>

You have load specific language file as per get language variable from URL.

It is better that you have define this language variable into config file.

config.php

if(isset($_GET['lang']) && $_GET['lang']=='persian')
{
   require_once('persian.php');
}
else
{
   require_once('english.php');
}

If I were you, I'd do it like this:

/inc/lang/en.lang.php

define('_HELLO', 'Hello');

/inc/lang/fa.lang.php

define('_HELLO', 'سلام');

index.php

// $_SESSION['lang'] could be 'en', 'fa', etc.
require_once '/inc/lang/' . $_SESSION['lang'] . 'lang.php';

echo _HELLO;

Benchmark: Constants vs. Variables

Here you see why I offered using Constants not Variables:

const.php

echo memory_get_usage() . '<br>';   // output: 674,576

for ($i = 0; $i <= 10000; $i++) {
    define($i, 'abc');
}

echo memory_get_usage() . '<br>';   // output: 994,784

var.php

echo memory_get_usage() . '<br>';   // output: 674,184

for ($i = 0; $i <= 10000; $i++) {
   $$i = 'abc';
}

echo memory_get_usage() . '<br>';   // output: 2,485,176

As i can read from the code, the translator class loads the translation data from en.txt file, if you want have 'fa' translation, just create fa.txt as copy of en.txt with all translations and edit and translate fa.txt to persian...

Hope it helps

From an Perl trans script I extracted the following for 100% free php google translation this function:

See working demo on http://ogena.net

function translate($q, $sl, $tl){

if($s==$e || $s=='' || $e==''){
    return $q;

}
else{
    $res="";

    $qqq=explode(".", $q);

    if(count($qqq)<2){

        @unlink($_SERVER['DOCUMENT_ROOT']."/transes.html");
        copy("http://translate.googleapis.com/translate_a/single?client=gtx&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&sl=".$sl."&tl=".$tl."&hl=hl&q=".urlencode($q), $_SERVER['DOCUMENT_ROOT']."/transes.html");
        if(file_exists($_SERVER['DOCUMENT_ROOT']."/transes.html")){
            $dara=file_get_contents($_SERVER['DOCUMENT_ROOT']."/transes.html");
            $f=explode("\"", $dara);

            $res.= $f[1];
        }
    }
    else{


    for($i=0;$i<(count($qqq)-1);$i++){

        if($qqq[$i]==' ' || $qqq[$i]==''){
        }
        else{
            copy("http://translate.googleapis.com/translate_a/single?client=gtx&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&sl=".$s."&tl=".$e."&hl=hl&q=".urlencode($qqq[$i]), $_SERVER['DOCUMENT_ROOT']."/transes.html");

            $dara=file_get_contents($_SERVER['DOCUMENT_ROOT']."/transes.html");
            @unlink($_SERVER['DOCUMENT_ROOT']."/transes.html");
            $f=explode("\"", $dara);

            $res.= $f[1].". ";
            }
        }
    }
    return $res;
}

}




//sample usage
echo translate("Goede dag dames en heren", "nl", "en");

@rbenmass Thank You :-)

I think it have to be , because it runs good for me :

    /* 
    original from @rbenmass :

    function translate($q, $sl, $tl){

    if($s==$e || $s=='' || $e==''){
        return $q;

    }
     **/

function translate($q, $sl, $tl){

if($sl==$tl || $sl=='' || $tl==''){
    return $q;

}
//  ...  //
original from @rbenmass :

try this:

function translate($q, $sl, $tl){
    $res= file_get_contents("https://translate.googleapis.com/translate_a/single?client=gtx&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&sl=".$sl."&tl=".$tl."&hl=hl&q=".urlencode($q), $_SERVER['DOCUMENT_ROOT']."/transes.html");
    $res=json_decode($res);
    return $res[0][0][0];
}

//example-- 
echo translate("اسمي منتصر الصاوي", "ar", "en");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!