Transliteration is a type of conversion of a text from one script to another, that involves swapping letters (thus trans- + liter-) in predictable ways (such as α → a, д → d, χ → ch, ն → n or æ → e). For instance, for the Modern Greek term “Ελληνική Δημοκρατία”, which is usually translated as “Hellenic Republic”, the usual transliteration to Latin script is “Ellēnikḗ Dēmokratía”, and the name for Russia in Cyrillic script, “Россия”, is usually transliterated as “Rossiya”.
Prerequisites
- To run the sample code you must have an edition of Visual Studio installed.
- You will need the Json.NET NuGet package.
- You will need the .NET SDK installed in your machine
- You will need an Azure Cognitive Services account with a Translator Text resource. If you don’t have an account, you can use the free trial to get a subscription key.
Create your Project
To create an application to translate your text follow the steps below:
- Create a .NET Core Console Application in Visual Studio 2017
- Add the JSON.net nuget package
Install-Package Newtonsoft.Json
- Add the following code under Program
static void TransliterateText() { string FromScript = "jpan"; string ToScript = "latn"; string host = "https://api.cognitive.microsofttranslator.com"; string route = "/transliterate?api-version=3.0&language=ja&fromScript="+FromScript+"&toScript="+ToScript; string subscriptionKey = "enter your subscription key"; System.Object[] body = new System.Object[] { new { Text = @"こんにちは" } }; var requestBody = JsonConvert.SerializeObject(body); using (var client = new HttpClient()) using (var request = new HttpRequestMessage()) { // Set the method to POST request.Method = HttpMethod.Post; // Construct the full URI request.RequestUri = new Uri(host + route); // Add the serialized JSON object to your request request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); // Add the authorization header request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey); // Send request, get response var response = client.SendAsync(request).Result; var jsonResponse = response.Content.ReadAsStringAsync().Result; // Print the response Console.WriteLine(jsonResponse); Console.WriteLine("Press any key to continue."); } } static void Main(string[] args) { TransliterateText(); Console.ReadLine(); }
- Replace the parameters FromScript and ToScript.
- Replace your subscription key here: string subscriptionKey = “enter your subscription key”;
- Add here the text you want to be translated System.Object[] body = new System.Object[] { new { Text = @”こんにちは” } };
The following limitations apply:- The array can have at most 10 elements.
- The text value of an array element cannot exceed 1,000 characters including spaces.
- The entire text included in the request cannot exceed 5,000 characters including spaces.
- Run the program
Get Results
The result is in the following format. That’s it we have transliterated the japanese text to latin!
[ {"text":"konnnichiha","script":"Latn"}]
You can find the complete source code in my Github in this repository in the TextTransliteration Project.