Quickstart for Microsoft Translator Text API with Ruby

This article shows you how to use the Translate method to translate text from one language to another. For information on how to use the other Translator Text APIs, see this Github repository.


You will need Ruby 2.4 or later to run this code.

You must have a Cognitive Services API account with Microsoft Translator Text API. You will need a paid subscription key from your Azure dashboard.

Translate text

The following code translates source text from one language to another, using the Translate method.

  1. Create a new Ruby project in your favorite IDE.
  2. Add the code provided below.
  3. Replace the key value with an access key valid for your subscription.
  4. Run the program.
require 'net/https'
require 'uri'
require 'cgi'

# **********************************************
# *** Update or verify the following values. ***
# **********************************************

# Replace the key string value with your valid subscription key.

host = 'https://api.microsofttranslator.com'
path = '/V2/Http.svc/Translate'

target = 'fr-fr'
text = 'Hello'

params = '?to=' + target + '&text=' + CGI.escape(text)
uri = URI (host + path + params)

request = Net::HTTP::Get.new(uri)
request['Ocp-Apim-Subscription-Key'] = key

response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request (request)

puts response.body

Translate response

A successful response is returned in XML, as shown in the following example:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Salut</string>

Next steps

See also

Translator Text overview
API Reference