翻译阿拉伯语到英语 [英] translate arabic to english

查看:118
本文介绍了翻译阿拉伯语到英语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将阿拉伯文本翻译成英语...


但是我在方法的这一部分出现错误
结果= result.Substring(res1,res2);"

错误是:
"

i am translating arabic text to english ...


but i getting error in this part of method
"result = result.Substring(res1, res2);"

error is :
"

Index and length must refer to a location within the string.Parameter name: length

"

异常详细信息:System.ArgumentOutOfRangeException:索引和长度必须引用字符串中的位置.



"

Exception Details: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.



protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string val = "مشرف لوحة أجهزة القياس";
            TranslateText(val, "ar|en");
         }
    }





public string TranslateText(string input, string languagePair)
   {
       string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
       WebClient webClient = new WebClient();
       webClient.Encoding = System.Text.Encoding.UTF8;
       string result = webClient.DownloadString(url);
       int  res1 = result.IndexOf("id=result_box") + 22;
       int res2 = result.IndexOf("id=result_box") + 499;
       int  len = result.Length;
       result = result.Substring(res1, res2);
       result = result.Substring(0, result.IndexOf("</div"));
       return result;
   }

推荐答案


检查此
Hi ,
Check this
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string val = "مشرف لوحة أجهزة القياس";

        trans.InnerHtml = TranslateText(val, "ar|en");
    }
}
public string TranslateText(string input, string languagePair)
{
    string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);

    WebClient webClient = new WebClient();
    webClient.Encoding = System.Text.Encoding.UTF8;
    string result = webClient.DownloadString(url);
    int len = result.Length;
    result = result.Remove(0, result.IndexOf("id=result_box"));
    int len2 = result.Length;
    result = result.Remove(result.IndexOf("</span>"));
    return "<span"+ result +"</span>";


}


<body>
    <form id="form1" runat="server">
    <div id="trans" runat="server">
    </div>
    </form>
</body>



最好的问候
M.Mitwalli



Best Regards
M.Mitwalli


您应该检查res1和res2的值是否不超过结果的长度(因为它是第一次分配的.您可能只想放子字符串中的其他值,而不是重复使用几次结果,以使其更清晰,更容易调试)
另外,如果IndexOf找不到您要查找的内容,它将得到-1,这也不是有效的索引.您可能还需要检查一下.

祝你好运!
You should check if the value of both res1 and res2 won''t exceed the length of result (as it is first assigned. you might simply want to put the substring in some other value instead of reusing result a few times to keep it more clear and easier to debug)
Also, if IndexOf can''t find what you are looking for it will give you -1, which isn''t a valid index either. You might also want to check that.

Good luck!


这篇关于翻译阿拉伯语到英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆