public void Detect_Method() { Assert.Throws <ArgumentNullException>(() => IYandexTranslatorExtensions.Detect(null, "text")); Assert.Throws <ArgumentNullException>(() => xmlTranslator.Detect(null)); Assert.Throws <ArgumentException>(() => xmlTranslator.Detect(string.Empty)); string language; Assert.Throws <ArgumentNullException>(() => IYandexTranslatorExtensions.Detect(null, "text", out language)); Assert.Throws <ArgumentNullException>(() => xmlTranslator.Detect(null, out language)); Assert.Throws <ArgumentException>(() => xmlTranslator.Detect(string.Empty, out language)); Assert.Equal("en", xmlTranslator.Detect("Hello, world")); Assert.Equal("ru", xmlTranslator.Detect("Привет, мир")); Assert.True(xmlTranslator.Detect("Hello, world", out language)); Assert.Equal("en", language); Assert.True(xmlTranslator.Detect("Привет, мир", out language)); Assert.Equal("ru", language); Assert.Equal("en", jsonTranslator.Detect("Hello, world")); Assert.Equal("ru", jsonTranslator.Detect("Привет, мир")); Assert.True(jsonTranslator.Detect("Hello, world", out language)); Assert.Equal("en", language); Assert.True(jsonTranslator.Detect("Привет, мир", out language)); Assert.Equal("ru", language); }
/// <summary> /// <para>Makes a language detection request to Yandex.Translator web service.</para> /// </summary> /// <param name="translator">Translator instance to be used.</param> /// <param name="text">Text fragment which language is to be detected.</param> /// <param name="language">Language of the provided text fragment.</param> /// <returns><c>true</c> if language was successfully determined and <paramref name="language"/> output parameter contains its ISO code, or <c>false</c> if detection failed and <paramref name="language"/> is a <c>null</c> reference.</returns> /// <exception cref="ArgumentNullException">If either <paramref name="translator"/> or <paramref name="text"/> is a <c>null</c> reference.</exception> /// <exception cref="ArgumentException">If <paramref name="text"/> is <see cref="string.Empty"/> string.</exception> /// <seealso cref="http://api.yandex.ru/translate/doc/dg/reference/detect.xml"/> public static bool Detect(this IYandexTranslator translator, string text, out string language) { Assertion.NotNull(translator); Assertion.NotEmpty(text); try { language = translator.Detect(text); return(true); } catch { language = null; return(false); } }