Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question How can I do column mapping for GoogleSheet in Localization?

Discussion in 'Localization Tools' started by magister_yoda_, Mar 2, 2023.

  1. magister_yoda_

    magister_yoda_

    Joined:
    Mar 27, 2016
    Posts:
    26
    Hello. I'm using Unity Localization and I have such situation: I have about 30 StringTableCollection-s which I should translate to over 10 languages. Translations I get from a GoogleSheet via GoogleSheetExtension in Unity. So, in this extencion I have to map "locale" to "column" in google sheet. As you can see, I have to map 10 locales in 30 table collections manually. And in inspector I cannot copy-paste 'component' GoogleSheetExtension. Is there any way to automate this tedious process? Can I someway copy columns mapping and paste it to another extension?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
  3. magister_yoda_

    magister_yoda_

    Joined:
    Mar 27, 2016
    Posts:
    26
    First advice is OK, but I only have "Add default column" and don't have "Extract Column..." option



    Did I understand correctly, that using this script I can change data in google sheet extension in editor? Or it's suitable for runtime setup?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
    The "Extract Columns From Sheet" menu item requires OAuth access, ill update the docs to point this out. Are you using an API key?
    The scripts are only suitable for Editor, we dont have player support however we do support updating tables in the player after a build. This can be done by either using an Addressables content update or by using the ITablePostprocessor system
     
  5. magister_yoda_

    magister_yoda_

    Joined:
    Mar 27, 2016
    Posts:
    26
    Thanks, that's clear.
    And can you prompt, how can I use this script to access the GoogleSheetProvider and map columns to locales?

    P.S. Yeap, I'm using API key for google sheets
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
    Here is an example to add a configured extension

    Code (csharp):
    1. public static class SetupExtensionSample
    2. {
    3.     [MenuItem("CONTEXT/StringTableCollection/Add Configured Google Sheet Extension")]
    4.     public static void AddAndConfigureExtension(MenuCommand command)
    5.     {
    6.         var collection = command.context as StringTableCollection;
    7.  
    8.         // Get or add a new GoogleSheetsExtension.
    9.         var googleExtension = collection.Extensions.FirstOrDefault(e => e is GoogleSheetsExtension) as GoogleSheetsExtension;
    10.         if (googleExtension == null)
    11.         {
    12.             googleExtension = new GoogleSheetsExtension();
    13.             collection.AddExtension(googleExtension);
    14.         }
    15.  
    16.         // Clear old data.
    17.         googleExtension.Columns.Clear();
    18.  
    19.         // We need configure what each column will contain in the sheet
    20.         var columnMappings = new List<SheetColumn>
    21.         {
    22.             // Column A will contain the Key
    23.             new KeyColumn { Column = "A" },
    24.  
    25.             // Column B will contain any shared comments. These are Comment Metadata in the Shared category.
    26.             new KeyCommentColumn { Column = "B" },
    27.  
    28.             // Column C will contain the English Locale and any comments that are just for this Locale.
    29.             new LocaleColumn { Column = "C", LocaleIdentifier = "en", IncludeComments = true },
    30.         };
    31.  
    32.         // Assign the columns to the extension
    33.         googleExtension.Columns.AddRange(columnMappings);
    34.  
    35.         // Assign our Google Sheets service asset
    36.         const string pathToYourAsset = "Assets/Google Sheets Service.asset"; //The path to your SheetsServiceProvider asset. See docs for further info.
    37.         var sheetsServiceProvider = AssetDatabase.LoadAssetAtPath<SheetsServiceProvider>(pathToYourAsset);
    38.         googleExtension.SheetsServiceProvider = sheetsServiceProvider;
    39.  
    40.         googleExtension.SpreadsheetId = "My spread sheet id"; // We need to provide the Spreadsheet id. This can be found in the url. See docs for further info.
    41.         googleExtension.SheetId = 123456; // This is the id of the sheet in the Google Spreadsheet. it will be in the url after `gid=`.
    42.  
    43.         // Mark the collection dirty so that the changes are saved
    44.         EditorUtility.SetDirty(collection);
    45.     }
    46. }
     
  7. magister_yoda_

    magister_yoda_

    Joined:
    Mar 27, 2016
    Posts:
    26
    Oh, thank you very much, that's exactly what I needed! I also modified your code and I get copy-paste functionality for this extension

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using UnityEditor;
    4. using UnityEditor.Localization;
    5. using UnityEditor.Localization.Plugins.Google;
    6. using UnityEditor.Localization.Plugins.Google.Columns;
    7.  
    8. public static class TableCollectionFuncExtension {
    9.  
    10.     private static List<SheetColumn> BUFFER;
    11.     private static string SPREADSHEET_ID;
    12.  
    13.     [MenuItem("CONTEXT/StringTableCollection/Copy GoogleSheetExtension data")]
    14.     public static void CopyData(MenuCommand command) {
    15.         var collection = command.context as StringTableCollection;
    16.      
    17.         var googleExtension = collection.Extensions.FirstOrDefault(e => e is GoogleSheetsExtension) as GoogleSheetsExtension;
    18.         if (googleExtension != null) {
    19.             BUFFER = new List<SheetColumn>();
    20.             foreach (var column in googleExtension.Columns) {
    21.                 if (column is KeyColumn keyColumn) {
    22.                     BUFFER.Add(new KeyColumn {Column = keyColumn.Column});
    23.                 } else if (column is LocaleColumn locColumn) {
    24.                     BUFFER.Add(new LocaleColumn {Column = locColumn.Column, LocaleIdentifier = locColumn.LocaleIdentifier});
    25.                 }
    26.             }
    27.  
    28.             SPREADSHEET_ID = googleExtension.SpreadsheetId;
    29.  
    30.         }
    31.     }
    32.  
    33.     [MenuItem("CONTEXT/StringTableCollection/Paste GoogleSheetExtension data")]
    34.     public static void PasteData(MenuCommand command) {
    35.         var collection = command.context as StringTableCollection;
    36.      
    37.         var googleExtension = collection.Extensions.FirstOrDefault(e => e is GoogleSheetsExtension) as GoogleSheetsExtension;
    38.         if (googleExtension == null) {
    39.             googleExtension = new GoogleSheetsExtension();
    40.             collection.AddExtension(googleExtension);
    41.         }
    42.  
    43.         var columnsList = new List<SheetColumn>();
    44.         foreach (var column in BUFFER) {
    45.             if (column is KeyColumn keyColumn) {
    46.                 columnsList.Add(new KeyColumn {Column = keyColumn.Column});
    47.             } else if (column is LocaleColumn locColumn) {
    48.                 columnsList.Add(new LocaleColumn {Column = locColumn.Column, LocaleIdentifier = locColumn.LocaleIdentifier});
    49.             }
    50.         }
    51.      
    52.      
    53.         //paste values to the extension
    54.         const string pathToYourAsset = "Assets/Localization/Tables/Google Sheet Extension.asset"; //The path to your SheetsServiceProvider asset. See docs for further info.
    55.         var sheetsServiceProvider = AssetDatabase.LoadAssetAtPath<SheetsServiceProvider>(pathToYourAsset);
    56.         googleExtension.SheetsServiceProvider = sheetsServiceProvider;
    57.      
    58.         googleExtension.SpreadsheetId = SPREADSHEET_ID;
    59.         googleExtension.Columns.AddRange(columnsList);
    60.      
    61.         EditorUtility.SetDirty(collection);
    62.     }
    63. }
     
    karl_jones likes this.
  8. magister_yoda_

    magister_yoda_

    Joined:
    Mar 27, 2016
    Posts:
    26
    Can you answer one more question? When should I use this code??
    Code (CSharp):
    1. LocalizationSettings.InitializationOperation
    I'm passing localized string keys to prefabs and scriptable objects, and I don't know at which point should I initialize localization.
    Now I don't call 'LocalizationSettings.InitializationOperation' and everything works fine.
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,388
    It's if you need to wait for the system to initialise. By default it will be waited on internally. If everything is working then it's fine :)