/// <summary> /// Import an XLIFF file into the target table, ignoring <see cref="IXliffDocument.TargetLanguage"/>. /// </summary> /// <param name="file">The XLIFF file path.</param> /// <param name="target">The target table that will be populated with the translated values.</param> /// <param name="importNotesBehavior">How should the notes be imported?</param> /// <param name="reporter">Optional reporter which can report the current progress.</param> public static void ImportFileIntoTable(string file, StringTable target, ImportNotesBehavior importNotesBehavior = ImportNotesBehavior.Replace, ITaskReporter reporter = null) { if (target == null) { throw new ArgumentNullException(nameof(target)); } reporter?.Start("Importing XLIFF", $"Importing {file}"); try { if (!File.Exists(file)) { throw new FileNotFoundException($"Could not find file {file}"); } using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { reporter?.ReportProgress("Parsing XLIFF", 0.1f); var document = XliffDocument.Parse(stream); ImportDocumentIntoTable(document, target, importNotesBehavior, reporter); } } catch (Exception e) { reporter?.Fail(e.Message); throw; } }
/// <summary> /// Import the XLIFF file into the collection. /// </summary> /// <param name="collection">The collection to import all the XLIFF data into.</param> /// <param name="file">The XLIFF file path.</param> /// <param name="importOptions">Optional import options which can be used to configure the importing behavior.</param> /// <param name="reporter">Optional reporter which can report the current progress.</param> public static void ImportFileIntoCollection(StringTableCollection collection, string file, ImportOptions importOptions = null, ITaskReporter reporter = null) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } reporter?.Start("Importing XLIFF", $"Importing {file}"); try { if (!File.Exists(file)) { throw new FileNotFoundException($"Could not find file {file}"); } using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { reporter?.ReportProgress("Parsing XLIFF", 0.1f); var document = XliffDocument.Parse(stream); float progress = 0.3f; reporter?.ReportProgress("Importing XLIFF into project", progress); float progressStep = document.FileCount / 1.0f * 0.7f; var options = importOptions ?? s_DefaultOptions; for (int i = 0; i < document.FileCount; ++i) { var f = document.GetFile(i); progress += progressStep; reporter?.ReportProgress($"Importing({i + 1}/{document.FileCount}) {f.Id}", progress); ImportFileIntoCollection(collection, f, document.SourceLanguage, document.TargetLanguage, options); } reporter?.Completed("Finished importing XLIFF"); } } catch (Exception e) { reporter?.Fail(e.Message); throw; } }
/// <summary> /// Imports a single XLIFF file into the project. /// Attempts to find matching <see cref="StringTableCollection"/>'s, if one could not be found then a new one is created. /// </summary> /// <param name="file">The XLIFF file.</param> /// <param name="importOptions">Optional import options which can be used to configure the importing behavior.</param> /// <param name="reporter">Optional reporter which can report the current progress.</param> public static void ImportFile(string file, ImportOptions importOptions = null, ITaskReporter reporter = null) { reporter?.Start("Importing XLIFF", $"Importing {file}"); try { if (!File.Exists(file)) { throw new FileNotFoundException($"Could not find file {file}"); } using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read)) { reporter?.ReportProgress("Parsing XLIFF", 0.1f); var document = XliffDocument.Parse(stream); ImportDocument(document, importOptions, reporter); } } catch (Exception e) { reporter?.Fail(e.Message); throw; } }