How to get the current locale? (LocalizationSettings.SelectedLocale does not work)

I am trying to display the current locale to make testing the language settings easier.

I found in other threads that I should use LocalizationSettings.SelectedLocale to get the selected locale, but I always get the error message: “error CS0103: The name ‘LocalizationSettings’ does not exist in the current context”

What am I doing wrong? I have using UnityEngine.Localization; in the script and the script works fine otherwise. It only starts acting up when I want to get the current locale. Has the command changed?

Is the script inside of an assembly definition file? You may need to add a reference to Unity.Localization to your asmdef.

No, its inside of a normal scene script. From your answer I assume thats not where it should go?

Asmdefs can be part of normal scene scripts. Unity - Manual: Assembly definition and packages
If one is being used it would explain the error.

What does the full code look like?

This is the whole script, it deals with many UI related things. The line with the locale is under SetupUIElements(). The error in full lenght is:

Assets\0_GAME_ASSETS\Scripts\UIManager.cs(53,26): error CS0103: The name ‘LocalizationSettings’ does not exist in the current context

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using TMPro;
using System;
using UnityEngine.Localization;

public class UIManager : MonoBehaviour
{

    public float upScalePercentage = 1.1f;
    public float downScalePercentage = 0.9f;
    public float normalButtonScale;

    public TMP_Text versionNumberText;

    public GameObject settingsMenu;

    public Slider musicSlider, sfxSlider;


    //values for error-shaking
    public Vector3 punchDirection;
    public float punchTime;
    public int vibrato;
    public float elasticity;

    private Texture2D currentCursor;

    public static UIManager Instance;


    void Awake()
    {
        Instance = this;
        Debug.Log("there should be an instance of ui manager");
    }
    
    
    // Start is called before the first frame update
    void Start()
    {
        SetupUIElements();
    }

    void SetupUIElements()
    {
        musicSlider.value = AudioManager.Instance.musicSource.volume;
        sfxSlider.value = AudioManager.Instance.sfxSource.volume;

        Locale _locale = LocalizationSettings.SelectedLocale;

        //Debug.Log("volume sliders are set up");
    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            Debug.Log("screenshot key was pressed");
            
            ScreenCapture.CaptureScreenshot(DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + "-sotw" + ".png", 1);
        }        
    }

    public void SetMusicVolume()
    {
        if(AudioManager.Instance != null)
        {
            AudioManager.Instance.SetMusicVolume(musicSlider.value);
        }
    }

    public void SetSFXVolume()
    {
        if(AudioManager.Instance != null)
        {
            AudioManager.Instance.SetSFXVolume(sfxSlider.value);
        }
    }

    public void PlaySFX(string _sfx)
    {
        if(AudioManager.Instance != null)
        {
            AudioManager.Instance.PlaySFX(_sfx);
        }
    }

    public void SetUICursor()
    {
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
    } 

}

I can’t see anything wrong. Are you able to share the project or one which has the same error?

I made a completely new 2D project in 2022.3.33f, installed the localization package from the package manager, and made this test script, which gives the same error.

I put it on our Hidrive Cloud where you can download it (or recreate it in a new project as well): HiDrive

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization;

public class localeTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Locale _locale = LocalizationSettings.SelectedLocale;
        Debug.Log(_locale);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

Thanks ill try and take a look later.
One thing to check, did you add the localization package to the project? Installation guide | Localization | 1.5.3

I have it installed and already use it sucessfully to translate UI texts. Are there maybe two and I have the wrong one? This is how my package manager looks:

I found the issue. LocalizationSettings is in the UnityEngine.Localization.Settings namespace.
So you need to add

using UnityEngine.Localization.Settings;
1 Like

Thank you! It works :slight_smile:

1 Like