Quickstart: Use the Translator Text API to transliterate text using C#

In this quickstart, you'll learn how to transliterate (convert) text from one script to another using .NET Core (C#) and the Translator Text REST API. In the sample provided, Japanese is transliterated to use the Latin alphabet.

This quickstart requires 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 a .NET Core project

Open a new command prompt (or terminal session) and run these commands:

dotnet new console -o transliterate-sample
cd transliterate-sample

The first command does two things. It creates a new .NET console application, and creates a directory named transliterate-sample. The second command changes to the directory for your project.

Next, you'll need to install Json.Net. From your project's directory, run:

dotnet add package Newtonsoft.Json --version 11.0.2

Add required namespaces to your project

The dotnet new console command that you ran earlier created a project, including Program.cs. This file is where you'll put your application code. Open Program.cs, and replace the existing using statements. These statements ensure that you have access to all the types required to build and run the sample app.

using System;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;

Create a function to transliterate text

Within the Program class, create a function called TransliterateText. This class encapsulates the code used to call the Transliterate resource and prints the result to console.

static void TransliterateText()
   * The code for your call to the translation service will be added to this
   * function in the next few sections.

Set the subscription key, host name, and path

Add these lines to the TransliterateText function. You'll notice that along with the api-version, two additional parameters have been appended to the route. These parameters are used to set the input language, and the scripts for transliteration. In this sample, it's set to Japanese (jpan) and Latin (latn). Make sure you update the subscription key value.

string host = "https://api.cognitive.microsofttranslator.com";
string route = "/transliterate?api-version=3.0&language=ja&fromScript=jpan&toScript=latn";
string subscriptionKey = "YOUR_SUBSCRIPTION_KEY";

Next, we need to create and serialize the JSON object that includes the text you want to transliterate. Keep in mind, you can pass more than one object in the body array.

System.Object[] body = new System.Object[] { new { Text = @"こんにちは" } };
var requestBody = JsonConvert.SerializeObject(body);

Instantiate the client and make a request

These lines instantiate the HttpClient and the HttpRequestMessage:

using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
  // In the next few sections you'll add code to construct the request.

Construct the request and print the response

Inside the HttpRequestMessage you'll:

  • Declare the HTTP method
  • Construct the request URI
  • Insert the request body (serialized JSON object)
  • Add required headers
  • Make an asynchronous request
  • Print the response

Add this code to the 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("Press any key to continue.");

Put it all together

The last step is to call TransliterateText() in the Main function. Locate static void Main(string[] args) and add these lines:


Run the sample app

That's it, you're ready to run your sample app. From the command line (or terminal session), navigate to your project directory and run:

dotnet run

Sample response

        "script": "latn",
        "text": "konnichiwa"

Clean up resources

Make sure to remove any confidential information from your sample app's source code, like subscription keys.

Next steps

Explore the sample code for this quickstart and others, including translation and language identification, as well as other sample Translator Text projects on GitHub.

See also