Toggle between specific language and last chosen?

More
4 years 2 months ago #1020 by jimmyt3d
Hi,

I have a game with 8 languages, I have a language select screen at the start which determines the language for the rest of the game.
What I would like to do however, is have a toggle button in place throughout the game, where the user can switch to English at any time and then switch back to whatever language they were on before.

eg. - Boot game, Choose Italian on the language select screen, go into a level and click a button that switches to English, Click button again and switch back to Italian.

I don't want all 8 languages available throughout the game, just English and the one selected at the start.

Is there a way of doing this? Some way of choosing 'last selected'?

Many thanks

Please Log in or Create an account to join the conversation.

More
4 years 2 months ago - 4 years 2 months ago #1021 by Frank
Hi,
The plugin allows remembering the selected language and even detect the device language on startup, but doesn't allow push/pop language settings.

However, you could do that by saving the value in a static variable or even in the PlayerPrefs if you like that previous settings to be remembered even if the game gets restarted.
public class CustomLanguageManager
{
     public static string PreviousLanguage;

    public static void SetLanguage( string newLanguage )
    {
          PreviousLanguage = LocalizationManager.currentLanguage;
          LocalizationManager.currentLanguage = newLanguage;
    }

    public static void RestorePreviousLanguage()
    {
          if (!string.IsNullOrEmpty(PreviousLanguage))
          {
               LocalizationManager.currentLanguage = PreviousLanguage;
               PreviousLanguage = string.Empty;
          }
    }    
}

Then, in your button, whenever you need to set the language, instead of modifying LocalizationManager.currentLanguage, you should call:
CustomLanguageManager.SetLanguage( "English" );

and whenever you want to restore, just do:
CustomLanguageManager.RestorePreviousLanguage();

That should do the trick.


Another possibility is by using the PlayerPrefs. that will allow you to remember the previous language even if the game is closed and restarted.
public class CustomLanguageManager
{
    public static void SetLanguage( string newLanguage )
    {
          PlayerPrefs.SetString("PreviousLanguage", LocalizationManager.currentLanguage);
          LocalizationManager.currentLanguage = newLanguage;
    }

    public static void RestorePreviousLanguage()
    {
          var PreviousLanguage= PlayerPrefs.GetString("PreviousLanguage", string.Empty);
          if (!string.IsNullOrEmpty(PreviousLanguage))
          {
               LocalizationManager.currentLanguage = PreviousLanguage;
               PlayerPrefs.SetString("PreviousLanguage",  string.Empty);
          }
    }    
}

Hope that helps
Frank

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out
Last edit: 4 years 2 months ago by Frank.

Please Log in or Create an account to join the conversation.

More
4 years 1 month ago #1061 by jimmyt3d
Hi Frank,

Thanks for your help. I am going with the first option, so I have attached the script to a GameObject, but when I run my scene, I get the error -

The name `LocalizationManager' does not exist in the current context

Any ideas why this might be?

Many thanks

Please Log in or Create an account to join the conversation.

More
4 years 1 month ago #1062 by Frank

The name `LocalizationManager' does not exist in the current context


Yes, to avoid clashing with other plugins you may have installed, I placed all classes inside the I2.Loc namespace.

So, you could either add the using directive at the top of the script
using I2.Loc;

Or replace the LocalizationManager by I2.Loc.LocalizationManager

Hope that helps!
Frank

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out

Please Log in or Create an account to join the conversation.

More
4 years 1 month ago - 4 years 1 month ago #1063 by jimmyt3d
Thanks for the tip, doing this and changing 'currentLanguage' to 'CurrentLanguage' (otherwise it showed an error), the script now no longer has errors.
So with regards to what to do next -

I have 2 buttons, one to change to English and one to switch back.

The script above is set to a game object, so I go to the button and select this gameobject in the 'On Click()' field, now I select the script, however if I choose to 'SetLanguage' or 'RestorePreviousLanguage', I get the message '<Missing CustomLanguageManager.restorePreviousLanguage>'.

See attached screenshots.

Thanks!



Attachments:
Last edit: 4 years 1 month ago by jimmyt3d. Reason: Realised previous comment about no SetLanguage option being available was incorrect.

Please Log in or Create an account to join the conversation.

More
4 years 1 month ago - 4 years 1 month ago #1065 by Frank
Hi,
If you are going to call the SetLanguage and RestoreLanguage methods from a button OnClick and not from your code, then you should remove the "static" from both methods.
That way, the OnClick will be able to see the functions.

Hope that helps,
Frank

New code:
using UnityEngine;
using System.Collections;
using I2.Loc;

public class CustomLanguageManager : MonoBehaviour
{
     public static string PreviousLanguage;

    public void SetLanguage( string newLanguage )
    {
          PreviousLanguage = LocalizationManager.CurrentLanguage;
          LocalizationManager.CurrentLanguage = newLanguage;
    }

    public void RestorePreviousLanguage()
    {
          if (!string.IsNullOrEmpty(PreviousLanguage))
          {
               LocalizationManager.CurrentLanguage = PreviousLanguage;
               PreviousLanguage = string.Empty;
          }
    }    
}

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out
Last edit: 4 years 1 month ago by Frank.

Please Log in or Create an account to join the conversation.

Time to create page: 0.166 seconds
Template by JoomlaShine