/// <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; } }
static int AddMetadataCommentsFromNotes(INoteCollection notes, IMetadataCollection metadata, NoteType requiredNoteType, ImportNotesBehavior importNotes) { if (importNotes == ImportNotesBehavior.Ignore) { return(0); } int count = 0; using (ListPool <Comment> .Get(out var comments)) { metadata.GetMetadatas <Comment>(comments); if (importNotes == ImportNotesBehavior.Replace) { foreach (var com in comments) { metadata.RemoveMetadata(com); } comments.Clear(); } for (int i = 0; i < notes.NoteCount; ++i) { var n = notes.GetNote(i); if (n.AppliesTo != requiredNoteType) { continue; } if (importNotes == ImportNotesBehavior.Merge) { // See if the note already exists if (comments.Any(c => c.CommentText == n.NoteText)) { continue; } } // Add a new note metadata.AddMetadata(new Comment { CommentText = n.NoteText }); count++; } } return(count); }
/// <summary> /// Import an XLIFF document into the target table, ignoring <see cref="IXliffDocument.TargetLanguage"/>. /// </summary> /// <param name="document">The XLIFF document to import.</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 ImportDocumentIntoTable(IXliffDocument document, StringTable target, ImportNotesBehavior importNotesBehavior = ImportNotesBehavior.Replace, ITaskReporter reporter = null) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } EditorUtility.SetDirty(target); float progress = reporter == null ? 0 : reporter.CurrentProgress + 0.1f; reporter?.ReportProgress("Importing XLIFF into table", progress); float progressStep = document.FileCount / (1.0f - progress); var options = new ImportOptions { UpdateSourceTable = false, ImportNotes = importNotesBehavior }; 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); ImportIntoTables(f, null, target, options); } var collection = LocalizationEditorSettings.GetCollectionFromTable(target); if (collection != null) { LocalizationEditorSettings.EditorEvents.RaiseCollectionModified(document, collection); } reporter?.Completed("Finished importing XLIFF"); }