This class contains the entry point to the application that demonstrates how to use the XLIFF 2.0 object model.
コード例 #1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">Command line arguments passed to the application.</param>
        public static void Main(string[] args)
        {
            string path;

            path = IO.Path.GetTempFileName();
            try
            {
                XliffDocument document;
                Segment       segment;
                Unit          unit;

                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");
                segment.Source = new Source();
                segment.Source.Text.Add(new PlainText("text"));
                unit.Resources.Add(segment);

                SampleCode.BlankDocument();
                SampleCode.DisableValidationOnWrite(document, path);
                SampleCode.ReadDocument(path);
                SampleCode.StoreCustomExtension();
                SampleCode.StoreGenericExtension();
                SampleCode.StoreGlossary();
                SampleCode.StoreMatches();
                SampleCode.StoreMetadata();
                SampleCode.ViewValidations(new XliffDocument("en-us"), path);
                SampleCode.WriteDocument(document, path);
                SampleCode.WhiteSpaces();
            }
            finally
            {
                IO.File.Delete(path);
            }
        }
コード例 #2
0
        /// <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);
            }
        }