Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Locale detection in WebGL

Discussion in 'Localization Tools' started by BoopShadoop, Feb 20, 2023.

  1. BoopShadoop

    BoopShadoop

    Joined:
    Apr 6, 2021
    Posts:
    9
    Hello,

    I am trying to autodetect the language to use for WebGL builds. I use the System Locale Selector and it works perfectly in mobile, but in web - the detected language is always English.

    I found this issue: https://issuetracker.unity3d.com/is...e-always-returns-en-us-when-building-to-webgl
    Which may be related to this, it seems it was solved for unity 2021+, but our unity version is 2020.3.42

    So I have a few questions:
    1. Is this issue the cause of the problem? If it is - will it be backported to 2020 too?
    2. For WebGL, does the language autodetect query the system language or the browser language?

    Thank you in advance!
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,227
    Hi,
    It looks like this is the cause of your issues, we don't have any special handling for WebGL and just use the current culture to try to identify the system language. From what I can see, that issue was tested by QA and they found it was fixed in 2021+ however they were unable to identify what fixed it so performing a backport would not be possible.

    I believe that on WebGL, Application.systemLanguage works correctly. By default we prioritise CurrentCulture before we test Application.systemLanguage however you could create a custom locale selector that uses Application.systemLanguage first.
    For example:
    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Localization;
    4. using UnityEngine.Localization.Settings;
    5.  
    6. [DisplayName("WebGL Startup Selector")]
    7. [Serializable]
    8. public class WebGLStartupSelectorExample : IStartupLocaleSelector
    9. {
    10.     public Locale GetStartupLocale(ILocalesProvider availableLocales)
    11.     {
    12.         #if UNITY_WEBGL
    13.         // Return the Locale that matches the language field or null if one does not exist.
    14.         return availableLocales.GetLocale(Application.systemLanguage);
    15.         #else
    16.         return null;
    17.         #endif
    18.     }
    19. }
    Add this to the localization settings locale selectors, and move it to the top of the list so it's used first.
    This is more limited in the locales that it supports, it won't work if you are using locales that are not part of SystemLanguage.
     
    BoopShadoop likes this.
  3. BoopShadoop

    BoopShadoop

    Joined:
    Apr 6, 2021
    Posts:
    9
    Thank you very much! Will try using it :)
     
    karl_jones likes this.