Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Creating String/Asset Tables Programmatically

Discussion in 'Localization Tools' started by james7132, Nov 5, 2019.

  1. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    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.
     
    karl_jones likes this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,368
    Can you give me an example of the data in these sheets? Ill write an example for you.
     
  3. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    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.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,368
    Ok heres something to get you started https://gist.github.com/karljj1/f3a204b69280cf01f70c8f260e233f91

    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.
     
  5. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    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:
    https://github.com/HouraiTeahouse/HouraiLocalization/blob/master/Editor/LocalizationGenerator.cs#L67

    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.
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,368
    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?
     
  7. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    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).
     
    Last edited: Nov 7, 2019
    karl_jones likes this.
  8. jaine-wond

    jaine-wond

    Joined:
    Nov 9, 2020
    Posts:
    29
    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?
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,368
  10. jaine-wond

    jaine-wond

    Joined:
    Nov 9, 2020
    Posts:
    29
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,368
    You can't assign to the columns but you can do

    googleSheetsExtension.Columns.AddRange( columns)
     
  12. jaine-wond

    jaine-wond

    Joined:
    Nov 9, 2020
    Posts:
    29
    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?
     
  13. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,368
    Oh yes try setting the collection dirty using EditorUtility.SetDirty.
     
  14. jaine-wond

    jaine-wond

    Joined:
    Nov 9, 2020
    Posts:
    29
    Tried that, and also tried setting the serviceProvider as dirty, but still got the type mismatch
     
  15. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,368
    Oh I missed it. You need to save the provider as an asset. AssetDatabase.CreateAsset
     
  16. jaine-wond

    jaine-wond

    Joined:
    Nov 9, 2020
    Posts:
    29
    Works! Thank you so much :)
     
    karl_jones likes this.