GTradRestAPI – C# client class library of the Google Translate service

icon for a translationThis C # class library implements a client over HTTP for the Google translation service.

Unlike the official Google Translate API, Google Translate connects to the Google service as if it were the Chrome translation module. This eliminates the need for an API key for the service.

This C # library is open source (licence MIT).Find it on GitHub : source code (GIT archive)

GTradRestAPI Client Features:

  • input parameters: text to translate, source language and target language
  • text is split into segments, based on punctuation, line breaks, and segment sizes
  • each segment is the subject of an asynchronous request (async / await) to the translation service
  • the partial results are collected to build the complete translation
  • .NET Framework 4.6.2, Any CPU, Dependencies: None

Use:

GCl = new GTradRestAPIClient();
// langage source, langage cible, texte à traduire
var r = GCl.TranslateAsync(Languages.fr, Languages.de, text);
// texte traduit ou erreur
var res = r.Result; // le texte traduit

Graphical user interface:

The GMiniTranslator tool (for the Windows desktop and free) uses the GTradRestAPI library

gmini translator 2

Synchronous operations:

/*
* SYNC API
*/

/// <summary>
/// translate text from source and target languages codes
/// </summary>
/// <param name="sourceLanguage">source language code</param>
/// <param name="targetLanguage">target language code</param>
/// <param name="text">text to be translated</param>
/// <returns>Translation object</returns>
Translation Translate(
    string sourceLanguageId, 
    string targetLanguageId, 
    string text);

/// <summary>
/// translate text from source and target language names if valids
/// </summary>
/// <param name="sourceLanguageName">source language name</param>
/// <param name="targetLanguageName">target language name</param>
/// <param name="text">text to be translated</param>
/// <returns>Translation object</returns>
Translation Translate(
    Languages sourceLanguage, 
    Languages targetLanguage, 
    string text);

/// <summary>
/// translate text from source and target languages ids given as string if valids
/// </summary>
/// <param name="sourceLanguageId">source language id</param>
/// <param name="targetLanguageId">target language id</param>
/// <param name="text">text to be translated</param>
/// <returns>Translation object</returns>
Translation TranslateFromNames(
    string sourceLanguageName, 
    string targetLanguageName, 
    string text);

Asynchronous operations:

/*
* ASYNC API
*/

/// <summary>
/// translate text from source and target languages codes
/// </summary>
/// <param name="sourceLanguage">source language code</param>
/// <param name="targetLanguage">target language code</param>
/// <param name="text">text to be translated</param>
/// <returns>Translation object</returns>
Task<Translation> TranslateAsync(
    Languages sourceLanguage,
    Languages targetLanguage,
    string text
);

/// <summary>
/// translate text from source and target language names if valids
/// </summary>
/// <param name="sourceLanguageName">source language name</param>
/// <param name="targetLanguageName">target language name</param>
/// <param name="text">text to be translated</param>
/// <returns>Translation object</returns>
Task<Translation> TranslateFromNamesAsync(
    string sourceLanguageName,
    string targetLanguageName,
    string text
);

/// <summary>
/// translate text from source and target languages ids given as string if valids
/// </summary>
/// <param name="sourceLanguageId">source language id</param>
/// <param name="targetLanguageId">target language id</param>
/// <param name="text">text to be translated</param>
/// <returns>Translation object</returns>
Task<Translation> TranslateAsync(
    string sourceLanguageId,
    string targetLanguageId,
    string text
);

Settings:

Language codes are described by the enumeration Languages:

afAfrikaans
sqAlbanian
arArabic
azAzerbaijani
euBasque
beBelarusian
bnBengali
bgBulgarian
caCatalan
zh_CNChinese Simplified
zh_TWChinese Traditional
hrCroatian
csCzech
daDanish
nlDutch
enEnglish
eoEsperanto
etEstonian
tlFilipino
fiFinnish
frFrench
glGalician
kaGeorgian
deGerman
elGreek
guGujarati
htHaitian Creole
iwHebrew
hiHindi
huHungarian
ice Icelandic
idIndonesian
gaIrish
itItalian
jaJapanese
knKannada
koKorean
laLatin
lvLatvian
ltLithuanian
mkMacedonian
msMalay
mtMaltese
noNorwegian
faPersian
plPolish
ptPortuguese
roRomanian
ruRussian
srSerbian
skSlovak
slSlovenian
esSpanish
swSwahili
svSwedish
taTamil
teTelugu
thThai
trTurkish
ukUkrainian
urUrdu
viVietnamese
cyWelsh
yiYiddish

 

Use on the command line:

The default compilation of the library produces an executable on the command line. See this page for usage details

Leave a Reply