Ejemplo n.º 1
0
        private void Translate2(string path)
        {
            try
            {
                string directory = Path.GetDirectoryName(path);
                string filename  = Path.GetFileNameWithoutExtension(path);
                string extension = Path.GetExtension(path);
                string newPath   = Path.Combine(directory, $"{filename}_vertaald{extension}");
                DocumentFilename         = path;
                DocumentFilenameVertaald = newPath;

                File.Copy(path, newPath, true);

                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(newPath, true))
                {
                    using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
                    {
                        //var translations = Translations; // GetTranslations();
                        _translationsByVan = Translations.CreateDictionary();
                        _scentencesByFound = new SentenceDictionary();
                        string docText = sr.ReadToEnd();
                        string newText = _sentences.Replace(docText, MatchEvaluator);
                        Sentences = _scentencesByFound.CreateCollection();

                        using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
                        {
                            sw.Write(newText);
                        }
                        AddLog($"Document {path} vertaald.");
                    }
                }
            }
            catch (Exception e)
            {
                AddLog(e);
            }
        }