Creating String/Asset Tables Programmatically

I already have localized strings in the form of JSON files ripped off a shared Google Sheet with my localizers. How do I turn these into String Tables? I know Google Sheets support is coming in 2020, but I am looking to swap out a less than complete custom solution for one that is first party in the meantime.

1 Like

Can you give me an example of the data in these sheets? Ill write an example for you.

It’s a straight mapping from English to the target language, with a different JSON file for each language, which the name is the language code and file extension: “de.json” → German.

Ok heres something to get you started Example of creating/updating StringTables · GitHub

It seems that we made a key API internal (CreateAssetTableCollection), we will fix that in the next release.
For now you should create some empty StringTables in the editor for the languages your JSON files support and then run this script to sync the data.

Reworked your example into something that turns a Google Sheets equivalent into a AssetTableCollection, decided to just skip the JSON files and just generate from the source:

It’s actually pretty unwieldy to properly clear a LocalizedTable. The publicly exposed dictionary is not synchronized with the serialized entry list, so calling Clear on it can result in invalid serialization state when adding entries after “clearing” it.

Sharing for others to use in the mean time until first party support is added for this kind of functionality.

Thanks for the example.
I see the problem. We could add a Clear function to clear both the dictionary and the list or we could just sync the dictionary over to the list during OnBeforeSerialize. What would you prefer?

It would be preferable if LocalizedTableT implemented IDictionary<uint, T> or something similar. The exposed public table and lists of entries in LocalizedTableT and it’s derivatives makes it easy for developers to break the internal state. Implementing the interface would expose a more controlled API surface, and it’d come with other benefits (i.e. more succinct integration with System.Linq).

1 Like

Waking up this old thread on the same topic: CreateStringTableCollection works nicely, but if I want to create a GoogleSheetsExtension for a StringTable, I can’t set the Columns to it as it only allows get?

You can set the columns via the extension or pass in the columns when you call push/pull.
https://docs.unity3d.com/Packages/com.unity.localization@1.4/api/UnityEditor.Localization.Plugins.Google.GoogleSheetsExtension.html

I would like to fully create the tables from code, like this: static void PullSheet(SheetsServiceProvider sheetsServiceProvider, string spread - Pastebin.com.

You can’t assign to the columns but you can do

googleSheetsExtension.Columns.AddRange( columns)

Ah, perfect, thanks! Now the only problem that remains is that the String Table Collection is missing the Sheets Service Provides after it’s been written to disk, it’s just displaying “Type mismatch” in that field. Should I somehow save the SheetServiceProvider to disk too?

Oh yes try setting the collection dirty using EditorUtility.SetDirty.

Tried that, and also tried setting the serviceProvider as dirty, but still got the type mismatch

Oh I missed it. You need to save the provider as an asset. AssetDatabase.CreateAsset

Works! Thank you so much :slight_smile:

1 Like