34.4 Translation
// one text
string norwegian = await db.Services.Translation.TranslateAsync("Good morning", "nb");
// many texts in one call; a TranslationText can name languages of its own
var result = await db.Services.Translation.TranslateAsync(
["Good morning", new TranslationText("Hej och välkommen", From: "sv"), new TranslationText("Free delivery", To: "de")],
to: "nb");
string[] texts = result.Texts; // in the order they were sent
string? from = result.Translations[0].From; // "en", detected
// the language of a text
var found = await db.Services.Translation.DetectLanguageAsync("Hei på deg"); // found.Language is "nb"- Languages are BCP 47 codes such as
en,nb,zh-Hansandpt-PT. Answers use the codesGetLanguagesAsync()lists, sonb-NOandnocome back asnb. A language the service does not translate is refused with a 400 before anything is charged. - Send many texts in one call. It is cheaper and faster than one call per text. Without a
from, each text's language is detected. - HTML: pass
format: TranslationFormat.Htmlto keep the markup and translate only the text. GetLanguagesAsync()gives the languages, the price and the most one call may carry, for free.DetectLanguagesAsynccosts the same as translating the texts.