• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Api for free text translation using Google translate

GoogleTranslateFreeApi

Api for free text translation using Google translate.

Badge
Target Framework .Net Standard 1.1
Nuget Nuget
License MIT

Main features:

  • Text corrections
  • Language corrections
  • Language auto detection
  • Transcriptions (original text, translated text)
  • Synonyms
  • Definitions + Examples
  • Extra translations
  • Proxy (Useful when getting a ban for a while)

Feature TranslateLiteAsync TranslateAsync
Text corrections + +
Language corrections + +
Language auto detection + +
Transcriptions + +
Synonyms - +
Definitions + Examples - +
Extra translations - +
See Also - +

Usage

Translation, transcription and text/language corrections

var translator = new GoogleTranslator();

Language from = Language.Auto;
Language to = GoogleTranslator.GetLanguageByName("Japanese");

TranslationResult result = await translator.TranslateLiteAsync("Hello. How are you?", from, to);

//The result is separated by the suggestions and the '\n' symbols
string[] resultSeparated = result.FragmentedTranslation;

//You can get all text using MergedTranslation property
string resultMerged = result.MergedTranslation;

//There is also original text transcription
string transcription = result.TranslatedTextTranscription; // Kon'nichiwa! Ogenkidesuka?

Language auto detection and correction

var result2 = await translator.TranslateLiteAsync("Drones", Language.Auto, Language.Czech);

foreach(LanguageDetection languageDetection in result2.LanguageDetections)
{
	Console.WriteLine(languageDetection);
}

// The output will be next. Double in brackets is confidence
// Spanish (0.61010)
// English (0.34365)

Language spanish = new Language("Spanish", "es"); // For the method, only the second parameter is important (ISO639)

result2 = await translator.TranslateLiteAsync("world", spanish, Language.Czech);
Console.WriteLine(result2.LanguageDetections.First()); // English (0.98828)
Console.WriteLine(result2.MergedTranslation) // ΠΌΠΈΡ€

Text corrections

string misspellingsText = "The quik brown fox jumps ovver the lazy dog"

var english = new Language("English language", "en");
// you could also get language from Language static properties
// for example: Language.English or Language.Korean

// check language is valid for GoogleTranslator class
Console.WriteLine(GoogleTranslator.IsLanguageSupported(english)); 

var result3 = await translator.TranslateLiteAsync(misspellingsText, english, GoogleTranslator.GetLanguageByISO("ru"));

if(result3.Corrections.TextWasCorrected)
	Console.WriteLine(string.Join(",", result3.Corrections.CorrectedWords); // "quick", "over"
	
Console.WriteLine(result3.MergedTranslation) // "Быстрая коричнСвая лиса ΠΏΡ€Ρ‹Π³Π°Π΅Ρ‚ Ρ‡Π΅Ρ€Π΅Π· Π»Π΅Π½ΠΈΠ²ΡƒΡŽ собаку"

Synonyms, extra translations, definitions and so on.

GoogleTranslator translator = new GoogleTranslator();

var result4 = await translator.TranslateAsync("ΠΊΠ½ΠΈΠ³Π°", Language.Russian, Language.English);

if(result.ExtraTranslations != null)
  Console.WriteLine(result.ExtraTranslations.ToString()); // ToString returns friendly for reading string
  

Example of ToString output:

Noun:
book: ΠΊΠ½ΠΈΠ³Π°, ΠΊΠ½ΠΈΠΆΠΊΠ°, ΠΆΡƒΡ€Π½Π°Π», ΠΊΠ½ΠΈΠΆΠ΅Ρ‡ΠΊΠ°, Ρ‚ΠΎΠΌ, тСкст
volume: объСм, Ρ‚ΠΎΠΌ, Π³Ρ€ΠΎΠΌΠΊΠΎΡΡ‚ΡŒ, ΠΊΠ½ΠΈΠ³Π°, Π΅ΠΌΠΊΠΎΡΡ‚ΡŒ, масса
Abbreviation:
bk: ΠΊΠ½ΠΈΠ³Π°, Π½Π°Π·Π°Π΄, ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎ

You can also get any part of the speech indently

foreach(ExtraTranslation item in result4.ExtraTranslations)
  Console.WriteLine($"{item.Phrase}: {String.Join(", ", item.PhraseTranslations)}"); 
  // just like item.ToString()

There are other translate information getting by the same principe

  • Definitions
  • Synonyms
  • See also

Google Translate can ban IP that sends too many requests at the same time. Ban lasts about a few hours, but you can use a Proxy

var proxy = new WebProxy(uri); // You also can use GoogleTranslateFreeApi.Proxy class for this
translator.Proxy = proxy;

LICENSE

Released under the MIT License.

This repository сontains part of the code from the library google-translate-token