using System; using System.IO; using System.Xml; using System.Xml.Serialization; using UnityEditor.Localization.Plugins.XLIFF.Common; namespace UnityEditor.Localization.Plugins.XLIFF { /// /// The XLIFF standard version. /// public enum XliffVersion { /// /// XLIFF Version 1.2 /// V12, /// /// XLIFF Version 2.0 /// V20 } /// /// Provides the ability to interact with XLIFF files. /// /// /// This shows how to create an XLIFF document, populate it with values to translate, and then write it to file. /// /// public static class XliffDocument { /// /// Creates a new XLIFF file with the requested version. /// /// The XLIFF version to target. /// The new XLIFF file. public static IXliffDocument Create(XliffVersion version) { IXliffDocument xdoc; if (version == XliffVersion.V12) { xdoc = new V12.xliff { version = V12.AttrType_Version.Item12 }; } else { xdoc = new V20.xliff { version = "2.0" }; } return xdoc; } /// /// Converts and XLIFF stream into a parsed document. /// /// The XLIFF stream. /// The parsed XLIFF document. public static IXliffDocument Parse(Stream stream) { if (stream == null) throw new ArgumentNullException(nameof(stream)); // First parse the version var version = GetVersionFromXml(stream); Type rootType; if (version == "1.2") rootType = typeof(V12.xliff); else if (version == "2.0") rootType = typeof(V20.xliff); else throw new NotSupportedException($"Unsupported XLIFF version {version}. Supported versions are 1.1 and 2.0"); var ser = new XmlSerializer(rootType); return ser.Deserialize(stream) as IXliffDocument; } static string GetVersionFromXml(Stream stream) { var currentPos = stream.Position; var reader = XmlReader.Create(stream, new XmlReaderSettings()); reader.MoveToContent(); var version = reader.GetAttribute("version"); if (version == null) throw new XmlException("Invalid XLIFF file. Could not determine the version."); // Reset stream position stream.Position = currentPos; return version; } } }