public XmlDocument CreateXliffDocument(XliffVersion xliffVersion, IEnumerable translationUnitsCollection, string SaveOption, string LangCode) { XmlDocument xmlDocument = new XmlDocument(); XmlNode docNode = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", null); xmlDocument.AppendChild(docNode); switch (xliffVersion) { case XliffVersion.V12: return(CreateXliffDocumentV12(xmlDocument, translationUnitsCollection, SaveOption, LangCode)); default: throw new NotImplementedException("Not implemented XliffVersion"); } }
public ObservableCollection <TranslationUnit> GetTranslationUnitsFromFile(string filePath) { this.LastFilePath = filePath; string text = string.Empty; int originalTextSize = -1, escapedTextLength = -1; using (StreamReader streamReader = new StreamReader(filePath)) { text = streamReader.ReadToEnd(); originalTextSize = text.Length; } text = text.Replace("&", "_AMP;_"); escapedTextLength = text.Length; //if (originalTextSize != escapedTextLength) //{ // MessageBox.Show("There were some invalid characters in the file.\nSince default XML parser doesn't work with invalid XML format file, these characters were replaced.\n& = &", "Invalid file", MessageBoxButton.OK, MessageBoxImage.Information); //} try { XmlDocument.LoadXml(text); } catch (XmlException ex) { MessageBox.Show("Error occured while reading file. Please, create new issue with this report on GitHub:\n\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(null); } XmlNamespaceManager = new XmlNamespaceManager(XmlDocument.NameTable); XmlNamespaceManager.AddNamespace(NAMESPACE_PREFIX, GetNamespace()); XliffVersion xliffVersion = GetXliffVersion(); SaveSourceLanguage(xliffVersion); switch (xliffVersion) { case XliffVersion.V12: return(GetTranslationUnitsV12()); case XliffVersion.V20: return(GetTranslationUnitsV20()); default: return(null); } }
/// <summary> /// Export the values in <paramref name="tables"/> using <paramref name="sourceLanguage"/> as the source language to one or more XLIFF files. /// </summary> /// <param name="sourceLanguage">This is the table that will be used as the source language for all generated XLIFF files.</param> /// <param name="directory">The directory where all generated XLIFF files will be saved to.</param> /// <param name="version">The XLIFF version to generate the files in.</param> /// <param name="tables">1 or more <see cref="StringTable"/> that will be used as the target language for each XLIFF file. 1 XLIFF file will be generated for each table.</param> /// <param name="reporter">Optional reporter which can report the current progress.</param> public static void Export(StringTable sourceLanguage, string directory, XliffVersion version, ICollection <StringTable> tables, ITaskReporter reporter = null) { if (sourceLanguage == null) { throw new ArgumentNullException(nameof(sourceLanguage)); } if (tables == null) { throw new ArgumentNullException(nameof(tables)); } try { // Used for reporting float taskStep = 1.0f / (tables.Count * 2.0f); float progress = 0; reporter?.Start($"Exporting {tables.Count} String Tables to XLIFF", string.Empty); // We need the key, source value and translated value. foreach (var stringTable in tables) { reporter?.ReportProgress($"Exporting {stringTable.name}", progress); progress += taskStep; var doc = CreateDocument(sourceLanguage.LocaleIdentifier, stringTable.LocaleIdentifier, version); AddTableToDocument(doc, sourceLanguage, stringTable); var cleanName = CleanFileName(stringTable.name); var fileName = $"{cleanName}.xlf"; var filePath = Path.Combine(directory, fileName); using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { doc.Serialize(stream); } } reporter?.Completed($"Finished exporting"); } catch (Exception e) { reporter?.Fail(e.Message); throw; } }
private void SaveSourceLanguage(XliffVersion xliffVersion) { switch (xliffVersion) { case XliffVersion.V12: { SourceLanguage = XmlDocument.DocumentElement.SelectSingleNode($"{NAMESPACE_PREFIX}:{Constants.XML_NODE_FILE}", XmlNamespaceManager)?.Attributes.GetNamedItem(Constants.XML_ATTRIBUTE_SOURCE_LANGUAGE_V12)?.Value ?? string.Empty; break; } case XliffVersion.V20: { SourceLanguage = XmlDocument.DocumentElement?.Attributes.GetNamedItem(Constants.XML_ATTRIBUTE_SOURCE_LANGUAGE_V20)?.Value ?? string.Empty; break; } default: throw new NotImplementedException("Not implemented XliffVersion"); } }
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); }
private string EncodeAndCleanValue(string str, XliffVersion xliffVersion) { switch (xliffVersion) { case XliffVersion.V12: { str = str.Replace(" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\" ", string.Empty); break; } case XliffVersion.V20: { str = str.Replace(" xmlns=\"urn:oasis:names:tc:xliff:document:2.0\" ", string.Empty); break; } default: throw new NotImplementedException("Not implemented XliffVersion"); } return(str.Replace("<", "_LT;_").Replace(">", "_GT;_")); }
/// <summary> /// Exports all <see cref="StringTable"/> in <paramref name="collections"/> as 1 or more XLIFF files where each file represents a single language. /// </summary> /// <param name="source">This is the language that will be used as the source language for all generated XLIFF files.</param> /// <param name="directory">The directory to output the generated XLIFF files.</param> /// <param name="name">The default name for all generated XLIFF files. Files will be saved with the full name "[name]_[Language Code].xlf"</param> /// <param name="version">The XLIFF version to generate the files in.</param> /// <param name="collections">1 or more <see cref="StringTableCollection"/>. The collections will be combines into language groups where each file represents a single </param> /// <param name="reporter">Optional reporter which can report the current progress.</param> public static void Export(LocaleIdentifier source, string directory, string name, XliffVersion version, ICollection <StringTableCollection> collections, ITaskReporter reporter = null) { if (collections == null) { throw new ArgumentNullException(nameof(collections)); } var dict = new Dictionary <StringTableCollection, HashSet <int> >(); foreach (var c in collections) { dict[c] = new HashSet <int>(Enumerable.Range(0, c.StringTables.Count)); } ExportSelected(source, directory, name, version, dict, reporter); }
internal static void ExportSelected(LocaleIdentifier source, string dir, string name, XliffVersion version, Dictionary <StringTableCollection, HashSet <int> > collectionsWithSelectedIndexes, ITaskReporter reporter = null) { var documents = DictionaryPool <LocaleIdentifier, IXliffDocument> .Get(); try { // Used for reporting int totalTasks = collectionsWithSelectedIndexes.Sum(c => c.Value.Count); float taskStep = 1.0f / (totalTasks * 2.0f); float progress = 0; reporter?.Start($"Exporting {totalTasks} String Tables to XLIFF", string.Empty); foreach (var kvp in collectionsWithSelectedIndexes) { var stringTableCollection = kvp.Key; var sourceTable = stringTableCollection.GetTable(source) as StringTable; if (sourceTable == null) { var message = $"Collection {stringTableCollection.TableCollectionName} does not contain a table for the source language {source}"; reporter?.Fail(message); throw new Exception(message); } foreach (var stringTableIndex in kvp.Value) { var stringTable = stringTableCollection.StringTables[stringTableIndex]; reporter?.ReportProgress($"Generating document for {stringTable.name}", progress); progress += taskStep; if (!documents.TryGetValue(stringTable.LocaleIdentifier, out var targetDoc)) { targetDoc = CreateDocument(source, stringTable.LocaleIdentifier, version); documents[stringTable.LocaleIdentifier] = targetDoc; } AddTableToDocument(targetDoc, sourceTable, stringTable); } } // Now write the files foreach (var doc in documents) { var cleanName = CleanFileName(name); var fileName = $"{cleanName}_{doc.Key.Code}.xlf"; var filePath = Path.Combine(dir, fileName); reporter?.ReportProgress($"Writing {fileName}", progress); progress += taskStep; using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { doc.Value.Serialize(stream); } } reporter?.Completed($"Finished exporting"); } catch (Exception e) { reporter?.Fail(e.Message); throw; } finally { DictionaryPool <LocaleIdentifier, IXliffDocument> .Release(documents); } }
/// <summary> /// Creates an empty XLIFF document ready for populating. /// </summary> /// <param name="source">The source language. The language used when populating <see cref="ITranslationUnit.Source"/>.</param> /// <param name="target">The target language. The language used when populating <see cref="ITranslationUnit.Target"/>.</param> /// <param name="version">The XLIFF file version.</param> /// <returns></returns> public static IXliffDocument CreateDocument(LocaleIdentifier source, LocaleIdentifier target, XliffVersion version) { var doc = XliffDocument.Create(version); doc.SourceLanguage = source.Code; doc.TargetLanguage = target.Code; return(doc); }