Ali Şahan Yalçın

Unity Localize Dropdown Component

·2 min read
UnityC#Localization
Unity Localize Dropdown Component

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.

Right-click on the Dropdown Component and select the Localize option at the bottom.

In the Localize Dropdown Component, we add items corresponding to the Dropdown options, and the final result will be as follows:

LocalizeDropdown.cs

csharp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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.