Create a language selection menu
Add a language selection menu to a UI Toolkit interface.
Read time 2 minutesLast updated 7 days ago
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:
- Localization settings with two or more locales. Refer to Get started with localization.
- A runtime UI document. Refer to Get started with runtime UI.
Add the controls
In UI Builder, the controls appear in the library under Controls: Language Dropdown () and Language Radio Button Group (). Click and drag the control that fits your menu into the Hierarchy pane, or author it in UI Toolkit XML (UXML):
LanguageDropdownLanguageRadioButtonGroup<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 , and every localized string, asset, and binding updates. Locales discovered from data files appear in the controls when localization finishes initializing.
LocalizationSettings.SelectedLocaleReact to the change
The controls don't need any setup code. To let other systems follow the user's choice, subscribe to :
SelectedLocaleChangedpublic 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.