Version: Unity 6.7 Beta (6000.7)
Language : English
Localization examples
Display dynamic values in a localized string

Create a language selection menu

Add a language selection menu to a UI Toolkit interface.

This example builds a settings menu where the user picks the language. The two built-in controls don’t need any setup code. They list the enabled locales, follow language changes from anywhere else in the application, and apply the user’s choice to the whole application.

Prerequisites

Before you start, make sure you have the following:

Add the controls

In UI Builder, the controls appear in the library under Controls: Language Dropdown (LanguageDropdown) and Language Radio Button Group (LanguageRadioButtonGroup). Click and drag the control that fits your menu into the Hierarchy pane, or author it in UI Toolkit XML (UXML):

<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:loc="Unity.Localization.UI">
    <ui:VisualElement name="language-settings" style="width: 300px;">
        <loc:LanguageDropdown label="Language" />
        <loc:LanguageRadioButtonGroup name="language-choices" />
    </ui:VisualElement>
</ui:UXML>

When the user selects a language in either control, the control sets LocalizationSettings.SelectedLocale, and every localized string, asset, and binding updates. Locales discovered from data files appear in the controls when localization finishes initializing.

React to the change

The controls don’t need any setup code. To let other systems follow the user’s choice, subscribe to SelectedLocaleChanged:

public class LanguageSelectionMenu : MonoBehaviour
{
    // The language controls in the UXML need no setup code: they fill themselves from the
    // available locales and write the player's choice to LocalizationSettings.SelectedLocale.
    void OnEnable() => LocalizationSettings.SelectedLocaleChanged += OnLocaleChanged;

    void OnDisable() => LocalizationSettings.SelectedLocaleChanged -= OnLocaleChanged;

    void OnLocaleChanged(Locale locale)
    {
        Debug.Log($"Language changed to {locale.LocaleName}");
    }
}

Persist the choice

To reopen the application in the chosen language, add the Player Pref Locale Selector to the startup selectors list in the Localization settings, before the System Locale Selector. The Player Pref Locale Selector saves every change and restores the saved locale at startup. To store the choice somewhere else, for example a save file, refer to Create a custom startup locale selector.

Additional resources

Localization examples
Display dynamic values in a localized string