Created
November 5, 2019 13:04
-
-
Save karljj1/f3a204b69280cf01f70c8f260e233f91 to your computer and use it in GitHub Desktop.
Example of creating/updating StringTables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using UnityEditor; | |
using UnityEditor.Localization; | |
using UnityEngine; | |
using UnityEngine.Localization.Tables; | |
public class GenerateStringTables : ScriptableWizard | |
{ | |
public TextAsset jsonFile; | |
public string tableCollectionName = "My Strings"; | |
[MenuItem("Localization Example/Import Json")] | |
static void CreateWizard() | |
{ | |
DisplayWizard<GenerateStringTables>("Import"); | |
} | |
static AssetTableCollection FindStringTableCollection(string name) | |
{ | |
var allCollections = LocalizationEditorSettings.GetAssetTablesCollection<StringTable>(); | |
return allCollections.FirstOrDefault(col => col.TableName == name); | |
} | |
void OnWizardCreate() | |
{ | |
// You will need to create the StringTables in the editor as we have not exposed `CreateAssetTableCollection`. | |
var foundCollection = FindStringTableCollection(tableCollectionName); | |
if (foundCollection == null) | |
{ | |
Debug.LogError($"Please create a StringTable collection with the namme `{tableCollectionName}`"); | |
return; | |
} | |
// TODO: Load your Json data here | |
foreach(var table in foundCollection.Tables) | |
{ | |
StringTable stringTable = table as StringTable; | |
// Assuming you have multiple json files in the form "de.json". | |
// Find the json file by using: | |
var languageCode = stringTable.LocaleIdentifier.Code; // For german this would be `de`. | |
/* | |
foreach(var entry in yourJsonFile) | |
{ | |
stringTable.AddEntry(entry.Name, entry.TranslatedValue); | |
} | |
*/ | |
// Mark the table dirty | |
EditorUtility.SetDirty(table); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment