Example #1
0
        public void TestWriter_FromFactory()
        {
            List <ILocanRow> expectedValues = new List <ILocanRow> {
                new LocanRow(id: "Key01", translatedString: "Value01"),
                new LocanRow(id: "Key02", translatedString: "Value02"),
                new LocanRow(id: "Key03", translatedString: "Value03"),
            };

            string filepath = this.GetTempFilename(true, ".resx");

            using (ILocanWriter writer = LocanReaderWriterFactory.Instance.GetWriter(new { filepath = filepath })) {
                writer.WriteRows(expectedValues);
            }

            // now we need to read that file
            using (ResXResourceReader reader = new ResXResourceReader(filepath)) {
                int currentIndex = 0;
                foreach (DictionaryEntry de in reader)
                {
                    string expectedKey   = expectedValues[currentIndex].Id;
                    string expectedValue = expectedValues[currentIndex].TranslatedString;

                    Assert.AreEqual(expectedKey, de.Key);
                    Assert.AreEqual(expectedValue, de.Value);

                    currentIndex++;
                }
            }
        }
        private ILocanWriter GetWriterForFilepath(string filepath)
        {
            if (string.IsNullOrEmpty(filepath))
            {
                throw new ArgumentNullException("filepath");
            }

            ILocanWriter writer = null;

            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(Consts.ResxRegexPattern);
            if (r.IsMatch(filepath))
            {
                writer = new ResxFileLocanWriter(filepath);
            }
            return(writer);
        }
        public ILocanWriter GetWriter(object properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException(Consts.Filepath);
            }

            IDictionary <string, object> values = CollectionHelper.Instance.ConvertToDictionary(properties);
            string       filepath = this.GetProperty(values, Consts.Filepath, true) as string;
            ILocanWriter writer   = this.GetWriterForFilepath(filepath);

            if (writer == null)
            {
                string message = string.Format("Couldn't find writer for filepath: [{0}]", filepath);
                throw new WriterNotFoundException(message);
            }

            return(writer);
        }
        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);
                    }
                });
            }
        }