- Home /
The question is answered, right answer was accepted
How to switch language in-game using official unity localization 0.9?
I'm a newbie unity user who has never written a code before starting to learn unity couple weeks ago. Now i'm trying to learn how to implement a localization into game using official unity localization https://docs.unity3d.com/Packages/com.unity.localization@0.9/manual/index.html I've more ol less succesfully assigned different string value for different languages using localization tables. Now i can switch language upon playing the game in editor. But how to switch it in game outside editor? I've tried to create a dropdown with a following script attached: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Localization; using UnityEngine.Localization.Settings;
public class LanguageDropdown : MonoBehaviour
{
private Dropdown dropdown;
private string identifier;
void Start()
{
dropdown = GetComponent<Dropdown>();
}
void Update()
{
if (dropdown.value == 0)
{ identifier = "en"; }
else if (dropdown.value == 1)
{ identifier = "de"; }
else if (dropdown.value == 2)
{ identifier = "ru"; }
else if (dropdown.value == 3)
{ identifier = "uk"; }
SetLanguage(identifier);
}
void SetLanguage(string identifier)
{
Debug.Log("Selected language: " + identifier);
LocalizationSettings.SelectLocale(identifier);
}
}
The problem is that a method LocalizationSettings.SelectLocale(); which probably (i'm not sure) should switch current locale is inaccessible due to its protection level. It's defined as protected virtual inside Unity LocalizationSettings class so i cannot access it. So what should i do considering that my scripting skills are very low at the moment?
Did you look at the samples that come with the package? There are some examples of language switchers. You can see the samples in the package manager window
Thanks for guidance. I didn't notice them and tried to read manual and scripting API, didn't manage to find answer there and finally stumbled upon a comment to a youtube video which gave the right answer.
Answer by viv5552 · Jan 10, 2021 at 05:31 PM
Turns out it should be done with LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[i];
Hey dude. Can you walk me through how it's done? I cant figure it out and i cant understand your solution. I'm using Localization 1.0.5
This worked for me:
public void LoadLocale(string languageIdentifier)
{
LocalizationSettings settings = LocalizationSettings.Instance;
LocaleIdentifier localeCode = new LocaleIdentifier(languageIdentifier);//can be "en" "de" "ja" etc.
for(int i = 0; i < LocalizationSettings.AvailableLocales.Locales.Count; i++)
{
Locale aLocale = LocalizationSettings.AvailableLocales.Locales[i];
LocaleIdentifier anIdentifier = aLocale.Identifier;
if(anIdentifier == localeCode)
{
LocalizationSettings.SelectedLocale = aLocale;
}
}
}