Example #1
0
 private void ReplaceResourceValuesWithTestString(XlfDocument xlfDocument)
 {
     foreach (var transUnit in xlfDocument.Files.SelectMany(f => f.TransUnits))
     {
         transUnit.Target = string.IsNullOrEmpty(TestString) ? DebugString(transUnit) : TestString;
     }
 }
        public void ValidationReportsErrorsOnMissingSourceElement()
        {
            var xliffText =
                @"<xliff xmlns=""urn:oasis:names:tc:xliff:document:1.2"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" version=""1.2"" xsi:schemaLocation=""urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd"">
  <file datatype=""xml"" source-language=""en"" target-language=""fr"" original=""test.resx"">
    <body>
      <trans-unit id=""Alpha"">
        <target state=""new"">Alpha {0}</target>
        <note />
      </trans-unit>
    </body>
  </file>
</xliff>";
            var document = new XlfDocument();

            document.Load(new StringReader(xliffText));

            var validationErrors = GetValidationErrors(document);

            Assert.Collection(validationErrors,
                              new Action <XmlSchemaException>[]
            {
                e => Assert.Equal(expected: "The element 'trans-unit' in namespace 'urn:oasis:names:tc:xliff:document:1.2' has invalid child element 'target' in namespace 'urn:oasis:names:tc:xliff:document:1.2'. List of possible elements expected: 'source' in namespace 'urn:oasis:names:tc:xliff:document:1.2'.", actual: e.Message)
            });
        }
        protected override void ExecuteCore()
        {
            string sourcePath         = XlfFile.GetMetadataOrThrow(MetadataKey.XlfSource);
            string sourceFormat       = XlfFile.GetMetadataOrThrow(MetadataKey.XlfSourceFormat);
            string language           = XlfFile.GetMetadataOrThrow(MetadataKey.XlfLanguage);
            string translatedFullPath = XlfFile.GetMetadataOrThrow(MetadataKey.XlfTranslatedFullPath);

            TranslatableDocument sourceDocument = LoadSourceDocument(sourcePath, XlfFile.GetMetadata(MetadataKey.XlfSourceFormat));
            XlfDocument          xlfDocument    = LoadXlfDocument(XlfFile.ItemSpec);

            bool validationFailed = false;

            xlfDocument.Validate(validationError =>
            {
                validationFailed = true;
                Log.LogErrorInFile(XlfFile.ItemSpec, validationError.LineNumber, validationError.Message);
            });

            IReadOnlyDictionary <string, string> translations = validationFailed
                ? new Dictionary <string, string>()
                : xlfDocument.GetTranslations();

            sourceDocument.Translate(translations);

            Directory.CreateDirectory(Path.GetDirectoryName(translatedFullPath));

            sourceDocument.RewriteRelativePathsToAbsolute(Path.GetFullPath(sourcePath));
            sourceDocument.Save(translatedFullPath);
        }
Example #4
0
 private void ReplaceResourceValuesWithTestString(XlfDocument xlfDocument)
 {
     foreach (var transUnit in xlfDocument.Files.SelectMany(f => f.TransUnits))
     {
         transUnit.Target = string.IsNullOrEmpty(TestString) ? DebugString(transUnit) : TestString;
     }
 }
        public void ValidationReportsNoErrorsOnDocumentWithNoContent()
        {
            var document         = new XlfDocument();
            var validationErrors = GetValidationErrors(document);

            Assert.Empty(validationErrors);
        }
        private static ISet <string> UntranslatedResources(string xliff)
        {
            var xliffDocument = new XlfDocument();

            xliffDocument.Load(new StringReader(xliff));

            return(xliffDocument.GetUntranslatedResourceIDs());
        }
        private static List <XmlSchemaException> GetValidationErrors(XlfDocument document)
        {
            var validationErrors = new List <XmlSchemaException>();
            Action <XmlSchemaException> exceptionHandler = e => validationErrors.Add(e);

            document.Validate(exceptionHandler);
            return(validationErrors);
        }
        public void ValidationReportsNoErrorsOnNewDocument()
        {
            var document = new XlfDocument();

            document.LoadNew("cs");
            var validationErrors = GetValidationErrors(document);

            Assert.Empty(validationErrors);
        }
Example #9
0
 public void XlfSampleHasValidData()
 {
     using (var sample = new ResxWithStaleCorrespondingXlf())
     {
         var doc  = new XlfDocument(sample.XlfFileName);
         var file = doc.Files.First();
         Assert.IsTrue(file.TransUnits.Count() > 0);
     }
 }
Example #10
0
 public void TryGetTransUnit_DoesNotThrow_if_id_is_wrong()
 {
     using (var sample = new ResxWithStaleCorrespondingXlf())
     {
         var doc  = new XlfDocument(sample.XlfFileName);
         var file = doc.Files.First();
         Assert.IsFalse(file.TryGetTransUnit(null, doc.Dialect, out XlfTransUnit u));
         Assert.IsFalse(file.TryGetTransUnit(string.Empty, doc.Dialect, out XlfTransUnit u2));
         Assert.IsFalse(file.TryGetTransUnit(Guid.NewGuid().ToString(), doc.Dialect, out XlfTransUnit u3));
     }
 }
Example #11
0
 public void GetTransUnit_Throws_InvalidOperation_if_id_is_wrong()
 {
     using (var sample = new ResxWithStaleCorrespondingXlf())
     {
         var doc  = new XlfDocument(sample.XlfFileName);
         var file = doc.Files.First();
         Assert.ThrowsException <InvalidOperationException>(() => file.GetTransUnit(null, doc.Dialect));
         Assert.ThrowsException <InvalidOperationException>(() => file.GetTransUnit(string.Empty, doc.Dialect));
         Assert.ThrowsException <InvalidOperationException>(() => file.GetTransUnit(Guid.NewGuid().ToString(), doc.Dialect));
     }
 }
Example #12
0
 public void UpdateTransUnitTest()
 {
     using (var sample = new ResxWithStaleCorrespondingXlf())
     {
         var doc          = new XlfDocument(sample.XlfFileName);
         var file         = doc.Files.First();
         var initialCount = file.TransUnits.Count();
         var unit         = file.AddOrUpdateTransUnit(System.Guid.NewGuid().ToString(), "New source text", "new translation", XlfDialect.Standard);
         file.AddOrUpdateTransUnit(unit.Id, unit.Source, "evene newer translation", XlfDialect.Standard);
         Assert.AreEqual(initialCount + 1, file.TransUnits.Count(), "after adding the same item again, count should still be (initial+1)");
     }
 }
Example #13
0
        public override bool Execute()
        {
            var xlfDocument = new XlfDocument(XlfPath);

            if (!string.IsNullOrEmpty(ReplaceWithTestString) && ReplaceWithTestString.Equals("true"))
            {
                ReplaceResourceValuesWithTestString(xlfDocument);
            }

            xlfDocument.SaveAsResX(ResxPath);

            return(!Log.HasLoggedErrors);
        }
Example #14
0
        public void ExporterGetsFileName()
        {
            using (var sample = new ResxWithStaleCorrespondingXlf())
            {
                var doc  = new XlfDocument(sample.XlfFileName);
                var file = doc.Files.First();

                var exporter = new TestXlfExporter();
                var fileName = Guid.NewGuid().ToString();
                file.Export(fileName, exporter, null, null, XlfDialect.Standard);
                Assert.AreEqual(exporter.File, fileName);
            }
        }
Example #15
0
        public void ExporterGetsTransUnits()
        {
            using (var sample = new ResxWithStaleCorrespondingXlf())
            {
                var doc  = new XlfDocument(sample.XlfFileName);
                var file = doc.Files.First();

                var exporter = new TestXlfExporter();
                file.Export(null, exporter, null, null, XlfDialect.Standard);
                Assert.IsTrue(exporter.Units.Count > 0);
                Assert.AreEqual(exporter.Units.Count, file.TransUnits.Count());
            }
        }
        private static string Sort(string xliff)
        {
            var xliffDocument = new XlfDocument();

            xliffDocument.Load(new StringReader(xliff));

            xliffDocument.Sort();

            var writer = new StringWriter();

            xliffDocument.Save(writer);
            return(writer.ToString());
        }
Example #17
0
        public override bool Execute()
        {
            var xlfDocument = new XlfDocument(XlfPath);

            if (!string.IsNullOrEmpty(ReplaceWithTestString) && ReplaceWithTestString.Equals("true"))
            {
                ReplaceResourceValuesWithTestString(xlfDocument);
            }

            xlfDocument.SaveAsResX(ResxPath);

            return !Log.HasLoggedErrors;
        }
Example #18
0
        public override bool Execute()
        {
            var xlfDocument = new XlfDocument(XlfPath);
            Tuple <int, int, int> result = xlfDocument.UpdateFromResX(ResxPath);

            Log.LogMessage(MessageImportance.Low,
                           "Update results:" +
                           $"\n\t{result.Item1} resources updated" +
                           $"\n\t{result.Item2} resources added" +
                           $"\n\t{result.Item3} resources deleted");

            xlfDocument.Save();

            return(!Log.HasLoggedErrors);
        }
Example #19
0
        public override bool Execute()
        {
            var xlfDocument = new XlfDocument(XlfPath);
            var result      = xlfDocument.Update(ResxPath, "updated", "new");

            Log.LogMessage(MessageImportance.Low,
                           "Update results:" +
                           $"\n\t{result.UpdatedItems} resources updated" +
                           $"\n\t{result.AddedItems} resources added" +
                           $"\n\t{result.RemovedItems} resources deleted");

            xlfDocument.Save();

            return(!Log.HasLoggedErrors);
        }
Example #20
0
        public override bool Execute()
        {
            var xlfDocument = new XlfDocument(XlfPath);
            var result = xlfDocument.Update(ResxPath, "updated", "new");

            Log.LogMessage(MessageImportance.Low,
                "Update results:" +
                $"\n\t{result.UpdatedItems} resources updated" +
                $"\n\t{result.AddedItems} resources added" +
                $"\n\t{result.RemovedItems} resources deleted");

            xlfDocument.Save();

            return !Log.HasLoggedErrors;
        }
Example #21
0
        public void RemoveTransUnitTest()
        {
            using (var sample = new ResxWithStaleCorrespondingXlf())
            {
                var doc          = new XlfDocument(sample.XlfFileName);
                var file         = doc.Files.First();
                var initialCount = file.TransUnits.Count();

                // first be sure unit is there
                file.GetTransUnit("c", XlfDialect.Standard);

                file.RemoveTransUnit("c", XlfDialect.Standard);
                Assert.AreEqual(initialCount - 1, file.TransUnits.Count(), "after removing an item the count should one less than before");
            }
        }
Example #22
0
        public override bool Execute()
        {
            var xlfDocument = new XlfDocument(XlfPath);
            Tuple<int, int, int> result = xlfDocument.UpdateFromResX(ResxPath);

            Log.LogMessage(MessageImportance.Low,
                "Update results:" +
                $"\n\t{result.Item1} resources updated" +
                $"\n\t{result.Item2} resources added" +
                $"\n\t{result.Item3} resources deleted");

            xlfDocument.Save();

            return !Log.HasLoggedErrors;
        }
Example #23
0
        protected override void ExecuteCore()
        {
            string sourcePath         = XlfFile.GetMetadataOrThrow(MetadataKey.XlfSource);
            string sourceFormat       = XlfFile.GetMetadataOrThrow(MetadataKey.XlfSourceFormat);
            string language           = XlfFile.GetMetadataOrThrow(MetadataKey.XlfLanguage);
            string translatedFullPath = XlfFile.GetMetadataOrThrow(MetadataKey.XlfTranslatedFullPath);

            TranslatableDocument sourceDocument = LoadSourceDocument(sourcePath, XlfFile.GetMetadata(MetadataKey.XlfSourceFormat));
            XlfDocument          xlfDocument    = LoadXlfDocument(XlfFile.ItemSpec);

            sourceDocument.Translate(xlfDocument.GetTranslations());

            Directory.CreateDirectory(Path.GetDirectoryName(translatedFullPath));
            sourceDocument.Save(translatedFullPath);
        }
        private void UpdateXlfFile(XlfDocument doc, List <ResXEntry> sourceFile)
        {
            var xlfFile = doc.Files.Single();
            Dictionary <string, XlfTransUnit> existingUnits = xlfFile.TransUnits.ToDictionary(i => i.Id, i => i);
            bool modified = false;

            foreach (var source in sourceFile)
            {
                if (existingUnits.TryGetValue(source.Id, out XlfTransUnit existing))
                {
                    // Exists, see if we need to update it
                    if (source.Value != existing.Source)
                    {
                        existing.Source      = source.Value;
                        existing.TargetState = XlfTransUnit.TargetState_New;
                        modified             = true;
                    }

                    // Remove it since we processed it
                    existingUnits.Remove(source.Id);
                }
                else
                {
                    // Doesn't exist, need to create it
                    xlfFile.AddTransUnit(source.Id, source.Value, source.Value, XlfFile.AddMode.DontCheckExisting, XlfDialect.MultilingualAppToolkit);
                    existingUnits.Remove(source.Id);
                    modified = true;
                }
            }

            if (existingUnits.Count > 0)
            {
                // Need to remove no-longer-used translations
                foreach (var existing in existingUnits.Values)
                {
                    existing.Remove();
                }

                modified = true;
            }

            if (modified)
            {
                doc.Save();
            }
        }
Example #25
0
        private static void ConvertResxToXlf(string resxFile, string xlfDirectory, string originalFile)
        {
            bool   madeNeutral      = false;
            string originalFileName = Path.GetFileName(originalFile);

            if (Path.GetExtension(originalFileName) == ".resx")
            {
                // Make common case of resx original file implicit, but don't remove other extensions
                // or else a .resx and .vsct with the same name will collide.
                originalFileName = Path.GetFileNameWithoutExtension(originalFileName);
            }

            if (!Directory.Exists(xlfDirectory))
            {
                Directory.CreateDirectory(xlfDirectory);
            }

            foreach (var language in s_languages)
            {
                string xlfFile = Path.Combine(xlfDirectory, $"{originalFileName}.{language}.xlf");
                if (!File.Exists(xlfFile))
                {
                    string originalFileId = MakeOriginalFileId(originalFile);

                    File.WriteAllText(xlfFile,
                                      $@"<?xml version=""1.0"" encoding=""utf-8""?>
<xliff xmlns=""urn:oasis:names:tc:xliff:document:1.2"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" version=""1.2"" xsi:schemaLocation=""urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd"">
  <file datatype=""xml"" source-language=""en"" target-language=""{language}"" original=""{originalFileId}"">
    <body>
      <group id=""{originalFileId}"" />
    </body>
  </file>
</xliff>");
                }

                var xlfDocument = new XlfDocument(xlfFile);
                xlfDocument.Update(resxFile, updatedResourceStateString: "needs-review-translation", addedResourceStateString: "new");
                xlfDocument.Save();

                if (!madeNeutral)
                {
                    MakeNeutral(xlfFile, Path.Combine(xlfDirectory, $"{originalFileName}.xlf"));
                    madeNeutral = true;
                }
            }
        }
Example #26
0
        public void AddTransUnitOnlyUpdatesExistingTranslationElements()
        {
            using (var sample = new ResxWithNeutralLangXlf())
            {
                // FIXME: use simpler empty document or even file object for this test!
                var doc   = new XlfDocument(sample.XlfFileName);
                var file  = doc.Files.First();
                var unit1 = file.AddOrUpdateTransUnit(System.Guid.NewGuid().ToString(), "New source text", null, XlfDialect.Standard);
                Assert.IsNull(unit1.Target, "target=null does not create a target element");
                var unit1b = file.AddOrUpdateTransUnit(unit1.Id, unit1.Source, "even newer translation", XlfDialect.Standard);
                Assert.IsNull(unit1b.Target, "update does not create new target elements if the unit already exists");

                var unit2 = file.AddOrUpdateTransUnit(System.Guid.NewGuid().ToString(), "Another source text", "some translation", XlfDialect.Standard);
                Assert.AreEqual(unit2.Target, "some translation", "non empty target strings will create a new target element with the same string");
                var unit2b = file.AddOrUpdateTransUnit(unit2.Id, unit2.Source, "new translation", XlfDialect.Standard);
                Assert.AreEqual(unit2b.Target, "new translation", "existing target elemts should receive new values");
            }
        }
Example #27
0
        public void LoadNewInitializesNewDocumentWithCorrectContent()
        {
            var xliffDocument = new XlfDocument();

            xliffDocument.LoadNew("es");

            var writer = new StringWriter();

            xliffDocument.Save(writer);

            string expected =
                @"<xliff xmlns=""urn:oasis:names:tc:xliff:document:1.2"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" version=""1.2"" xsi:schemaLocation=""urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd"">
  <file datatype=""xml"" source-language=""en"" target-language=""es"" original=""_"">
    <body />
  </file>
</xliff>";

            Assert.Equal(expected, writer.ToString());
        }
Example #28
0
        private static void ConvertXlfToResx(string xlfDirectory)
        {
            // convert xlf -> resx
            foreach (var xlfFile in EnumerateLocalizedFiles(xlfDirectory, "*.xlf"))
            {
                var xlfDocument = new XlfDocument(xlfFile);

                foreach (var language in s_languages)
                {
                    var translatedResxFile =
                        Path.ChangeExtension(
                            Path.Combine(
                                Path.GetDirectoryName(xlfDirectory),
                                Path.GetFileName(xlfFile)),
                            ".resx");

                    xlfDocument.SaveAsResX(translatedResxFile);
                }
            }
        }
Example #29
0
        internal XlfDocument LoadXlfDocument(string path, string language = null, bool createIfNonExistent = false)
        {
            var document = new XlfDocument();

            if (File.Exists(path))
            {
                document.Load(path);
            }
            else if (createIfNonExistent)
            {
                Release.Assert(!string.IsNullOrEmpty(language));
                document.LoadNew(language);
            }
            else
            {
                throw new FileNotFoundException($"File not found: {path}", path);
            }

            return(document);
        }
Example #30
0
        public void UpdateEmptyXlfFromResx()
        {
            using (var sample = new ResxWithEmptyCorrespondingXlf())
            {
                var xlfDocument  = new XlfDocument(sample.XlfFileName);
                var updateResult = xlfDocument.Update(sample.ResxFileName, "new", "new");

                Assert.AreEqual(4, updateResult.AddedItems.Count());
                Assert.AreEqual(0, updateResult.RemovedItems.Count());
                Assert.AreEqual(0, updateResult.UpdatedItems.Count());

                var xlfTransUnits = xlfDocument.Files.SelectMany(f => f.TransUnits).ToDictionary(tu => tu.Id, tu => tu);

                Assert.AreEqual(4, xlfTransUnits.Count);

                AssertTranslationUnit(xlfTransUnits, "a", "Text for a", "Text for a", "Comment for a", "new");
                AssertTranslationUnit(xlfTransUnits, "b", "Text for b", "Text for b", "Comment for b", "new");
                AssertTranslationUnit(xlfTransUnits, "c", "Text for c", "Text for c", "Comment for c", "new");
                AssertTranslationUnit(xlfTransUnits, "d", "Text for d", "Text for d", "Comment for d", "new");
            }
        }
Example #31
0
        public void UpdateStaleXlfFromResxWithCustomStateStrings()
        {
            using (var sample = new ResxWithStaleCorrespondingXlf())
            {
                var xlfDocument  = new XlfDocument(sample.XlfFileName);
                var updateResult = xlfDocument.Update(sample.ResxFileName, "foo", "bar");

                Assert.AreEqual(1, updateResult.AddedItems.Count(), "number of added items must be 1");
                Assert.AreEqual(1, updateResult.RemovedItems.Count(), "number of removed items must be 1");
                Assert.AreEqual(2, updateResult.UpdatedItems.Count(), "number of updated items must be 2");

                var xlfTransUnits = xlfDocument.Files.SelectMany(f => f.TransUnits).ToDictionary(tu => tu.Id, tu => tu);

                Assert.AreEqual(4, xlfTransUnits.Count);

                AssertTranslationUnit(xlfTransUnits, "a", "Text for a", "Translation", "Comment for a", null);
                AssertTranslationUnit(xlfTransUnits, "b", "Text for b", "Translation", "Comment for b", "foo");
                AssertTranslationUnit(xlfTransUnits, "c", "Text for c", "Translation", "Comment for c", "foo");
                AssertTranslationUnit(xlfTransUnits, "d", "Text for d", "Text for d", "Comment for d", "bar");
            }
        }
Example #32
0
        public void ExporterGetsOnlyFilteredItems()
        {
            using (var sample = new ResxWithStaleCorrespondingXlf())
            {
                var doc  = new XlfDocument(sample.XlfFileName);
                var file = doc.Files.First();

                var exporter = new TestXlfExporter();
                file.Export(null, exporter, new List <string>()
                {
                    "this-state-does-not-exist"
                }, null, XlfDialect.Standard);
                Assert.AreEqual(exporter.Units.Count, 0);

                file.Export(null, exporter, new List <string>()
                {
                    "this-state-does-not-exist", "final"
                }, null, XlfDialect.Standard);
                Assert.IsTrue(exporter.Units.Count > 0);
            }
        }
Example #33
0
        private static void ConvertXlfToResx(string xlfDirectory, string resxFile)
        {
            foreach (var xlfFile in EnumerateLocalizedFiles(xlfDirectory, $"{Path.GetFileNameWithoutExtension(resxFile)}.*.xlf"))
            {
                if (xlfFile.Contains(".vsct.") != resxFile.Contains(".vsct."))
                {
                    // quick fix to deal with vsct and resx with same base name
                    continue;
                }

                var xlfDocument = new XlfDocument(xlfFile);

                var translatedResxFile =
                    Path.ChangeExtension(
                        Path.Combine(
                            Path.GetDirectoryName(xlfDirectory),
                            Path.GetFileName(xlfFile)),
                        ".resx");

                xlfDocument.SaveAsResX(translatedResxFile);
            }
        }
        public XlfDocument CreateEmptyXlfDocument(string location, string original, string dataType, string sourceLang)
        {
            try
            {
                using (StreamWriter sw = File.CreateText(location))
                {
                    sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    sw.WriteLine("<xliff version=\"1.2\" xmlns=\"urn: oasis:names: tc:xliff: document:1.2\">");
                    sw.WriteLine(string.Format("  <file source-language=\"{0}\" datatype=\"{1}\" original=\"{2}\">", sourceLang, dataType, original));
                    sw.WriteLine("  </file>");
                    sw.WriteLine("</xliff>");
                }

                XlfDocument doc = new XlfDocument(location);
                xlfDocuments.Add(doc);
                return(doc);
            }
            catch (Exception ex)
            {
                fileSaveErrors.Add(new XliffFileError(location, ex));
                return(null);
            }
        }