Use Google Translate for free on the command line

image

keyboard with translation keyThe official Google Translate API is available to businesses by paying a license fee … But it is possible to call the secret API translate.googleapis.com which is used internally by the extension for Google Translate Chrome!

On his website Digital Inspiration, the famous Amit Agarwal (historically the 1st Indian blogger) informs us of this possibility and shows us how to implement it in Javascript.

It gave me the idea to make a small API in C # to provide access to the translation service, and a command line tool to use the API. Here is an example of the result :

console dos GTradRestAPI

The syntax of the command is given by it in the absence of parameters:

GTradRestAPI.exe

command line syntax:
source_langid target_langid text [-q]
    source_langid : original text lang id
    target_langid : translated text lang id
    -q : turn off all outputs excepting errors
or
    -list : dump list of langugaes ids & names

the language codes that must be provided as parameters for the translation can be obtained by using the second syntax of the command :

GTradRestAPI.exe -list

the API proposed here allows to perform the same work with the command line tool while agreeing to work with names or language codes in addition to the identifiers :

namespace GTradRestAPI
{
    public interface IGTradRestAPIClient
    {
        /// <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);
    }
}

how to get the tool?

The binary is available for download on this site. You must be logged in to access the download. Use the following link to download the GTradRestAPI.exe tool:

GTradRestAPI

The source code is placed in a public repository on GitHub at:

https://github.com/franck-gaspoz/GTradRestAPI

To learn more about the GitHub project, go to the wiki project page on GitHub

Leave a Reply