Language translation at run time
Sometimes we need to show data two or more languages on form/report which is not possible by any configuration and achievable only using code.
Below code help us to translate data from one language to another using code at run time. public static void main(Args args) { str inputStr = "Parashuram"; System.String downloadedString; int len, extraOutputLen, commaOutputLen; str resultActual, url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=" + "en" + "&tl=" + "hi" + "&dt=t&q=" + inputStr; // "en" is the source language, "hi" is the target language and inputStr // is the to_be_convert string // url return the result as : https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&q=Parashuram // If we navigate generated URL(https://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=hi&dt=t&q=Parashuram) // That's download a JSON file and JSON file contains following details // [[["परशुराम","Parashuram",null,null,3,null,null,[[]],[[["233f82c94cbc4425e4cee75c166d47a8","en_hi_2022q2.md"]]]]],null,"en",null,null,null,null,[]] // The first text value is the translated text and from the downloaded text we need to fetch // Targeted translated value. System.Net.WebClient webClient = new System.Net.WebClient(); webClient.set_Encoding(System.Text.Encoding::get_UTF8()); downloadedString = webClient.DownloadString(url); len = downloadedString.get_Length(); resultActual = downloadedString; extraOutputLen = strScan(resultActual, '[', 0, len); commaOutputLen = strScan(resultActual, ',', 0, len); str translatedResult = subStr(resultActual,OutputLen+3,OutputLen1-3); }
Comments
Post a Comment