Exemple #1
0
        public ActionResult Save(string isoCode, Guid fileId)
        {
            var dal    = new LocanDal();
            var apiKey = dal.GetUserByFileId(fileId).ApiKey;
            ILocanTranslator bingTranslator = new BingLocanTranslator(apiKey);
            Language         language       = Language.GetLanguages(apiKey).SingleOrDefault(l => l.IsoCode.Equals(isoCode, StringComparison.OrdinalIgnoreCase));

            var phrases = dal.GetPhrases(p => p.FileId == fileId).ToList();
            IDictionary <string, string> result = new Dictionary <string, string>();

            foreach (string key in Request.Form)
            {
                var phrase = phrases.SingleOrDefault(p => p.TranslateKey == key);
                if (phrase != null)
                {
                    result.Add(phrase.StringToTranslate, Request.Form[key]);
                }
            }

            ILanguage sourceLanguage = bingTranslator.DetectLanguage(phrases.First().StringToTranslate);
            ILanguage destLanguage   = new Locan.Translate.BaseLanguage(isoCode);

            bingTranslator.AddCustomTranslations(sourceLanguage, destLanguage, result);

            return(View("Done"));
        }
        public ActionResult Save(string isoCode, Guid fileId)
        {
            var dal = new LocanDal();
            var apiKey = dal.GetUserByFileId(fileId).ApiKey;
            ILocanTranslator bingTranslator = new BingLocanTranslator(apiKey);
            Language language = Language.GetLanguages(apiKey).SingleOrDefault(l => l.IsoCode.Equals(isoCode, StringComparison.OrdinalIgnoreCase));

            var phrases = dal.GetPhrases(p => p.FileId == fileId).ToList();
            IDictionary<string, string> result = new Dictionary<string, string>();

            foreach (string key in Request.Form)
            {
                var phrase = phrases.SingleOrDefault(p => p.TranslateKey == key);
                if (phrase != null)
                {
                    result.Add(phrase.StringToTranslate, Request.Form[key]);
                }
            }

            ILanguage sourceLanguage = bingTranslator.DetectLanguage(phrases.First().StringToTranslate);
            ILanguage destLanguage = new Locan.Translate.BaseLanguage(isoCode);

            bingTranslator.AddCustomTranslations(sourceLanguage, destLanguage, result);

            return View("Done");
        }
        public ILanguage DetectLanguage(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException("text");
            }

            ILanguage result = null;

            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                string lang = client.Detect(this.ApiKey, text);
                result = new BaseLanguage(lang);
            }
            return(result);
        }
        public ILanguage DetectLanguage(string text)
        {
            if (string.IsNullOrEmpty(text)) { throw new ArgumentNullException("text"); }

            ILanguage result = null;
            using (BingTranslatorService.SoapService client = new BingTranslatorService.SoapService()) {
                string lang = client.Detect(this.ApiKey, text);
                result = new BaseLanguage(lang);
            }
            return result;
        }
        public void TestTranslateAsync()
        {
            BingLocanTranslator translator = this.GetNewBingLocanTranslator();

            List<string> stringsToTranslate = new List<string> {
                    @"What is your name?",
                    @"How old are you?",
                    @"My name is Sayed."
                };

            List<string> expectedTranslations = new List<string> {
                    @"Qual é seu nome?",
                    @"São velhos ou não é?",
                    @"Meu nome é Sayed.",
                };

            ILanguage sourceLanguage = new BaseLanguage("en");
            ILanguage destLangage = new BaseLanguage("pt");

            int currentIndex = 0;
            translator
                .Translate(stringsToTranslate, destLangage, sourceLanguage)
                .OnTranslationComplete((payload, translationValues) => {
                    Assert.AreEqual(stringsToTranslate.Count, translationValues.Count());
                    for (int i = 0; i < stringsToTranslate.Count; i++) {
                        string stringToTranslate = stringsToTranslate[i];
                        string expectedTranslation = expectedTranslations[i];
                        ITranslation translation = translationValues.ElementAt(i);

                        Assert.AreEqual(stringToTranslate, translation.StringToTranslate);
                        Assert.AreEqual(expectedTranslation, translation.TrnaslatedString);

                        currentIndex++;
                    }
                });

            // must give the service time to perform the translations
            Thread.Sleep(10000);

            Assert.IsTrue(currentIndex == stringsToTranslate.Count);
        }
        public void TestTranslateSingleString_NoDestLangSpecified()
        {
            BingLocanTranslator translator = this.GetNewBingLocanTranslator();

            string stringToTranslate = @"What is your name?";
            string expectedTranslation = @"Qual é seu nome?";
            ILanguage destLanguage = new BaseLanguage("pt");
            ITranslation translation = translator.Translate(stringToTranslate, destLanguage);
            Assert.IsNotNull(translation);
            Assert.AreEqual(stringToTranslate, translation.StringToTranslate);
            Assert.AreEqual(destLanguage, translation.DestLanguage);
            Assert.AreEqual(expectedTranslation, translation.TrnaslatedString);
        }