Home > Net >  Change language in WebGL
Change language in WebGL

Time:12-08

I have a webgl project with language localization. In unity, it changes the language depending on what language format is in the window. But in the browser, he puts the language that is in the browser. How to change the language in the browser from the language format in windows

[SerializeField] private Dropdown dropdown;

    public void SetLanguage(int value)
    {
        Lean.Localization.LeanLocalization.CurrentLanguage = value == 0 ? "Russian" : "English";
    }

    private string DefineCurrentLanguage()
    {
        if (Application.systemLanguage == SystemLanguage.Russian)
        {
            Debug.Log("This system is in Russian. ");
            return "Russian";
        } 
        else if (Application.systemLanguage == SystemLanguage.English)
        {
            Debug.Log("This system is in English. ");
            return "English";
        }
        else
        {
            Debug.Log("System Language not defined. ");
            return "English";
        }        

    }

    private void SetLanguage(string lang)
    {
        Lean.Localization.LeanLocalization.CurrentLanguage = lang;
    }
    private void Awake()
    {
        SetLanguage(DefineCurrentLanguage());

        dropdown.value = Lean.Localization.LeanLocalization.CurrentLanguage == "Russian" ? 0 : 1;
    }
}

CodePudding user response:

I am pretty sure they will internally use navigator.language which is

a string representing the preferred language of the user, usually the language of the browser UI.

See also this post

There is no cross-browser way to do this. Internet Explorer supports the following:

navigator.browserLanguage: browser language
navigator.systemLanguage: Windows system language
navigator.userLanguage: Windows user-specific language

But there is no way to access these settings from any other browsers (that I can tell) so don't use them: stick to the standard navigator.language (corresponding to the browser language) if you want to maintain cross-browser functionality.

There is also navigator.languages which lists all available preferences which might include system language etc

In the returned array they are ordered by preference with the most preferred language first.

However, navigator.language already returns that first most preferred item which - again - is usually the browser language

=> There isn't really a way to tell which of these exactly would be the system language either.


You could of course follow this post and at least try to get it where it is supported

var language = window.navigator.systemLanguage || window.navigator.language;

You could search specifically for each browser you are going to support and research if there is a specific way for getting the system language and implement an || case for each of them - always falling back to navigator.language as worst case.


Then in order to actually use it you would have your custom jslib plugin like e.g.

WebGLLanguage.jslib

mergeInto(LibraryManager.library, 
{    
    GetLanguageJS : function()
    {
        const language = navigator.systemLanguage || navigator.language;

        const languageString = language.toLowerCase();
        const bufferSize = lengthBytesUTF8(languageString)   1;
        const buffer = _malloc(bufferSize);
        stringToUTF8(languageString, buffer, bufferSize);
        
        return buffer;
    }
}

WebGLLanguage.cs

public static class WebGLLanguage
{
    [DllImport("__Internal")]
    private static string GetLanguageJS();

    public static SystemLanguage GetLanguage()
    {
        var language = GetLanguageJS();

        // well and now you need to map the string to the enum
        // I am too lazy but I hope you get the idea ;)

        if(language.StartsWith("en")) return SystemLanguage.English;
        if(language.StartsWith("fr")) return SystemLanguage.French;
        if(language.StartsWith("es")) return SystemLanguage.Spanish;
        ......
    }
}
  • Related