- Home /
How to localize an array of strings in script.
Hi,
I am trying to get localization with the Localization package to work with some strings in my script. But I'm not sure how to achieve that. This is what I've got so far:
[SerializeField] private LocalizedString[] myStrings;
private string[] localizedTexts = new string[16];
private void OnEnable()
{
for (int i = 0; i < myStrings.Length; i++)
{
myStrings[i].StringChanged += UpdateString;
}
}
private void OnDisable()
{
for (int i = 0; i < myStrings.Length; i++)
{
myStrings[i].StringChanged -= UpdateString;
}
}
private void UpdateString(string s)
{
localizedTexts[0] = s;
}
What I want to accomplish is for the localized text in myStrings at a given index to go into localizedTexts at the same index. E.g. the localized text in myStrings[4] should be passed to localizedTexts[4], instead of always being passed to index 0 as the script is written now.
I've never accessed Unity's localization system through scripting before, so I do not know of any preferred practices.
Answer by Hellium · Feb 24 at 08:57 PM
CODE NOT TESTED
[SerializeField] private LocalizedString[] myStrings;
private ChangeHandler[] callbacks;
private string[] localizedTexts;
private void OnEnable()
{
callbacks = new ChangeHandler[myStrings.Length];
localizedTexts = new string[myStrings.Length];
for (int i = 0; i < myStrings.Length; i++)
{
int index = i;// needed to avoid closure problem
callbacks[i] = delegate(string s){ localizedTexts[index] = s; }
myStrings[i].StringChanged += callbacks[i];
}
}
private void OnDisable()
{
for (int i = 0; i < myStrings.Length; i++)
{
myStrings[i].StringChanged -= callbacks[i];
}
}
Your answer
Follow this Question
Related Questions
Non-ASCII characters in Input.inputString in OSX 0 Answers
Localized audio with Handheld.PlayFullScreenMovie() 1 Answer
Issues with iOS Build that are not visible in Editor 0 Answers
Why is localization option not apearing in game build? 1 Answer
Different app name for different languages (Android). 2 Answers