Click here to Skip to main content
15,883,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, the following code is to convert from English to Arabic but somehow the Arabic appear as a question mark, but when converting from arbic to English it's work I don't if some one can help me, I will be appreciate

this is for design code

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>



and this is for code behind

What I have tried:

<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
     protected void Button1_Click(object sender, EventArgs e)
        {
            TranslateText(TextBox1.Text, "en|ar");//Hindi to English
        }

        public string TranslateText(string input, string languagePair)
        {

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

            System.Net.WebClient webClient = new System.Net.WebClient();

            webClient.Encoding = System.Text.Encoding.UTF8;




            string result = webClient.DownloadString(url);
            int index1 = Convert.ToInt32(result.IndexOf("onmouseout=" + '"' + "this." + "style." + "backgroundColor" + "=" + "'" + "#fff" + "'" + '"' + ">"));//onmouseout="this.style.backgroundColor='#fff'">
            int index2 = Convert.ToInt32(result.IndexOf("id=" + '"' + "gt-edit" + '"'));
            int index3 = index2 - index1;
            result = result.Substring((index1 + 47), (index3 - 49));
            Label1.Text = result.ToString();

            return result;

        }

    }

Thank u...
Posted
Updated 5-Dec-21 22:47pm
Comments
Mehdi Gholam 22-Jul-17 6:31am    
? in your output is a presentation problem to do with character encoding in you html or font settings.
Graeme_Grant 22-Jul-17 7:02am    
Does it work as expected if you do it manually with Google Translate?

Side Note: If Google become aware of what you are doing, and they will, you will find that your website will stop working. The correct way is to use their Google Cloud Translation API[^]
Learn.net37 23-Jul-17 4:16am    
thank u but this for testing and I already solve this by change web client.Encoding = System.Text.Encoding.UTF8; to webClient.Encoding = System.Text.Encoding.GetEncoding("windows-1256");

1 solution

In Widows Control PAnel.. Regional Option... Administrative Tab... Select in Langauge Non Unicode Programe.. Select the Arabic langauge of that Region
 
Share this answer
 
Comments
CHill60 6-Dec-21 5:27am    
So everyone using the website would have to do this? Not a good solution at all

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