Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

language translation in php

How is language translation handled in PHP? I need to change the language of a site based on user preferences. Also, I may not have access to the client source code. At most, I could upload a folder of my own and do the dynamic translation myself.

As far as I know, the basic process would be: Send Language 1 output to global function -> translate -> Output to screen.

I need someone to help me out with the specifics in PHP.

Are there any standard implementations that I am unaware of? Has anybody done such a thing and if so, how did you do it?

Thanks.

like image 502
Gaurav Sharma Avatar asked Jul 22 '09 06:07

Gaurav Sharma


People also ask

How to change language in PHP code?

This tutorial will show you how to do just that, very simply create language files and call a specific language on request. First create a folder called 'lang' in this folder is there all the languages will be stored. Create a new file in the lang folder one for each language for example eng. php for english fre.

How to add localization in PHP?

php if (! isset($_GET['langID'])) $lang = 'en_US'; else $lang = $_GET['langID']; putenv("LANG=". $lang); setlocale(LC_ALL, $lang); $domain = "messages"; bindtextdomain($domain, "locale"); textdomain($domain); echo gettext("welcome"); ?>

What is PHP gettext?

gettext — Lookup a message in the current domain. ngettext — Plural version of gettext. textdomain — Sets the default domain.

How do I translate HTML into another language?

In your browser, go to Google Translate. At the top, click Websites. We recommend setting the original language to “Detect language.” In the “Website,” enter a URL.


3 Answers

I've been looking for an answer to this question as well and even though I couldn't solve it with php I think it can be done with google's language api.

There appears to be a standard way of doing what you want to in php, I think it's what wordpress uses http://codex.wordpress.org/Translating_WordPress and it's very similar to you making your own dynamic translation system, you can also do this with zend apparently.

But going back to the google api, what you could do is asking the user their language of preference and save that variable either in a cookie, session, or database, then pass that variable to the api and let it handle the translation.

like image 102
SoftwareDev Avatar answered Nov 15 '22 16:11

SoftwareDev


As google stop this service but you can still use this service as a free service.Put this code in your server(www) and run this file.

<?php

function curl($url,$params = array(),$is_coockie_set = false)
{

if(!$is_coockie_set){
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
}

$str = ''; $str_arr= array();
foreach($params as $key => $value)
{
$str_arr[] = urlencode($key)."=".urlencode($value);
}
if(!empty($str_arr))
$str = '?'.implode('&',$str_arr);

/* STEP 3. visit cookiepage.php */

$Url = $url.$str;

$ch = curl_init ($Url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec ($ch);
return $output;
}

function Translate($word,$conversion = 'hi_to_en')
{
$word = urlencode($word);
// dutch to english
if($conversion == 'nl_to_en')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=nl&tl=en&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';
 //english to arabic

 if($conversion=='en_to_ar')
 {
     $url = 'http://translate.google.co.in/translate_a/t?client=t&text='.$word.'&sl=en&tl=ar&hl=en&sc=2&ie=UTF-8&oe=UTF-8&prev=btn&ssel=3&tsel=4&q=free%20translate%20api';
 }
// english to hindi
if($conversion == 'en_to_hi')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=en&tl=hi&ie=UTF-8&oe=UTF-8&multires=1&otf=1&ssel=3&tsel=3&sc=1';

// hindi to english
if($conversion == 'hi_to_en')
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=hi&tl=en&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&trs=1&ssel=3&tsel=6&sc=1';

//$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=nl&tl=en&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';

$name_en = curl($url);

$name_en = explode('"',$name_en);
return  $name_en[1];
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
echo "<br><br> Hindi To English <br>";
echo  Translate('कानूनी नोटिस: यह गूगल के अनुवादक सेवाओं की एक दुरुपयोग है, आप इस के लिए भुगतान करना होगा.');
echo "<br><br> English To Hindi <br> ";
echo  Translate('legal notice: This is an abuse of google translator services ,  you must pay for this.','en_to_hi');
echo "<br><br> Dutch To English <br>";
echo  Translate('Disclaimer: Dit is een misbruik van Google Translator diensten, moet u betalen.','nl_to_en');
echo "<br><br> English To Arabic<br>";
echo 'hii how are you<br>';
echo  Translate('hii how are you','en_to_ar');

echo "<br><br> Just Kidding ....... <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g' alt=':)' class='wp-smiley'> ";
?>
</body>
</html>

Currently its translating hindi to english, english to hindi, english to arabic. But you can translate any language to any language supported by Google. You just need to do is goto this page

https://translate.google.co.in/

Now open your firebug or inspect element. select from which language to which language you need to translate.Write something on the box and hit translate button. Now catch the request google send to translate.It looks like this.

'https://translate.google.co.in/translate_a/t?client=t&sl=en&tl=ar&hl=en&sc=2&ie=UTF-8&oe=UTF-8&oc=1&otf=1&ssel=0&tsel=0&q=d'

Now paste the request in the translate function.And add text='.$word.'& after client=t. Change https to http.

After that you need to call the translate function from html tag. like this echo Translate('hii how are you','en_to_ar'); First parameter is what do you want to translate.Put you statement over there. Second parameter is short form of which language to which language you want to translate. You can get the short form from the request you get from google translate.

check this client=t&sl=en&tl=ar&hl=en&..sl & hl= en(english) and tl=ar(arabic). Now you are ready to call translate function.

Give the credit to this. Enjoy translating.

like image 37
Kingshuk Deb Avatar answered Nov 15 '22 16:11

Kingshuk Deb


You can use this idea: http://www.phpcollection.com/spell-check-php-yahoo-api.html

like image 27
inakiabt Avatar answered Nov 15 '22 15:11

inakiabt