Language translation using PHP

后端 未结 6 1458
名媛妹妹
名媛妹妹 2021-01-03 13:51

Hi i am devloping sample site in php i need to translate whole website in to persian. how can it possible in php?? I have tried using the following code.. This code will wor

相关标签:
6条回答
  • 2021-01-03 14:06
    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");
    
    0 讨论(0)
  • 2021-01-03 14:10

    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");
    
    0 讨论(0)
  • 2021-01-03 14:11

    @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;
    
    }
    //  ...  //
    
    0 讨论(0)
  • 2021-01-03 14:13

    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
    
    0 讨论(0)
  • 2021-01-03 14:16

    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');
    }
    
    0 讨论(0)
  • 2021-01-03 14:24

    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

    0 讨论(0)
提交回复
热议问题