I’m trying to implement localization for my game, which takes in a csv file in the StreamingAssets folder named after the languages (e.g. English.csv). I parse this file into a dictionary, giving me access using the key to get the translated value to output to the text component. I have a separate script which I want to add on to an event that calls for localization when the game starts or whenever the language is changed. This script is then attached to the text gameobject and changes the text based on the given key when the event is invoked.
However, some of my gameobjects require themselves disabled at the start (e.g. the inventory of the player). This makes them unable to receive the event subscription. My dictionary is stored in a LocalizationManager class, which is a static singleton generated on Awake(). How do I let disabled objects receive the event call?
The normal way to do this is where you display the text, it makes the call to your localization manager to get the localized text. Much simpler, more robust, more efficient.
Hmm how would I go about making the call? If I have three different texts under a disabled game object, I don’t want to call all the children in succession to localize their text. It would be great if events worked…
First off create a proper singleton or just use a static method so you don’t have to check if it exists. And create a method to get your string don’t just reference it directly like that.
Better would be to abstract that out. So subclass the text class and add a SetText(string key) method to it.
public void SetText(string key) {
text = LocalizationManager.instance.GetString(key);
}
Your localization doesn’t care about when a component is active, it’s not it’s business to know that. Your component has it’s own logic for when it actually sets the text. Separation of concerns and all.