Setting up Translation Services - RickStrahl/Westwind.Globalization GitHub Wiki

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

  • Google Translate API v2 (paid API with API key)
  • Google Translator Web Apis (unofficial Google API free)
  • Bing Translate API (free API with API keys)

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:

  • Create a new Project
  • From the Dashboard click on the API (under Explore Services)
  • Find 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.

Bing Translate API

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

</configuration>
   	<DbResourceConfiguration>
    	<add key="BingClientId" value="2b33ee00-4b99-47ed-be7e-caf733526020" />
  		<add key="BingClientSecret" value="Westwind.Globalization.IAATF" />
  	</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 which I've outlined some time ago in a blog post:

https://weblog.west-wind.com/posts/2013/Jun/06/Setting-up-and-using-Bing-Translate-API-Service-for-Machine-Translation

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.

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.

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().