Esempio n. 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"));
        }
Esempio n. 2
0
        public void TestGetSupportedLanguages()
        {
            BingLocanTranslator     translator         = this.GetNewBingLocanTranslator();
            IEnumerable <ILanguage> supportedLanguages = translator.GetSupportedLanguages();

            Assert.IsNotNull(supportedLanguages);
            Assert.IsTrue(supportedLanguages.Count() > 0);
        }
Esempio n. 3
0
        public static IEnumerable <Language> GetLanguages(string apiKey)
        {
            BingLocanTranslator bingTranslator = new BingLocanTranslator(apiKey);
            var result = bingTranslator.GetSupportedLanguages()
                         .Select(l => l.Language.Replace("zh-CHT", "zh"))
                         .Where(l => l != "ht" && l != "zh-CHS");

            return(result.Select(l => new Language()
            {
                IsoCode = l, Emails = new string[0]
            }).OrderBy(l => l.Culture.EnglishName));
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        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);
        }
        private static IEnumerable <TranslationItem> Convert(IEnumerable <Phrase> phrases, string apiKey, string isoCode)
        {
            BingLocanTranslator bingTranslator = new BingLocanTranslator(apiKey);
            var result = bingTranslator.TranslateSync(phrases.Select(p => p.StringToTranslate), new BaseLanguage(isoCode));

            for (int i = 0; i < phrases.Count(); i++)
            {
                Phrase phrase      = phrases.ElementAt(i);
                string translation = result[i].TranslatedText;

                yield return(new TranslationItem()
                {
                    ID = phrase.TranslateKey,
                    ValueFrom = phrase.StringToTranslate,
                    ValueTo = translation,
                    Comment = phrase.Comment
                });
            }
        }
Esempio n. 7
0
        internal void Translate(string apiKey, Project project, ProjectItem selectedItem, EnvDTE80.DTE2 dte)
        {
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                throw new ArgumentNullException("apiKey");
            }
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }
            if (selectedItem == null)
            {
                throw new ArgumentNullException("selectedItem");
            }
            if (dte == null)
            {
                throw new ArgumentNullException("dte");
            }

            // get the file path which should be translated
            string filePath = selectedItem.Properties.Item("FullPath").Value.ToString();

            dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationGeneral);

            ILocanTranslator  bingTranslator  = new BingLocanTranslator(apiKey);
            IList <ILocanRow> rowsToTranslate = this.ReadResxFileForTranslation(filePath);

            var stringsToTranslate = from r in rowsToTranslate
                                     select r.StringToTranslate;

            IList <ILanguage> languages = bingTranslator.GetSupportedLanguages().ToList();

            int       currentLanguageIndex  = 1;
            int       totalCountOfLanguages = languages.Count;
            ILanguage sourceLanguage        = bingTranslator.DetectLanguage(stringsToTranslate.First());

            foreach (ILanguage destLang in languages)
            {
                if (this.PreserveUpdates)
                {
                    // TODO: Look to see if there is an existing file at {Filename}.{language}.resx.cache
                    this.SendTranslationUpdatesToBing(filePath, rowsToTranslate, sourceLanguage, destLang, bingTranslator);
                }

                ProjectItem addedResxProjectItem = null;
                bingTranslator
                .Translate(stringsToTranslate, destLang, sourceLanguage)
                .OnTranslationComplete((payload, translations) => {
                    string destFile = this.GetDestFilename(filePath, payload.DestLanguage);
                    this.UpdateProgressBar(destFile, currentLanguageIndex++, totalCountOfLanguages, dte);
                    using (ILocanWriter writer = LocanReaderWriterFactory.Instance.GetWriter(new { filepath = destFile })) {
                        // it is not reliable to use any variables declared outside of this scope
                        // because this is happening async the loop may change the values outside of this scope
                        int currentIndex = 0;
                        foreach (ITranslation translation in translations)
                        {
                            // get source row
                            ILocanRow sourceRow     = rowsToTranslate[currentIndex];
                            ILocanRow translatedRow = new LocanRow(id: sourceRow.Id, translatedString: translation.TrnaslatedString);
                            writer.WriteRow(translatedRow);

                            // addedResxProjectItem = this.AddFileToProject(selectedItem, destFile);
                            addedResxProjectItem = this.AddFileToProjectAsChild(selectedItem, destFile);
                            currentIndex++;
                        }
                    }

                    if (this.PreserveUpdates)
                    {
                        // now copy this file to the cache location so that we can compute difference next time.
                        string cacheFile = this.GetDestCacheFilename(filePath, payload.DestLanguage);
                        File.Copy(destFile, cacheFile, true);
                        this.AddFileToProjectAsChild(addedResxProjectItem, cacheFile);
                    }
                });
            }
        }