GTranslate is a collection of free translation APIs (Google Translate, Bing Translator, Microsoft Translator and Yandex.Translate). It currently supports translation, transliteration, language detection and text-to-speech.
-
5 translation services:
- Google Translate (old and new APIs)
- Bing Translator
- Microsoft Azure Translator
- Yandex.Translate
-
Support for translation, transliteration, language detection and text-to-speech in the included translators.
-
Support for all the languages of each translator.
-
A language class with methods for getting the supported languages and determining the availability of a language in a specific translator.
-
Interfaces, allowing to write custom translators and languages.
-
An
AggregateTranslatorclass that groups the default translators for ease of use with the ability to add custom translators.
Install via NuGet
Or via command:
dotnet add package GTranslate
using GTranslate.Translators;
// Create an instance of the Google Translator
var translator = new GoogleTranslator();
// Translate "Hello world" to Spanish (es)
var result = await translator.TranslateAsync("Hello world", "es");
Console.WriteLine(result);
// Output:
// Translation: 'Hola Mundo', TargetLanguage: 'Spanish (es)', SourceLanguage: 'English (en)', Service: GoogleTranslatorTransliteration is similar to translation but the way it works is specific to each translator. Some translators only support transliteration implicitly and others have dedicated transliteration endpoints (like Yandex).
using GTranslate.Translators;
var translator = new YandexTranslator();
// Transliterate "Hello world" (in Russian) into English (latin script)
var result = await translator.Transliterate("Привет, мир", "en");
Console.WriteLine(result);
// Output:
// Transliteration: 'privet, mir', TargetLanguage: 'English (en)', SourceLanguage: 'Russian (ru)', Service: YandexTranslatorIt's recommended to use MicrosoftTranslator for transliteration because of its superior API that allows you to specify the source and target script explicitly.
GTranslate provides an easy way to access languages through the Language class. A Language object contains the English name, native name, ISO 639-1 code, ISO 639-3 code and the supported services (translation engines).
To get a Language object from its ISO 639-1 code, use the Language.GetLanguage or Language.TryGetLanguage methods. If the language was not found, Language.GetLanguage will throw an exception and Language.TryGetLanguage will simply return false.
A language can also be obtained through its English/native name, ISO-6393 code and some aliases (like zh-Hans or zh-Hant).
using GTranslate;
var french = Language.GetLanguage("fr"); // Get the French language
string input = Console.ReadLine();
if (Language.TryGetLanguage(input, out var language)
{
// Use language from input
}GTranslate exposes the complete list of languages via the language dictionary class LanguageDictionary which can be accessed through Language.LanguageDictionary.
It is essentially a read-only dictionary of ISO 639-1 codes and their respective languages.
Calling TranslateAsync returns an object that derives from ITranslationResult. It contains the translation, source text, service, source language and target language.
The same applies to TransliterateAsync and ITransliterationResult except the transliteration is present instead of the translation.
Some translation engines will provide results with additional data. This extra data is exposed through properties in their concrete classes. For example, GoogleTranslationResult from (GoogleTranslator.TranslateAsync) will sometimes provide the confidence level of the translation and the transliteration.