internal ExportResult Export(ICollection <LocalizationResource> resources, CultureInfo fromLanguage, CultureInfo toLanguage)
        {
            if (resources == null)
            {
                throw new ArgumentNullException(nameof(resources));
            }
            if (fromLanguage == null)
            {
                throw new ArgumentNullException(nameof(fromLanguage));
            }
            if (toLanguage == null)
            {
                throw new ArgumentNullException(nameof(toLanguage));
            }

            var doc = new XliffDocument(fromLanguage.Name)
            {
                TargetLanguage = toLanguage.Name
            };

            var file = new File("f1");

            doc.Files.Add(file);

            var unit = new Unit("u1");

            file.Containers.Add(unit);

            foreach (var resource in resources)
            {
                var segment = new Segment(XmlConvert.EncodeNmToken(resource.ResourceKey))
                {
                    Source = new Source(),
                    Target = new Target()
                };

                segment.Source.Text.Add(new CDataTag(resource.Translations.ByLanguage(fromLanguage.Name, false)));
                segment.Target.Text.Add(new CDataTag(resource.Translations.ByLanguage(toLanguage.Name, false)));

                unit.Resources.Add(segment);
            }

            var dest = new MemoryStream();

            var settings = new XliffWriterSettings();

            settings.Validators.Clear();

            var writer = new XliffWriter(settings);

            writer.Serialize(dest, doc);
            dest.Position = 0;

            var reader = new StreamReader(dest);

            return(new ExportResult(reader.ReadToEnd(), "application/x-xliff+xml", $"{fromLanguage.Name}-{toLanguage.Name}-{DateTime.UtcNow:yyyyMMdd}.xliff"));
        }
        public void XliffWriter_SpanningCodeEnd()
        {
            File file;
            Segment segment;
            SpanningCodeEnd span;
            Unit unit;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            unit = new Unit("unit1");
            file.Containers.Add(unit);

            segment = new Segment("seg1");
            segment.Source = new Source();
            span = new SpanningCodeEnd();
            segment.Source.Text.Add(span);
            unit.Resources.Add(segment);

            Console.WriteLine("Test with null Id.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SpanningCodeEndWithNullId), actualValue);

            Console.WriteLine("Test with empty Id.");
            span.Id = string.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SpanningCodeEndWithNullId), actualValue);

            Console.WriteLine("Test with valid Id.");
            span.Id = "span1";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SpanningCodeEndWithValidId), actualValue);
        }
        public void XliffWriter_Segment()
        {
            File file;
            Segment segment;
            Unit unit;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            unit = new Unit();
            file.Containers.Add(unit);

            segment = new Segment();
            unit.Resources.Add(segment);

            Console.WriteLine("Test with default values. Also tests that State is not output because SubState is not output.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SegmentWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values. Also tests that State is output because SubState is output.");
            segment.Id = String.Empty;
            segment.SubState = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SegmentWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            segment.CanResegment = true;
            segment.Id = "id";
            segment.State = TranslationState.Reviewed;
            segment.SubState = "substate";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SegmentWithValidValues), actualValue);
        }
        public void XliffWriter_Skeleton()
        {
            File file;
            string actualValue;

            file = new File();
            file.Skeleton = new Skeleton();
            this._document.Files.Add(file);

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SkeletonWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            file.Skeleton.HRef = String.Empty;
            file.Skeleton.NonTranslatableText = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SkeletonWithEmptyValues), actualValue);

            Console.WriteLine("Test with empty values.");
            file.Skeleton.HRef = "href";
            file.Skeleton.NonTranslatableText = "text";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.SkeletonWithValidValues), actualValue);
        }
        public void XliffWriter_Ignorable()
        {
            File file;
            Ignorable ignorable;
            Unit unit;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            unit = new Unit();
            file.Containers.Add(unit);

            ignorable = new Ignorable();
            unit.Resources.Add(ignorable);

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.IgnorableWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            ignorable.Id = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.IgnorableWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            ignorable.Id = "id";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.IgnorableWithValidValues), actualValue);
        }
        public void XliffWriter_Inheritance()
        {
            File file;
            Segment segment;
            Unit unit;
            string actualValue;

            file = new File("newfile");
            file.Translate = false;
            this._document.SourceLanguage = "en-us";
            this._document.Files.Add(file);

            unit = new Unit("newunit");
            // Inherit the value of Translate.
            file.Containers.Add(unit);

            segment = new Segment("newsegment");
            unit.Resources.Add(segment);

            segment.Source = new Source();
            segment.Source.Text.Add(new MarkedSpan("newmrk") { Translate = true });

            actualValue = this.Serialize(OutputDetail.Minimal);
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.InheritanceWithMinimalOutput), actualValue);
        }
        public void XliffWriter_Group()
        {
            File file;
            Group group;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            group = new Group();
            file.Containers.Add(group);

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.GroupWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            group.Id = String.Empty;
            group.Name = String.Empty;
            group.Type = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.GroupWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            group.CanResegment = false;
            group.Id = "id";
            group.Name = "name";
            group.SourceDirectionality = ContentDirectionality.Auto;
            group.Space = Preservation.Default;
            group.TargetDirectionality = ContentDirectionality.LTR;
            group.Translate = true;
            group.Type = "type";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.GroupWithValidValues), actualValue);
        }
        public void XliffWriter_FullDocument()
        {
            File file;
            Group group1;
            Group group2;
            Ignorable ignorable;
            Note note;
            Segment segment;
            Skeleton skeleton;
            Unit unit;
            string actualValue;

            this._document.SourceLanguage = "en-us";
            this._document.Space = Preservation.Default;
            this._document.TargetLanguage = "de-de";
            this._document.Version = "version";

            file = new File();
            file.CanResegment = true;
            file.Id = "id";
            file.Original = "original";
            file.SourceDirectionality = ContentDirectionality.LTR;
            file.Space = Preservation.Preserve;
            file.TargetDirectionality = ContentDirectionality.RTL;
            file.Translate = true;
            this._document.Files.Add(file);

            group1 = new Group();
            group1.CanResegment = true;
            group1.Id = "id";
            group1.Name = "name";
            group1.SourceDirectionality = ContentDirectionality.RTL;
            group1.Space = Preservation.Preserve;
            group1.TargetDirectionality = ContentDirectionality.Auto;
            group1.Translate = true;
            group1.Type = "type";
            file.Containers.Add(group1);

            group2 = new Group();
            group2.CanResegment = false;
            group2.Id = "id2";
            group2.Name = "name2";
            group2.SourceDirectionality = ContentDirectionality.LTR;
            group2.Space = Preservation.Default;
            group2.TargetDirectionality = ContentDirectionality.RTL;
            group2.Translate = false;
            group2.Type = "type2";
            file.Containers.Add(group2);

            unit = new Unit();
            unit.CanResegment = false;
            unit.Id = "id";
            unit.Name = "name";
            unit.Notes.Add(new Note("text"));
            unit.SourceDirectionality = ContentDirectionality.LTR;
            unit.Space = Preservation.Default;
            unit.TargetDirectionality = ContentDirectionality.Auto;
            unit.Translate = true;
            unit.Type = "type";
            group2.Containers.Add(unit);

            unit.OriginalData = new OriginalData();
            unit.OriginalData.AddData("id", "data1");
            unit.OriginalData.DataElements[0].Directionality = ContentDirectionality.RTL;
            unit.OriginalData.DataElements[0].Space = Preservation.Preserve;
            unit.OriginalData.DataElements[0].Text.Add(new CodePoint(1));

            unit = new Unit();
            unit.CanResegment = false;
            unit.Id = "id2";
            unit.Name = "name2";
            unit.Notes.Add(new Note("text2"));
            unit.SourceDirectionality = ContentDirectionality.RTL;
            unit.Space = Preservation.Preserve;
            unit.TargetDirectionality = ContentDirectionality.LTR;
            unit.Translate = false;
            unit.Type = "type2";
            group2.Containers.Add(unit);

            unit.OriginalData = new OriginalData();
            unit.OriginalData.AddData("id1", "data1");
            unit.OriginalData.DataElements[0].Directionality = ContentDirectionality.LTR;
            unit.OriginalData.DataElements[0].Space = Preservation.Preserve;
            unit.OriginalData.DataElements[0].Text.Add(new CodePoint(2));
            unit.OriginalData.AddData("id2", "data2");
            unit.OriginalData.DataElements[1].Directionality = ContentDirectionality.RTL;
            unit.OriginalData.DataElements[1].Space = Preservation.Preserve;
            unit.OriginalData.DataElements[1].Text.Add(new CodePoint(1));

            ignorable = new Ignorable();
            ignorable.Id = "id";
            ignorable.Source = new Source("text");
            ignorable.Target = new Target("text");
            unit.Resources.Add(ignorable);

            segment = new Segment();
            segment.CanResegment = true;
            segment.Id = "id";
            segment.Source = new Source();
            segment.State = TranslationState.Reviewed;
            segment.SubState = "substate";
            segment.Target = new Target();
            segment.Source.Language = "en-us";
            segment.Source.Space = Preservation.Default;
            segment.Source.Text.Add(new CodePoint(1));
            segment.Source.Text.Add(new PlainText("text"));
            segment.Target.Language = "en-us";
            segment.Target.Order = 100;
            segment.Target.Space = Preservation.Default;
            segment.Target.Text.Add(new CodePoint(12));
            segment.Target.Text.Add(new PlainText("text2"));
            unit.Resources.Add(segment);

            note = new Note();
            note.AppliesTo = TranslationSubject.Source;
            note.Category = "category";
            note.Id = "id";
            note.Priority = 2;
            note.Text = "text";
            file.Notes.Add(note);

            file.Notes.Add(new Note("text2"));

            skeleton = new Skeleton();
            skeleton.HRef = "href";
            skeleton.NonTranslatableText = "text";
            file.Skeleton = skeleton;

            file = new File();
            file.CanResegment = false;
            file.Id = "id2";
            file.Notes.Add(new Note("text"));
            file.Original = "original2";
            file.SourceDirectionality = ContentDirectionality.Auto;
            file.Space = Preservation.Default;
            file.TargetDirectionality = ContentDirectionality.LTR;
            file.Translate = false;
            this._document.Files.Add(file);

            skeleton = new Skeleton();
            skeleton.HRef = "href";
            skeleton.NonTranslatableText = "text";
            file.Skeleton = skeleton;

            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.FullDocument), actualValue);
        }
        public void XliffWriter_FileNotes()
        {
            File file;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            Console.WriteLine("Test with default values.");
            file.Notes.Add(new Note());
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.FileNoteWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            file.Notes[0].Category = String.Empty;
            file.Notes[0].Id = String.Empty;
            file.Notes[0].Text = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.FileNoteWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            file.Notes[0].AppliesTo = TranslationSubject.Target;
            file.Notes[0].Category = "category";
            file.Notes[0].Id = "id";
            file.Notes[0].Priority = 100;
            file.Notes[0].Text = "text";
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.FileNoteWithValidValues), actualValue);
        }
Esempio n. 10
0
        public void XliffWriter_File()
        {
            File file;
            string actualValue;

            file = new File();
            this._document.Files.Add(file);

            Console.WriteLine("Test with default values.");
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.FileWithDefaultValues), actualValue);

            Console.WriteLine("Test with empty values.");
            file.Id = String.Empty;
            file.Original = String.Empty;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.FileWithEmptyValues), actualValue);

            Console.WriteLine("Test with valid values.");
            file.CanResegment = true;
            file.Id = "id";
            file.Original = "original";
            file.Translate = false;
            file.SourceDirectionality = ContentDirectionality.LTR;
            file.TargetDirectionality = ContentDirectionality.RTL;
            actualValue = this.Serialize();
            Assert.AreEqual(TestUtilities.GetFileContents(TestData.FileWithValidValues), actualValue);
        }
Esempio n. 11
0
        internal ExportResult Export(ICollection <LocalizationResource> resources, CultureInfo fromLanguage, CultureInfo toLanguage)
        {
            if (resources == null)
            {
                throw new ArgumentNullException(nameof(resources));
            }
            if (fromLanguage == null)
            {
                throw new ArgumentNullException(nameof(fromLanguage));
            }
            if (toLanguage == null)
            {
                throw new ArgumentNullException(nameof(toLanguage));
            }

            // NOTE: legacy reosurces could not be exported as they contain illegal characters in keys
            // also some more modern resources cannot be exported as-is (nested classes)
            var exportableResources = resources.Where(r => !r.ResourceKey.StartsWith("/"))
                                      .ForEach(r => r.ResourceKey = r.ResourceKey.Replace("+", "---"));

            var doc = new XliffDocument(fromLanguage.Name)
            {
                TargetLanguage = toLanguage.Name
            };

            var file = new File("f1");

            doc.Files.Add(file);

            var unit = new Unit("u1");

            file.Containers.Add(unit);

            foreach (var resource in exportableResources)
            {
                var segment = new Segment(resource.ResourceKey)
                {
                    Source = new Source(),
                    Target = new Target()
                };

                segment.Source.Text.Add(new CDataTag(resource.Translations.ByLanguage(fromLanguage)));
                segment.Target.Text.Add(new CDataTag(resource.Translations.ByLanguage(toLanguage)));

                unit.Resources.Add(segment);
            }

            var dest = new MemoryStream();

            var settings = new XliffWriterSettings();

            settings.Validators.Clear();

            var writer = new XliffWriter(settings);

            writer.Serialize(dest, doc);
            dest.Position = 0;

            var reader = new StreamReader(dest);

            return(new ExportResult(reader.ReadToEnd(), "application/x-xliff+xml", $"{fromLanguage.Name}-{toLanguage.Name}-{DateTime.UtcNow:yyyyMMdd}.xliff"));
        }