public static void WhiteSpaces() { string data = "<xliff srcLang='en' version='2.0' xmlns='urn:oasis:names:tc:xliff:document:2.0'>" + "<file id='f1'><unit id='u1'>" + "<segment><source>Sentence 1.</source></segment>" + "<ignorable><source> </source></ignorable>" + "<segment><source>Sentence 2.</source></segment>" + "</unit></file></xliff>"; using (IO.MemoryStream ms = new IO.MemoryStream(Encoding.UTF8.GetBytes(data))) { XliffReader reader = new XliffReader(); XliffDocument doc = reader.Deserialize(ms); foreach (XliffElement e in doc.CollapseChildren <XliffElement>()) { Console.WriteLine("Type: " + e.GetType().ToString()); if (e is PlainText) { PlainText pt = (PlainText)e; Console.WriteLine("Content: '" + pt.Text + "'"); } } } Console.ReadKey(); }
/// <summary> /// Demonstrates how to read an XLIFF document from a file. /// </summary> /// <param name="file">The path to the document to read.</param> public static void ReadDocument(string file) { using (IO.FileStream stream = new IO.FileStream(file, IO.FileMode.Open, IO.FileAccess.Read)) { XliffDocument document; XliffReader reader; reader = new XliffReader(); document = reader.Deserialize(stream); } }
//Move to actual XliffReader private static XliffDocument LoadXliff(string xliff) { XliffDocument document = null; using (Stream stream = GenerateStreamFromString(xliff)) { var reader = new XliffReader(); document = reader.Deserialize(stream); } return(document); }
public void XliffWriter_InvalidSegmentId() { var unit = new Unit("u1"); var segment = new Segment(XmlConvert.EncodeNmToken("/this/is/legacy/id")) { Source = new Source(), Target = new Target() }; segment.Source.Text.Add(new CDataTag("from-source")); segment.Target.Text.Add(new CDataTag("to-target")); unit.Resources.Add(segment); var segment2 = new Segment(XmlConvert.EncodeNmToken("this+is+nested")) { Source = new Source(), Target = new Target() }; segment2.Source.Text.Add(new CDataTag("from-source-nested")); segment2.Target.Text.Add(new CDataTag("to-target-nested")); unit.Resources.Add(segment2); var segment3 = new Segment(XmlConvert.EncodeNmToken("this.is.normal.id")) { Source = new Source(), Target = new Target() }; segment3.Source.Text.Add(new CDataTag("from-source-normal")); segment3.Target.Text.Add(new CDataTag("to-target-normal")); unit.Resources.Add(segment3); _document.Files.Add(new File("f1")); _document.Files[0].Containers.Add(unit); var actualValue = Serialize(); var reader = new XliffReader(); var doc = reader.Deserialize(AsStream(actualValue)); var resources = doc.Files[0].Containers.OfType <Unit>().First().Resources; Assert.NotNull(resources.FirstOrDefault(r => XmlConvert.DecodeName(r.Id) == "this+is+nested")); Assert.NotNull(resources.FirstOrDefault(r => XmlConvert.DecodeName(r.Id) == "/this/is/legacy/id")); Assert.NotNull(resources.FirstOrDefault(r => XmlConvert.DecodeName(r.Id) == "this.is.normal.id")); }
public ParseResult Parse(string fileContent) { var reader = new XliffReader(); var doc = reader.Deserialize(AsStream(fileContent)); var result = new List <LocalizationResource>(); var languages = new List <string>(); foreach (var file in doc.Files) { foreach (var container in file.Containers.OfType <Unit>()) { foreach (var resource in container.Resources) { var targetLanguage = resource.Target.Language; var targetCulture = new CultureInfo(targetLanguage).Name; var newResource = new LocalizationResource(XmlConvert.DecodeName(resource.Id), false); newResource.Translations.AddRange(new List <LocalizationResourceTranslation> { new LocalizationResourceTranslation { Language = targetCulture, Value = resource.Target.Text.OfType <CDataTag>() .FirstOrDefault() ?.Text } }); result.Add(newResource); if (!languages.Contains(targetCulture)) { languages.Add(targetCulture); } } } } return(new ParseResult(result, languages.Select(l => new CultureInfo(l)).ToList())); }
public ParseResult Parse(string fileContent) { var reader = new XliffReader(); var doc = reader.Deserialize(AsStream(fileContent)); var result = new List <LocalizationResource>(); var languages = new List <string>(); foreach (var file in doc.Files) { foreach (var container in file.Containers.OfType <Unit>()) { foreach (var resource in container.Resources) { // NOTE: some more modern resources cannot be imported as-is (nested classes) // this might be a dependency on export comp. - knowing how substitution works over there, but yeah.. result.Add(new LocalizationResource(resource.Id.Replace("---", "+")) { Translations = new List <LocalizationResourceTranslation> { new LocalizationResourceTranslation { Language = resource.Target.Language, Value = resource.Target.Text.OfType <CDataTag>().FirstOrDefault()?.Text } } }); if (!languages.Contains(resource.Target.Language)) { languages.Add(resource.Target.Language); } } } } return(new ParseResult(result, languages.Select(l => new CultureInfo(l)).ToList())); }
/// <summary> /// Demonstrates how to store custom attributes and elements on a <see cref="File"/> element using a custom /// extension and element types. /// </summary> public static void StoreCustomExtension() { TestExtension extension; IExtensible extensible; Segment segment; XliffDocument document; XliffReader reader; Unit unit; string path; // This namespace will be stored on the document element like: <xliff xmlns:pre1="urn:custom:extension:1.0" const string customNamespace = "urn:custom:extension:1.0"; const string customPrefix = "customPrefix"; extension = new TestExtension(); document = new XliffDocument("en-us"); document.Files.Add(new File("f1")); unit = new Unit("u1"); document.Files[0].Containers.Add(unit); segment = new Segment("s1"); unit.Resources.Add(segment); segment.Source = new Source(); segment.Source.Text.Add(new PlainText("text")); extensible = document.Files[0]; // Create custom attributes that look like: <file id="f1" pre1:testattr1="testvalue1" pre1:testattr2="testvalue2"> if (extensible.SupportsAttributeExtensions) { extension.AddAttribute(new TestAttribute(customPrefix, customNamespace, "testattr1", "testvalue1")); extension.AddAttribute(new TestAttribute(customPrefix, customNamespace, "testattr2", "testvalue2")); extensible.Extensions.Add(extension); } // Create a custom element that looks like: <pre1:testelement1 pre1:testattr1="testvalue1" /> if (extensible.SupportsElementExtensions) { ElementInfo info; TestElement element; element = new TestElement(); element.SetAttribute(customPrefix, customNamespace, "testattr1", "testvalue1"); info = new ElementInfo(new XmlNameInfo(customPrefix, customNamespace, "testelement1"), element); extension.AddChild(info); } // Write the file just like any other file. path = IO.Path.GetTempFileName(); SampleCode.WriteDocument(document, path); // Read the file using an custom extension handler so the custom types are loaded. The loaded File will // have the custom extension and attributes and elements on it just like it was created above. reader = new XliffReader(); reader.RegisterExtensionHandler(customNamespace, new TestExtensionHandler()); using (IO.FileStream stream = new IO.FileStream(path, IO.FileMode.Open, IO.FileAccess.Read)) { document = reader.Deserialize(stream); } }
public void Xliff_DataDriven() { string path; ExpectedResult expectedResult; int errorNumber; errorNumber = -1; path = (string)this.TestContext.DataRow[FileBasedTests.TestEntryPathAttribute]; expectedResult = (ExpectedResult)Enum.Parse( typeof(ExpectedResult), (string)this.TestContext.DataRow[FileBasedTests.TestEntryResultAttribute]); if (expectedResult == ExpectedResult.ValidationError) { errorNumber = int.Parse((string)this.TestContext.DataRow[FileBasedTests.TestEntryErrorAttribute]); } Trace.WriteLine("Path: " + path); Trace.WriteLine("ExpectedResult: " + expectedResult); Trace.WriteLine("ErrorNumber: " + errorNumber); try { Assert.IsTrue(System.IO.File.Exists(path), "File '{0}' doesn't exist.", path); using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { XliffDocument document; XliffReader reader; reader = new XliffReader(); document = reader.Deserialize(stream); } Assert.AreEqual(expectedResult, ExpectedResult.Success, "Expected result is incorrect."); } catch (FormatException) { if (expectedResult != ExpectedResult.BadFormat) { throw; } } catch (InvalidOperationException) { if (expectedResult != ExpectedResult.InvalidOperation) { throw; } } catch (NotSupportedException) { if (expectedResult != ExpectedResult.NotSupported) { throw; } } catch (ValidationException e) { if (expectedResult != ExpectedResult.ValidationError) { throw; } Assert.AreEqual(errorNumber, e.ErrorNumber, "ErrorNumber is incorrect."); } catch (XmlException) { if (expectedResult != ExpectedResult.ReadError) { throw; } } }