Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Language randomly switches on scene change!

Discussion in 'Localization Tools' started by daniel_lochner, Feb 19, 2023.

  1. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    168
    Hi there!

    I’m encountering an issue where the language randomly changes upon switching to a different scene. It’s very unpredictable though, and doesn’t happen to everyone.

    In one case, the language changed when returning to the main menu (he mentions it at the end of the video):


    …and in another, it changed when teleporting to a different map:


    Fortunately, the bug actually happened live during the second video I linked above!

    The course of events in this video are as follows:
    1. 20:20 — Leaves on raft with the notification “You unlocked Farm!” still in English.
    2. 20:43 — Switches scene and the language is now in German.
    3. 21:40 — Opens leave confirmation dialog which is in German.
    4. 21:45 — Returns to the main menu and with the language is still in German.
    5. 22:06 — Confirmation dialog is also in German.
    6. 23:04 - Tries to change language, but it doesn’t work?
    7. 23:43 — Clicks on Quit button, but now the language is in English!? (So changing language did actually work, but event wasn’t fired…)
    8. 24:02 — Closes and re-opens the game and language is in finally back to English.

    Now if the language were really German at (7), then the “are you sure you want to quit” text would also have been in German, because I have localised that as well. The difference is that I am using the LocalizedStringEvent component for the scene UI elements, and GetLocalizedString for the generic confirmation dialog text. As noted before at (3), this text was in german.

    What’s strange is that at (6) the toggle is set to German… but setting the language programatically doesn’t change the actual toggle — isOn is only changed by clicking on the toggle (the toggle group then disables the previous as allowSwitchOff is false). I have also checked my code thoroughly, and can’t find any obvious issues. I don’t change the language anywhere else besides a single menu which is initialised in the Startup scene.

    Another thing I should mention is that It also doesn’t necessarily always change to German! Someone else reported it changed to French, and another to Russian!

    I’m not quite sure where else to check for this issue, but is there possibly a way to clear all “cache” and unused Playable addressables relating to localisation? I added and removed languages quite a bit in the beginning, so it’s possibly quite messy! Other than “turning it off and on” I’m not sure what else to try, so would really appreciate the help!

    Thank you for your time!

    Cheers,
    Daniel
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Hi,

    That's very strange, I have not seen this issue before.
    What version of the package are you using? 1.4.3 is the latest, if it's not visible in the package manager you can manually upgrade by editing the manifest.json file in the Packages folder.

    How do you set the initial language at the start? Are you using a locale selector or setting the SelectedLocale?
    Are you using preloading? Are your Addressables stored locally or remotely?
    If you are setting the language through SelectedLocale I would suggest creating a LocaleSelector instead or at least making sure the InititializationOperation has been completed before you switch. it is possible that it is loading one language and you then switch to another whilst it's still loading the previous one. Addressables doesn't have a way to cancel a partial loading operation, so I suspect it starts loading one language, you switch, and then have 2 InitializationOperations running which conflict and cause the language to switch.
    If you are able to reproduce this then a bug report would be great, at the moment it's just a theory and hard to solve without something to test against.
    Can you share your code that sets the selected locale?
     
  3. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    168
    Hi @karl_jones,

    Thank you so much for the response!

    I just figured out why changing the language at (6) didn't work! I accidentally used an anonymous method when subscribing to SelectedLocaleChanged in EditorManager, and so hadn't unsubscribed. So, when he left the gameplay scene and returned to the main menu, the "UpdateStatistics" method was being invoked trying to access destroyed objects... (causing a
    NullReferenceException
    ).

    This still doesn't explain why it changed in the first place though, but at least one issue is resolved!

    I've actually made the project open source, so you can view it here.

    Initial setup is done in the LocalizationMenu, which is a persistent menu from the Startup scene. As seen on line 33, I do ensure the InitializationOperation has completed! When setting the locale, I use the SelectedLocale property, as seen in SettingsManager.SetLocale. I'm not preloading and addressables are stored locally (I'm using all the default suggested localization settings).

    I've tried inserting debug statements into SetLocale, and can confirm it's only ever called once while switching between languages from the toggles in the LocalizationMenu. I'm worried all this has something to do with the fact that I'm using a toggle group to change between languages, because there's no other way German would have been enabled without having toggled it manually? (Unless of course it was enabled before but hadn't actually set the language?)

    Thanks again for your help, I really appreciate it!
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    The only way I can see German being enabled would be by the locale selector during initialization. This may be different for users depending on what system language they happen to be running in, e.g if running on a German machine but using English. This may explain why it's a different language for some users.
    You may be able to reproduce this by adding selected locale selector to the locale selectors and setting it to German, then move it to the top of the list so that the game always tries to use German first.
    The safest and most performative way to initialize the language is by using a locale selector. These get run during initialization. You should be able to move your code in Setup into a locale selector.
    https://docs.unity3d.com/Packages/c...lization.Settings.IStartupLocaleSelector.html
     
  5. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    168
    Alright thanks! Will try it out!
     
    karl_jones likes this.
  6. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    168
    Unfortunately the issue still seems to be happening for players! It occurs randomly during the game so not in initialisation.

    Has anyone else experienced something similar? Are there any open source projects using localization which I can check?

    I’m pretty confident that there aren’t any silly issues on my part regarding the random so I think it might be a bug? Of course not absolutely sure though…
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    There are some, such as this https://github.com/UnityTechnologies/open-project-1
    Im not sure it will help though. Are you able to share your project? I can take a look and see if you are doing anything strange. DM me and I can provide a secure link to upload it.