Configuring Translate APIs

The default implementation of the Localization interface supports 3 translation services:

  • Google Translator Web Apis (unofficial Google API free)
  • Bing Translate API (free API with API keys)
  • DeepL (free API)

Google Translate APIs

West Wind Globalization supports 2 modes of operation using the Google Translate API. The first uses the full Google Translate API which is a paid API and has to be configured using the Google Developer Console. The other is an 'unofficial' hack using the Google Translator backend APIs that google uses on their Google Translate Web site. The latter is free, but it's unofficial and subject to change by Google at any time.

Full Google Translate API v2

To use the full Google Translate API provide the GoogleApiKey value in .config file for your application:

</configuration>
   	<DbResourceConfiguration>
   		<add key="GoogleApiKey" value="AIzaSyDcsmVhHN7FlynP9QUZOLF8_5K8iF9Ch22" />
  	</DbResourceConfiguration>
</configuration>

Note that the Google Translate API is a paid for service and you need to create a Google Cloud Platform account tied to a credit card number to use it:

  • Go to Google Cloud Platform (https://cloud.google.com/)
  • Create an account or sign in
  • Create a new Project and name it for your Application
  • From the Dashboard click on the API (under Explore Services)
  • Find and click Translate API under (other Popular APIs)
  • Click Enable
  • On the left side-panel click Credentials
  • Create Credentials
  • Create Server Key and give it a name (and optional IP Address restrictions)
Unofficial Google Translator Web API

To use the unofficial API that doesn't require an API key or Google account leave the GoogleApiKey empty. This seems to be working but potentially can be turned off or changed at any time by Google if excessive use is detected or they simply decide to change their internal API.

Use with caution - don't depend on it without a plan B.

Bing Translate API

To use the Bing Translate Services you need to specify a BingClientId in the application's .config file:

</configuration>
   	<DbResourceConfiguration>
    	<add key="BingClientId" value="2b33ee00-4b99-47ed-be7e-caf733526020" />
  	</DbResourceConfiguration>
</configuration>

The full name for the service is Bing Translate API Service for Machine Translation and requires you to sign up for a Microsoft account in the Azure Marketplace. Setting this up is a byzantine process:

https://www.microsoft.com/en-us/translator/trial.aspx

Scroll to to near the bottom.

The Bing Translate Service is free and for a lot of things it actually works much better than that Google service, especially for longer text where the Google service is pretty terrible.

Ideally you have both service hooked up so you can compare between the provided choices.

DeepL

Is new service that uses Machine Learning to do translations. The service has an open JSON RPC api and it frequently produces much more natural translations than the other two providers.

This service doesn't require any configuration settings or keys - it should always be available.

Programmatically using the Translation Services

Google
[Test]
public void TranslateGoogleTest()
{
    TranslationServices service = new TranslationServices();

    string q = null;

    q = "Where are you?";
    string result = service.TranslateGoogle(q, "en", "de");
    Console.WriteLine(q);
    Console.WriteLine(result);
    Console.WriteLine();

    Assert.IsFalse(string.IsNullOrEmpty(result),service.ErrorMessage);
            

    string result2 = service.TranslateGoogle(result, "de", "en", "<GOOGLE API ACCESS KEY>");
    Console.WriteLine(result);
    Console.WriteLine(result2);

    Assert.IsFalse(string.IsNullOrEmpty(result2), service.ErrorMessage);


    q = "Here's some text \"in quotes\" that needs to encode properly";
    string result3 = service.TranslateGoogle(q, "en", "de");
    Console.WriteLine(q);
    Console.WriteLine(result3);

    Assert.IsFalse(string.IsNullOrEmpty(result3), service.ErrorMessage);


    q = "Here's some text \"in quotes\" that needs to encode properly Really, where do I go, what do I do, how do I do it and when can it be done, who said it, where is it and whatever happened to Jim, what happened to Helmut when he came home I thought he might have been dead";

    string result4 = service.TranslateGoogle(q, "en", "de");
    Console.WriteLine(q);
    Console.WriteLine(result4);

    Assert.IsFalse(string.IsNullOrEmpty(result4), service.ErrorMessage);

}

API Key Usage

Note if you don't pass an access key, TranslateGoogle() looks in the .config first for an API key. If it doesn't find it it uses the unofficial, free API. If an API key is provided as a parameter or one is provided in the .config file the API key is used.

DeepL

[Test]
public void DeepLTest()
{
    TranslationServices service = new TranslationServices();

    string result = service.TranslateDeepL("Life is one big wave with a giant bottom turn!", "en",
        "de");
    Assert.IsNotNull(result);

    Console.WriteLine(result);

    string result2 = service.TranslateDeepL(result, "de", "en");
    Console.WriteLine(result2);

    string result3 = service.TranslateDeepL("Here's some text \"in quotes\" that needs to encode properly", "en",
        "de");
    Console.WriteLine(result3);

    string ttext =
        "Here's some text \"in quotes\" that needs to encode properly Really, where do I go, what do I do, how do I do it and when can it be done, who said it, where is it and whatever happened to Jim, what happened to Helmut when he came home I thought he might have been dead";
    Console.WriteLine(ttext);
    string result4 = service.TranslateDeepL(ttext, "en", "de");

    Console.WriteLine(result4);
}

Bing

[Test]
public void BingTest()
{
    TranslationServices service = new TranslationServices();

    // use app.config clientid and clientsecret
    string token = service.GetBingAuthToken();
    Assert.IsNotNull(token);

    string result = service.TranslateBing("Life is great and one is spoiled when it goes on and on and on", "en", "de",token);
    Console.WriteLine(result);

    string result2 = service.TranslateBing(result, "de", "en",token);
    Console.WriteLine(result2);

    string result3 = service.TranslateBing("Here's some text \"in quotes\" that needs to encode properly", "en", "de",token);
    Console.WriteLine(result3);

    string ttext = "Here's some text \"in quotes\" that needs to encode properly Really, where do I go, what do I do, how do I do it and when can it be done, who said it, where is it and whatever happened to Jim, what happened to Helmut when he came home I thought he might have been dead";
    Console.WriteLine(ttext);
    string result4 = service.TranslateBing(ttext, "en", "de",token);

    Console.WriteLine(result4);
}

Note that this is a two step process that retrieves the token first with GetBingAuthToken() which is bearer token you can reuse on subsequent calls to the API when you call TranslateBing().


© West Wind Technologies, 2006 - 2019 • Updated: 01/24/18
Comment or report problem with topic