/// <summary> /// Imports a single XLIFF document 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="document">The root XLIFF document.</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 ImportDocument(IXliffDocument document, ImportOptions importOptions = null, ITaskReporter reporter = null) { if (document == null) { throw new ArgumentNullException(nameof(document)); } reporter?.Start("Importing XLIFF", "Importing document"); try { float progress = reporter == null ? 0.1f : reporter.CurrentProgress + 0.1f; reporter?.ReportProgress("Importing XLIFF into project", progress); float progressStep = document.FileCount / (1.0f - progress); 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); ImportFileNode(f, document.SourceLanguage, document.TargetLanguage, options); } reporter?.Completed("Finished importing XLIFF"); } catch (Exception e) { reporter?.Fail(e.Message); throw; } }
/// <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"); }
/// <summary> /// Populate the document with the entries from <paramref name="target"/> using <paramref name="source"/> as the source reference. /// Note: The source and target tables must be part of the same collection, they must both use the same <see cref="SharedTableData"/>. /// </summary> /// <param name="document">The XLIFF document to add the entries to.</param> /// <param name="source"></param> /// <param name="target"></param> public static void AddTableToDocument(IXliffDocument document, StringTable source, StringTable target) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } if (source.SharedData != target.SharedData) { throw new Exception("Source and Target StringTables must be part of the same collection and use the same SharedTableData."); } var file = document.AddNewFile(); var filePath = AssetDatabase.GetAssetPath(target); file.Original = filePath; file.Id = AssetDatabase.AssetPathToGUID(filePath); var group = file.AddNewGroup(); group.Id = TableReference.StringFromGuid(target.SharedData.TableCollectionNameGuid); group.Name = target.SharedData.TableCollectionName; AddNotesFromMetadata(group, target.SharedData.Metadata, NoteType.General); AddNotesFromMetadata(group, source, NoteType.Source); if (source != target) { AddNotesFromMetadata(group, target, NoteType.Target); } foreach (var row in StringTableCollection.GetRowEnumerator(source, target)) { var unit = group.AddNewTranslationUnit(); unit.Id = row.KeyEntry.Id.ToString(); unit.Name = row.KeyEntry.Key; unit.Source = row.TableEntries[0]?.Value; // Dont add a value if its empty. if (row.TableEntries[1] != null && !string.IsNullOrEmpty(row.TableEntries[1].Value)) { unit.Target = row.TableEntries[1].Value; } // Add notes AddNotesFromMetadata(unit, row.KeyEntry.Metadata, NoteType.General); AddNotesFromMetadata(unit, row.TableEntries[0], NoteType.Source); if (source != target) { AddNotesFromMetadata(unit, row.TableEntries[1], NoteType.Target); } } }