コード例 #1
0
        /// <summary>
        /// Demonstrates how to validate a document on write and display the data where the validation error occurred.
        /// </summary>
        /// <param name="document">The document to validate.</param>
        /// <param name="file">The path to write the document to.</param>
        public static void ViewValidations(XliffDocument document, string file)
        {
            using (IO.FileStream stream = new IO.FileStream(file, IO.FileMode.Create, IO.FileAccess.Write))
            {
                XliffWriter writer;

                writer = new XliffWriter();

                try
                {
                    writer.Serialize(stream, document);
                }
                catch (ValidationException e)
                {
                    Console.WriteLine("ValidationException Details:");
                    if (e.Data != null)
                    {
                        foreach (object key in e.Data.Keys)
                        {
                            Console.WriteLine("  '{0}': '{1}'", key, e.Data[key]);
                        }
                    }
                }
            }
        }
コード例 #2
0
        private string Serialize()
        {
            _stream.SetLength(0);
            _stream.Seek(0, SeekOrigin.Begin);
            _writer.Serialize(_stream, _document);

            return(GetStreamContents(_stream));
        }
コード例 #3
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));
            }

            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"));
        }
コード例 #4
0
        /// <summary>
        /// Demonstrates how to write an XLIFF document to a file.
        /// </summary>
        /// <param name="document">The document to write.</param>
        /// <param name="file">The path to write the document to.</param>
        public static void WriteDocument(XliffDocument document, string file)
        {
            using (IO.FileStream stream = new IO.FileStream(file, IO.FileMode.Create, IO.FileAccess.Write))
            {
                XliffWriter writer;

                writer = new XliffWriter();
                writer.Serialize(stream, document);
            }
        }
コード例 #5
0
        /// <summary>
        /// Demonstrates how to disable validation when writing an XLIFF document.
        /// </summary>
        /// <param name="document">The document to write.</param>
        /// <param name="file">The path to the document to write.</param>
        public static void DisableValidationOnWrite(XliffDocument document, string file)
        {
            using (IO.FileStream stream = new IO.FileStream(file, IO.FileMode.Create, IO.FileAccess.Write))
            {
                XliffWriter         writer;
                XliffWriterSettings settings;

                settings = new XliffWriterSettings();
                settings.Validators.Clear();

                writer = new XliffWriter(settings);
                writer.Serialize(stream, document);
            }
        }
コード例 #6
0
        /// <summary>
        /// Writes the internal document to the internal stream and returns the contents of the stream using a
        /// specific output detail.
        /// </summary>
        /// <param name="detail">The output detail to use when writing.</param>
        /// <returns>The contents of the stream.</returns>
        private string Serialize(OutputDetail detail)
        {
            XliffWriterSettings settings;
            XliffWriter         writer;

            settings             = new XliffWriterSettings();
            settings.Indent      = true;
            settings.IndentChars = "    ";
            settings.Detail      = detail;

            this._stream.Seek(0, System.IO.SeekOrigin.Begin);
            writer = new XliffWriter(settings);

            writer.Serialize(this._stream, this._document);
            return(this.GetStreamContents());
        }
コード例 #7
0
        public string Write(XliffDocument document, bool indent = false)
        {
            var result = string.Empty;

            using (IO.Stream stream = new IO.MemoryStream())
            {
                XliffWriter writer;

                var settings = new XliffWriterSettings();
                settings.Indent = indent;

                writer = new XliffWriter(settings);
                writer.Serialize(stream, document);
                stream.Position = 0;
                var sr = new IO.StreamReader(stream);
                result = sr.ReadToEnd();
            }
            return(result);
        }
コード例 #8
0
        /// <summary>
        /// Writes the internal document to the internal stream and returns the contents of the stream using a
        /// specific output detail.
        /// </summary>
        /// <param name="detail">The output detail to use when writing.</param>
        /// <returns>The contents of the stream.</returns>
        private string Serialize(OutputDetail detail)
        {
            XliffWriterSettings settings;
            XliffWriter writer;

            settings = new XliffWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "    ";
            settings.Detail = detail;

            this._stream.Seek(0, System.IO.SeekOrigin.Begin);
            writer = new XliffWriter(settings);

            writer.Serialize(this._stream, this._document);
            return this.GetStreamContents();
        }
コード例 #9
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"));
        }