Omar Kh

Omar Kh

  • NA
  • 301
  • 21.8k

how to translate from language to another in c# ASP.NET?

Mar 4 2021 12:13 PM
im working on a project  (Names Translator ) in c# ASP.NET
 
this is my code
  1. public String Translate(String word)    
  2. {    
  3.     var toLanguage = "en";//English    
  4.     var fromLanguage = "ar";//Deutsch    
  5.     var url = $"https://translate.googleapis.com/translate_a/single?client=gtx&sl={fromLanguage}&tl={toLanguage}&dt=t&q={HttpUtility.UrlEncode(word)}";    
  6.     var webClient = new WebClient    
  7.     {    
  8.         Encoding = System.Text.Encoding.UTF8    
  9.     };    
  10.     var result = webClient.DownloadString(url);    
  11.     try    
  12.     {    
  13.         result = result.Substring(4, result.IndexOf("\"", 4, StringComparison.Ordinal) - 4);    
  14.         return result;    
  15.     }    
  16.     catch    
  17.     {    
  18.         return "Error";    
  19.     }  
  20. } 
and its works for most names
but sometimes "google translate" translate the names literally
for example

the result will be 
He was authorized by Mohamed Ahmed 
????=he was authorized
it translate the name literally
when i go to google translate and translate the same name
it gives me the same wrong translation
but as you notice from the picture "khwlh muhamad ahmad next to red arrow" appear below  is what i want !
 how can i achive this ?
i need help ! thank you 

Answers (1)