php php中的语言翻译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1163454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
language translation in php
提问by Gaurav Sharma
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.
PHP 中如何处理语言翻译?我需要根据用户偏好更改站点的语言。此外,我可能无法访问客户端源代码。我最多可以上传一个自己的文件夹,自己做动态翻译。
As far as I know, the basic process would be: Send Language 1 output to global function -> translate -> Output to screen.
据我所知,基本过程是:将语言 1 输出发送到全局函数 -> 翻译 -> 输出到屏幕。
I need someone to help me out with the specifics in PHP.
我需要有人帮助我解决 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.
谢谢。
回答by SoftwareDev
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.
我也一直在寻找这个问题的答案,即使我无法用 php 解决它,我认为它可以用谷歌的语言 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_WordPressand it's very similar to you making your own dynamic translation system, you can also do this with zendapparently.
似乎有一种标准的方式可以在 php 中做你想做的事,我认为这是 wordpress 使用http://codex.wordpress.org/Translating_WordPress 的方式,它与你制作自己的动态翻译系统非常相似,你也可以这样做这显然与zend。
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.
但是回到google api,您可以做的是询问用户他们的偏好语言并将该变量保存在 cookie、会话或数据库中,然后将该变量传递给 api 并让它处理翻译。
回答by Kingshuk Deb
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.
由于谷歌停止此服务,但您仍然可以将此服务用作免费服务。将此代码放入您的服务器(www)并运行此文件。
<?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
目前它的翻译印地语到英语,英语到印地语,英语到阿拉伯语。但是你可以translate any language to any language supported by Google。你只需要转到这个页面
https://translate.google.co.in/
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.
现在将请求粘贴到翻译函数中。并在text='.$word.'&之后添加client=t。将 https 更改为 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 requestyou get from google translate.
之后,您需要从 html 标签调用 translate 函数。像这样echo Translate('hii how are you','en_to_ar');第一个参数是你想翻译什么。把你的语句放在那里。第二个参数是您想要翻译成哪种语言的简短形式。你可以从request你从谷歌翻译得到的简短表格。
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.
检查这个client=t&sl=en&tl=ar&hl=en&..sl & hl= en(english) 和 tl=ar(arabic)。现在您已准备好调用翻译函数。
Give the credit to this. Enjoy translating.
归功于此。享受翻译。
回答by AjayR
why dont you use google API for this in PHP. http://code.google.com/p/gtranslate-api-php/
为什么不在 PHP 中为此使用 google API。 http://code.google.com/p/gtranslate-api-php/
Specific to your question, you have two options 1. Change whole web page (translate) Or 2. Translate only specific portion like user data.
针对您的问题,您有两个选择:1. 更改整个网页(翻译)或 2. 仅翻译特定部分,如用户数据。
The second one is important when you dont want to change the menus, page title etc.
当您不想更改菜单、页面标题等时,第二个很重要。
In this case, you need to store the content in MySQL (like CMS) and display them after translating from the original.
在这种情况下,您需要将内容存储在 MySQL(如 CMS)中,并在从原始翻译后显示它们。
回答by inakiabt
You can use this idea: http://www.phpcollection.com/spell-check-php-yahoo-api.html
你可以使用这个想法:http: //www.phpcollection.com/spell-check-php-yahoo-api.html
回答by sehhorng
Try http://www.phptranslator.comIt can translate your website in the background using Google API.
试试http://www.phptranslator.com它可以使用 Google API 在后台翻译您的网站。

