/// <summary>
        /// Exports a <see cref="StringTableCollection"/> using <paramref name="columnMappings"/> to control the contents of each exported column.
        /// <see cref="ColumnMapping.CreateDefaultMapping(bool)"/>.
        /// </summary>
        /// <param name="writer">The target that will be populated with CSV data.</param>
        /// <param name="collection">The collection to export to CSV.</param>
        /// <param name="cellMappings">Controls what will be exported.
        /// The <seealso cref="KeyIdColumns"/> can be used to export the Key, Id and shared comments whilst <seealso cref="LocaleColumns"/> can be
        /// used to export the values and comments for a specific <see cref="UnityEngine.Localization.Locale"/></param>.
        /// <seealso cref="ColumnMapping.CreateDefaultMapping(bool)"/> can be used to generate the default columns for the project.
        /// <param name="reporter">An optional reporter that can be used to provide feedback during export.</param>
        public static void Export(TextWriter writer, StringTableCollection collection, IList <CsvColumns> columnMappings, ITaskReporter reporter = null)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }
            VerifyColumnMappings(columnMappings);

            using (var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture))
            {
                try
                {
                    reporter?.Start("Exporting CSV", string.Empty);
                    reporter?.ReportProgress("Writing Headers", 0);
                    foreach (var cell in columnMappings)
                    {
                        cell.WriteBegin(collection, csvWriter);
                    }

                    reporter?.ReportProgress("Writing Contents", 0.1f);
                    foreach (var row in collection.GetRowEnumerator())
                    {
                        if (row.TableEntries[0] != null && row.TableEntries[0].SharedEntry.Metadata.HasMetadata <ExcludeEntryFromExport>())
                        {
                            continue;
                        }

                        csvWriter.NextRecord();
                        foreach (var cell in columnMappings)
                        {
                            cell.WriteRow(row.KeyEntry, row.TableEntries, csvWriter);
                        }
                    }

                    foreach (var cell in columnMappings)
                    {
                        cell.WriteEnd(collection);
                    }

                    reporter?.Completed("Finished Exporting");
                }
                catch (Exception e)
                {
                    reporter?.Fail("Failed Exporting.\n" + e.Message);
                    throw;
                }
            }
        }
Example #2
0
        /// <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);
                }
            }
        }