I tried to translate text by using the method of the link below
the main part of the code:
public string TranslateText(string input)
{
// Set the language from/to in the url (or pass it into this function)
string url = String.Format
("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
"en", "es", Uri.EscapeUriString(input));
HttpClient httpClient = new HttpClient();
string result = httpClient.GetStringAsync(url).Result;
// Get all json data
var jsonData = new JavaScriptSerializer().Deserialize<List<dynamic>>(result);
// Extract just the first array element (This is the only data we are interested in)
var translationItems = jsonData[0];
// Translation Data
string translation = "";
// Loop through the collection extracting the translated objects
foreach (object item in translationItems)
{
// Convert the item array to IEnumerable
IEnumerable translationLineObject = item as IEnumerable;
// Convert the IEnumerable translationLineObject to a IEnumerator
IEnumerator translationLineString = translationLineObject.GetEnumerator();
// Get first object in IEnumerator
translationLineString.MoveNext();
// Save its value (translated text)
translation += string.Format(" {0}", Convert.ToString(translationLineString.Current));
}
// Remove first blank character
if (translation.Length > 1) { translation = translation.Substring(1); };
// Return translation
return translation;
}
This works on editor and Mono build. but the “dynamic” will crash il2cpp build, becuase it is not surported.
so how can I deserialize this result.
the file attached is the result recieved.
8449160–1120694–translate.7z (5.31 KB)