Beispiel #1
0
        public string ExportXLIFF(string filePath, bool overwrite = true)
        {
            string result = "Successfully exported to XLIFF.";

            string fileName = Path.GetFileNameWithoutExtension(filePath);

            try
            {
                if (!File.Exists(filePath) || (File.Exists(filePath) && overwrite))
                {
                    if (HasLabels)
                    {
                        XmlDocument xmlDocument = new XmlDocument();

                        XmlWriterSettings xmlSettings = new XmlWriterSettings
                        {
                            Encoding           = FileEncoding,
                            Indent             = true,
                            IndentChars        = "\t",
                            CheckCharacters    = false,
                            OmitXmlDeclaration = true
                        };

                        string     xliffNameSpace = "urn:oasis:names:tc:xliff:document:2.0";
                        XmlElement xliff          = xmlDocument.CreateElement("xliff", xliffNameSpace);
                        xliff.SetAttribute("xmlns", xliffNameSpace);
                        xliff.SetAttribute("version", "2.0");
                        xliff.SetAttribute("srcLang", "en-US");
                        xmlDocument.AppendChild(xliff);

                        XmlElement xliffFile = xmlDocument.CreateElement("file", xliffNameSpace);
                        xliffFile.SetAttribute("id", fileName);
                        xliff.AppendChild(xliffFile);

                        foreach (Label lbl in LBL1.Labels)
                        {
                            XmlElement xliffUnit = xmlDocument.CreateElement("unit", xliffNameSpace);
                            xliffUnit.SetAttribute("id", lbl.Name);
                            xliffFile.AppendChild(xliffUnit);

                            XmlElement xliffSegment = xmlDocument.CreateElement("segment", xliffNameSpace);
                            xliffUnit.AppendChild(xliffSegment);

                            XmlElement xliffSource = xmlDocument.CreateElement("source", xliffNameSpace);

                            xliffSource.InnerText = QA.CleanBeforeExport(lbl.String.Value);
                            xliffSegment.AppendChild(xliffSource);
                        }

                        StreamWriter stream = new StreamWriter(filePath, false, FileEncoding);
                        xmlDocument.Save(XmlWriter.Create(stream, xmlSettings));
                        stream.Close();
                    }
                    else
                    {
                        result = "XLIFF export does not currently support files without an LBL1 section.";
                    }
                }
                else
                {
                    result = fileName + " already exists and overwrite was set to false.";
                }
            }
            catch (Exception ex)
            {
                result = "Unknown Error - " + ex.Message;
            }

            return(result);
        }