Click here to Skip to main content
15,610,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am translating arabic text to english ...


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

error is :
"
SQL
Index and length must refer to a location within the string.Parameter name: length
"

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



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


C#
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;
   }
Posted
Updated 23-May-12 2:36am
v2

Hi ,
Check this
C#
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>";


}

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


Best Regards
M.Mitwalli
 
Share this answer
 
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!
 
Share this answer
 
Comments
er.munishyadav 23-May-12 4:54am    
thanks for reply..
i have solve this problem
AmitGajjar 23-May-12 8:49am    
please mention how you resolved your issue. that may help others.
anil_bang0011 8-Dec-17 5:25am    
Hello
I get the issue when I see the response it will showing me ???????
Kindly guide me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900