Changing localized label entry key using C#

Hello. I’m trying to dynamically change a localized label in the UI, using the UI Toolkit. I’m using the Unity 6000.0.23f1. I followed the docs page on localization in the UI Toolkit and managed to add a binding of type Localized String. When I select the localized string, such as brothers_turn, in my localization table called main, the label does update with the localized text as expected in the UI Builder window. However, when I start the game and trigger the code below, the label says ‘No translation found’ rather than the expected text.

Here’s what I tried:

currentTurnText = ui.Q<Label>("CurrentTurnText");

var binding = currentTurnText.GetBinding("text") as LocalizedString;

binding.TableReference = "main";
binding.TableEntryReference = "sisters_turn";
binding.RefreshString();

That didn’t work, though. On the docs page, it goes into using variables, but I simply want to change the localization table entry that is applied to the label. How can I change the localized string entry from, say, main/brothers_turn to main/sisters_turn?

It also says ‘Unresolved binding’, which I don’t know how to solve.

image

Project Settings → Localization

I must be doing something wrong. Any help in figuring out how to do this the proper way would be much appreciated!

Are the 2 strings in the same table?
Setting each value (TableReference & TableEntryReference ) here triggers an automatic refresh, so changing the table first could cause it to look in the wrong table
Do

binding.SetReference("main", "sisters_turn");

SetReference will trigger the automatic update, no need to call RefreshString.

Thank you so much! That worked flawlessly! I appreciate the help. :blue_heart:

var binding = currentTurnText.GetBinding("text") as LocalizedString;
binding.SetReference("main", "sisters_turn");
1 Like