public async Task GivenTextToTranslate_WhenTranslated_ThenTranslationProviderReturnsTranslation() { // Arrange string textToBeTranslated = "Space: the final frontier. These are the voyages of the starship Enterprise. Her ongoing mission: to explore strange new worlds, to seek out new life-forms and new civilizations; to boldly go where no one has gone before."; string vulcanTranslation = "Ret: wuh kim-shah frontier. Aifa nam-tor wuh halan t' wuh yel-hali enterprise. Ish-veh ongoing skrol: tor explore flekh uzh panu, tor seek si' uzh ha'kiv-shidau heh uzh sutenivaya; tor boldly hal-tor wilat kling has hal-tor fa'."; var apiTranslationResponse = new ApiTranslationResponse() { success = new Success() { total = 1 }, contents = new Contents() { translated = vulcanTranslation, text = textToBeTranslated, translation = "vulcan" } }; _mockTranslationApi.Setup(t => t.Translate(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(apiTranslationResponse); ITranslationProvider vulcanTranslationProvider = new VulcanTranslator(_mockLoggerFactory, _mockTranslationApi.Object); // Act var translationResult = await vulcanTranslationProvider.TranslatedPokemon(new List <TranslationRequest>() { new TranslationRequest("test property", textToBeTranslated) }); // Assert translationResult.Should().NotBeNull(); translationResult.Count().Should().Be(1); translationResult.First().TranslatedText.Should().Be(vulcanTranslation); }
public string Translate(Language from, Language to, string str) { string fromCode = GetLanguageCodeFor(from); string toCode = GetLanguageCodeFor(to); if (string.IsNullOrWhiteSpace(str) || fromCode.Equals(toCode)) { return(str); } string endpoint = string.Format(ApiEndpoint, "translate"); string uri = endpoint + $"&from={fromCode}&to={toCode}"; string requestBody = JsonConvert.SerializeObject(new[] { new ApiTranslationString(str) }); string resp = DoSimpleWebRequest(uri, requestBody, true); ApiTranslationResponse response = JsonConvert .DeserializeObject <ApiTranslationResponse[]>(resp) .FirstOrDefault(); return(response.translations.FirstOrDefault().text); }