In the new UI Builder I am having an issue figuring out how to use localization with dropdown and radio buttons. I have setup localization tables for all the app text and images and that is working great.
I can’t figure out how to link up a localization table to the dropdown & radio button lists/values. Anyone have some insights on how to do that?
Hi,
The Dropdown and RadioButtonGroup use a List<string>
field for their choices. You will need to bind to this field however we dont have a localized binding for List<string>
.
You could create a custom binding to convert a localized value that uses comma separated values into a list and then apply that:
Thanks! I’ve added that and now this is working for radio buttons.
There’s only a small glitch on drop downs where the “select” area does not get updated immediately, it does after selection though.
Thats probably worth a bug report. Changing the choices
should trigger an update.
A workaround would be to try and manually force an update in the StringListBinding.Update
method.
Something like
using System.Linq;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.UIElements;
[UxmlObject]
public partial class StringListBinding : LocalizedString
{
protected override BindingResult Update(in BindingContext context)
{
if (IsEmpty)
return new BindingResult(BindingStatus.Success);
#if UNITY_EDITOR
// When not in playmode and not previewing a language we want to show something, so we revert to the project locale.
if (!Application.isPlaying && LocaleOverride == null && LocalizationSettings.SelectedLocale == null)
{
LocaleOverride = LocalizationSettings.ProjectLocale;
}
#endif
if (!CurrentLoadingOperationHandle.IsDone)
return new BindingResult(BindingStatus.Pending);
var element = context.targetElement;
var index = -1;
if (element is DropdownField df)
{
index = df.index;
}
// Convert to a list
var list = GetLocalizedString().Split(',').ToList();
if (ConverterGroups.TrySetValueGlobal(ref element, context.bindingId, list, out var errorCode))
{
if (df != null)
df.choices = list[index];
return new BindingResult(BindingStatus.Success);
}
return new BindingResult(BindingStatus.Failure);
}
}