docs.unity3d.com
    Show / Hide Table of Contents

    Class Csv

    Comma Separated Values (CSV) support. Used to transfer localized data and carry it from one step of the localization process to the other, while allowing interoperability between and among tools.

    Inheritance
    Object
    Csv
    Namespace: UnityEditor.Localization.Plugins.CSV
    Syntax
    public static class Csv

    Methods

    Export(TextWriter, StringTableCollection, IList<CsvColumns>, ITaskReporter)

    Exports a StringTableCollection using columnMappings to control the contents of each exported column. CreateDefaultMapping(Boolean).

    Declaration
    public static void Export(TextWriter writer, StringTableCollection collection, IList<CsvColumns> columnMappings, ITaskReporter reporter = null)
    Parameters
    Type Name Description
    TextWriter writer

    The target that will be populated with CSV data.

    StringTableCollection collection

    The collection to export to CSV.

    IList<CsvColumns> columnMappings

    Controls what will be exported. The KeyIdColumns can be used to export the Key, Id and shared comments whilst LocaleColumns can be used to export the values and comments for a specific Locale

    ITaskReporter reporter

    An optional reporter that can be used to provide feedback during export.

    Examples

    This example shows how to configure the data you wish to export in CSV through column mappings.

        var collection = LocalizationEditorSettings.GetStringTableCollection("My Strings");
    
        // Use custom column mappings to control what data gets exported
        var columnMappings = new List<CsvColumns>();
    
        // We must have 1 KeyIdColumns so that we can map the table entries correctly
        columnMappings.Add(new KeyIdColumns
        {
            IncludeId = true, // Include the Id column field.
            IncludeSharedComments = true, // Include Shared comments.
        });
    
        // Export English with no comments
        columnMappings.Add(new LocaleColumns
        {
            LocaleIdentifier = "en",
            IncludeComments = false
        });
    
        // Export Japanese with comments
        columnMappings.Add(new LocaleColumns
        {
            LocaleIdentifier = SystemLanguage.Japanese,
            IncludeComments = true
        });
    
        // Now export
        using (var stream = new StreamWriter("My Strings CSV.csv", false, Encoding.UTF8))
        {
            Csv.Export(stream, collection, columnMappings);
        }

    This example shows how to export every StringTableCollection that contains a CsvExtension.

    [MenuItem("Localization/CSV/Export All CSV Files(With Extensions)")]
    public static void PullAllExtensions()
    {
        // Get every String Table Collection
        var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();
    
        foreach (var collection in stringTableCollections)
        {
            // Its possible a String Table Collection may have more than one extension.
            foreach (var extension in collection.Extensions)
            {
                if (extension is CsvExtension csvExtension)
                {
                    if (!string.IsNullOrEmpty(csvExtension.File))
                    {
                        using (var stream = new StreamWriter(csvExtension.File, false, Encoding.UTF8))
                        {
                            Csv.Export(stream, collection, csvExtension.Columns);
                        }
                    }
                }
            }
        }
    }
    See Also
    CreateDefaultMapping(Boolean)

    Export(TextWriter, StringTableCollection, ITaskReporter)

    Exports all StringTable in collection using default column mappings generated through CreateDefaultMapping(Boolean).

    Declaration
    public static void Export(TextWriter writer, StringTableCollection collection, ITaskReporter reporter = null)
    Parameters
    Type Name Description
    TextWriter writer

    The target that will be populated with CSV data.

    StringTableCollection collection

    The collection to export to CSV.

    ITaskReporter reporter

    An optional reporter that can be used to provide feedback during export.

    Examples

    This example shows how to export a StringTableCollection to a CSV file.

        var collection = LocalizationEditorSettings.GetStringTableCollection("My Strings");
        using (var stream = new StreamWriter("My Strings CSV.csv", false, Encoding.UTF8))
        {
            Csv.Export(stream, collection);
        }

    ImportInto(TextReader, StringTableCollection, Boolean, ITaskReporter)

    Import the CSV data into collection using columnMappings to control what data will be imported. See KeyIdColumns and LocaleColumns for further details.

    Declaration
    public static void ImportInto(TextReader reader, StringTableCollection collection, bool createUndo = false, ITaskReporter reporter = null)
    Parameters
    Type Name Description
    TextReader reader

    The source of the CSV data.

    StringTableCollection collection

    The target collection to be updated using the CSV data.

    Boolean createUndo

    Should an Undo operation be created so the changes can be undone?

    ITaskReporter reporter

    An optional reporter that can be used to provide feedback during import.

    Examples

    This example show how to import a collection with default settings.

        var collection = LocalizationEditorSettings.GetStringTableCollection("My Strings");
        using (var stream = new StreamReader("My Strings CSV.csv"))
        {
            Csv.ImportInto(stream, collection);
        }

    ImportInto(TextReader, StringTableCollection, IList<CsvColumns>, Boolean, ITaskReporter)

    Import the CSV data into collection using default column mappings generated using CreateDefaultMapping(Boolean).

    Declaration
    public static void ImportInto(TextReader reader, StringTableCollection collection, IList<CsvColumns> columnMappings, bool createUndo = false, ITaskReporter reporter = null)
    Parameters
    Type Name Description
    TextReader reader
    StringTableCollection collection
    IList<CsvColumns> columnMappings
    Boolean createUndo
    ITaskReporter reporter
    Examples

    This example shows how to configure the data you wish to import in CSV through column mappings.

        var collection = LocalizationEditorSettings.GetStringTableCollection("My Strings");
    
        // Use custom column mappings to control what data gets imported.
        var columnMappings = new List<CsvColumns>();
    
        // We must have 1 KeyIdColumns so that we can map the table entries correctly
        columnMappings.Add(new KeyIdColumns
        {
            IncludeId = true, // Include the Id column field.
            IncludeSharedComments = true, // Include Shared comments.
        });
    
        // Export English with no comments
        columnMappings.Add(new LocaleColumns
        {
            LocaleIdentifier = "en",
            IncludeComments = false
        });
    
        // Export Japanese with comments
        columnMappings.Add(new LocaleColumns
        {
            LocaleIdentifier = SystemLanguage.Japanese,
            IncludeComments = true
        });
    
        using (var stream = new StreamReader("My Strings CSV.csv"))
        {
            Csv.ImportInto(stream, collection, columnMappings);
        }

    This example shows how to import every StringTableCollection that contains a CsvExtension.

    [MenuItem("Localization/CSV/Import All CSV Files")]
    public static void ImportAllExtensions()
    {
        // Get every String Table Collection
        var stringTableCollections = LocalizationEditorSettings.GetStringTableCollections();
    
        foreach (var collection in stringTableCollections)
        {
            // Its possible a String Table Collection may have more than one extension.
            foreach (var extension in collection.Extensions)
            {
                if (extension is CsvExtension csvExtension)
                {
                    if (!string.IsNullOrEmpty(csvExtension.File) && File.Exists(csvExtension.File))
                    {
                        using (var stream = new StreamReader(csvExtension.File))
                        {
                            Csv.ImportInto(stream, collection, csvExtension.Columns);
                        }
                    }
                }
            }
        }
    }
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023