Ejemplo n.º 1
0
        private static string TranslateFio(string fio)
        {
            var ukName = Translator.Translate("uk", fio).Text;
            var list   = ukName.Split(new char[] { ' ' });

            return($"{list.ElementAt(0)} {list.ElementAt(1)[0]}. {list.ElementAt(2)[0]}.");
        }
Ejemplo n.º 2
0
 public void SetLanguage(string language)
 {
     FormName   = IYandexTranslator.Translate(_FormName, language);
     ButtonNext = IYandexTranslator.Translate(_ButtonNext, language);
     ButtonPrev = IYandexTranslator.Translate(_ButtonPrev, language);
     MenuFile   = IYandexTranslator.Translate(_MenuFile, language);
     MenuEdit   = IYandexTranslator.Translate(_MenuEdit, language);
     OutputBy   = IYandexTranslator.Translate(_OutputBy, language);
     Language   = IYandexTranslator.Translate(_Language, language);
     Page       = IYandexTranslator.Translate(_Page, language);
 }
        public void Translate_Method()
        {
            Assert.Throws <ArgumentNullException>(() => IYandexTranslatorExtensions.Translate(null, "language", "text"));
            Assert.Throws <ArgumentNullException>(() => xmlTranslator.Translate(null, "text"));
            Assert.Throws <ArgumentNullException>(() => xmlTranslator.Translate("language", null));
            Assert.Throws <ArgumentException>(() => xmlTranslator.Translate(string.Empty, "text"));
            Assert.Throws <ArgumentException>(() => xmlTranslator.Translate("language", string.Empty));

            ITranslation translation;

            Assert.Throws <ArgumentNullException>(() => IYandexTranslatorExtensions.Translate(null, request => { }, out translation));
            Assert.Throws <ArgumentNullException>(() => xmlTranslator.Translate(null, out translation));

            TestTranslationResult(xmlTranslator.Translate("ru-en", "Привет, мир"));
            TestTranslationResult(xmlTranslator.Translate(t => t.From("ru").To("en").Text("Привет, мир")));
            Assert.True(xmlTranslator.Translate(t => t.From("ru").To("en").Text("Привет, мир"), out translation));
            TestTranslationResult(translation);

            TestTranslationResult(jsonTranslator.Translate("ru-en", "Привет, мир"));
            TestTranslationResult(jsonTranslator.Translate(t => t.From("ru").To("en").Text("Привет, мир")));
            Assert.True(jsonTranslator.Translate(t => t.From("ru").To("en").Text("Привет, мир"), out translation));
            TestTranslationResult(translation);
        }
Ejemplo n.º 4
0
        public static string GetTranslation(IYandexTranslator translator, string input)
        {
            string translation = HttpUtility.UrlEncode(input);

            try
            {
                translation = translator.Translate("nl-en", translation).Text;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                translation = "";
            }
            translation = HttpUtility.UrlDecode(translation);
            return(translation);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///   <para>Makes a translation request to Yandex.Translator web service.</para>
        /// </summary>
        /// <param name="translator">Translator instance to be used.</param>
        /// <param name="request">Delegate that specifies text for translation, source/target languages and addional options.</param>
        /// <param name="translation"><see cref="ITranslation"/> instance that represents result of text's translation.</param>
        /// <returns><c>true</c> if translation was successfull and <paramref name="translation"/> output parameter contains translated text, or <c>false</c> if translation failed and <paramref name="translation"/> is a <c>null</c> reference.</returns>
        /// <exception cref="ArgumentNullException">If either <paramref name="translator"/> or <paramref name="request"/> is a <c>null</c> reference.</exception>
        /// <seealso cref="http://api.yandex.ru/translate/doc/dg/reference/translate.xml"/>
        public static bool Translate(this IYandexTranslator translator, Action <ITranslationRequest> request, out ITranslation translation)
        {
            Assertion.NotNull(translator);
            Assertion.NotNull(request);

            try
            {
                translation = translator.Translate(request);
                return(true);
            }
            catch
            {
                translation = null;
                return(false);
            }
        }
Ejemplo n.º 6
0
        private void btn_Translate_Click(object sender, EventArgs e)
        {
            if (txt_ShowPDF.Text == "")
            {
                if (cb_ChooseLanguage.SelectedItem == null)
                {
                    MessageBox.Show("Please Open PDF, Convert and Select Language!");
                }
                else
                {
                    MessageBox.Show("Please Open PDF and Convert!");
                }
            }
            else
            {
                if (cb_ChooseLanguage.SelectedItem == null)
                {
                    MessageBox.Show("Select language!");
                }
                else
                {
                    /* string credential_path = @"C:\Users\user\Desktop\selinbuyurman\translate\translate\translate\translate_api.json";
                     * System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);
                     * System.Console.OutputEncoding = System.Text.Encoding.Unicode;
                     * TranslationClient client = TranslationClient.Create();
                     * var response = client.TranslateText( txt_ShowPDF.Text, cb_ChooseLanguage.SelectedItem.ToString());
                     * System.Console.WriteLine(response.TranslatedText); */

                    IYandexTranslator translator = Yandex.Translator.Yandex.Translator(
                        api => api.ApiKey("trnsl.1.1.20171205T210927Z.1672e72d24851492.692b3b66cd77d62884c5e5ab6c2f51a0befa0e17")
                        .Format(ApiDataFormat.Json));
                    IEnumerable <ITranslationPair> translationPairs = translator.TranslationPairs();
                    ITranslation translation = translator.Translate(cb_ChooseLanguage.SelectedItem.ToString(), txt_ShowPDF.Text, "html");
                    txt_ShowTranslate.Text = translation.Text;
                }
            }
        }