We cannot localize the dropdown component using com.unity.localization
library, so I will show you how to localize the dropdown component in Unity.
WARNING
I assume you have already added the com.unity.localization
package in your project, setup Localization Table and set up the content.
Localize
option at the bottom.Localize Dropdown Component
, we add items corresponding to the Dropdown options, and the final result will be as follows:using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using TMPro;
using UnityEditor;
public class LocalizeDropdown : MonoBehaviour
{
[SerializeField] private List dropdownOptions;
private TMP_Dropdown _tmpDropdown;
private void Awake()
{
if (_tmpDropdown == null)
_tmpDropdown = GetComponent();
LocalizationSettings.SelectedLocaleChanged += ChangedLocale;
}
private void ChangedLocale(Locale newLocale)
{
List tmpDropdownOptions = new List();
foreach (var dropdownOption in dropdownOptions)
{
tmpDropdownOptions.Add(new TMP_Dropdown.OptionData(dropdownOption.GetLocalizedString()));
}
_tmpDropdown.options = tmpDropdownOptions;
}
}
public abstract class AddLocalizeDropdown
{
[MenuItem("CONTEXT/TMP_Dropdown/Localize", false, 1)]
private static void AddLocalizeComponent()
{
// add localize dropdown component to selected gameobject
GameObject selected = Selection.activeGameObject;
if (selected != null)
{
selected.AddComponent();
}
}
}
Translation is being performed between lines 8
and 30
, and a Localize Dropdown Component is being added between lines 32
and 44
.