static void ImportFileNode(IFile file, LocaleIdentifier source, LocaleIdentifier target, ImportOptions importOptions) { // Find the string table and collection for this file var collection = FindProjectCollection(file); // Import translation units which have no groups. INoteCollection extraNodes = file; if (file.TranslationUnitCount > 0) { if (collection == null) { var dir = importOptions.NewCollectionDirectory; if (string.IsNullOrEmpty(dir)) { dir = EditorUtility.SaveFolderPanel($"Create new String Table Collection {file.Id}", "Assets", file.Id); } if (!string.IsNullOrEmpty(dir)) { var newCollection = LocalizationEditorSettings.CreateStringTableCollection(file.Id, dir); extraNodes = null; ImportFileIntoCollection(newCollection, file, source, target, importOptions); } } else { extraNodes = null; ImportFileIntoCollection(collection, file, source, target, importOptions); } } for (int i = 0; i < file.GroupCount; ++i) { var group = file.GetGroup(i); var groupCollection = FindProjectCollection(group) ?? collection; if (groupCollection == null) { // Use the provided directory otherwise ask the user to provide one var dir = importOptions.NewCollectionDirectory; if (string.IsNullOrEmpty(dir)) { dir = EditorUtility.SaveFolderPanel($"Create new String Table Collection {file.Id}", "Assets", file.Id); if (string.IsNullOrEmpty(dir)) { continue; } } var collectionName = string.IsNullOrEmpty(group.Name) ? group.Id : group.Name; groupCollection = LocalizationEditorSettings.CreateStringTableCollection(collectionName, dir); } ImportGroupIntoCollection(groupCollection, group, extraNodes, source, target, importOptions); } }
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); }
static void AddNotesFromMetadata(INoteCollection noteCollection, IMetadataCollection metadata, NoteType noteType) { // May be null if the entry is missing for the current row if (metadata == null) { return; } using (ListPool <Comment> .Get(out var comments)) { metadata.GetMetadatas <Comment>(comments); foreach (var com in comments) { var note = noteCollection.AddNewNote(); note.AppliesTo = noteType; note.NoteText = com.CommentText; } } }
private static void UpdateNotes(IEnumerable<NoteViewModel> from, ObservableCollection<NoteViewModel> targetCollection, INoteCollection targetViewModel) { targetCollection.Clear(); foreach (var note in from) { targetViewModel.AddNote(new NoteViewModel { Description = note.Description, Type = note.Type }); } }
static void ImportGroupIntoCollection(StringTableCollection collection, IGroup group, INoteCollection extraNotes, LocaleIdentifier source, LocaleIdentifier target, ImportOptions importOptions) { if (collection == null) { throw new ArgumentNullException(nameof(collection)); } var sourceTable = collection.GetTable(source) ?? collection.AddNewTable(source); var targetTable = collection.GetTable(target) ?? collection.AddNewTable(target); // Extract file comments? var generalNotes = AddMetadataCommentsFromNotes(group, collection.SharedData.Metadata, NoteType.General, importOptions.ImportNotes); var sourceNotes = AddMetadataCommentsFromNotes(group, sourceTable, NoteType.Source, importOptions.ImportNotes); int targetNotes = sourceTable != targetTable?AddMetadataCommentsFromNotes(group, targetTable, NoteType.Target, importOptions.ImportNotes) : 0; // If we are importing a group and the file contains notes that were not used then we can include them as extras here. if (extraNotes != null) { // If we imported some notes from the group then we need to switch to merge or we will lose those notes. var overrideBehavior = generalNotes > 0 ? ImportNotesBehavior.Merge : importOptions.ImportNotes; AddMetadataCommentsFromNotes(extraNotes, collection.SharedData.Metadata, NoteType.General, overrideBehavior); overrideBehavior = sourceNotes > 0 ? ImportNotesBehavior.Merge : importOptions.ImportNotes; AddMetadataCommentsFromNotes(extraNotes, sourceTable, NoteType.Source, overrideBehavior); overrideBehavior = targetNotes > 0 ? ImportNotesBehavior.Merge : importOptions.ImportNotes; if (sourceTable != targetTable) { AddMetadataCommentsFromNotes(extraNotes, targetTable, NoteType.Target, overrideBehavior); } } ImportIntoTables(group, sourceTable as StringTable, targetTable as StringTable, importOptions); LocalizationEditorSettings.EditorEvents.RaiseCollectionModified(null, collection); }