Author Topic: Localization: PlayerPrefs gets wrong language  (Read 4714 times)

amaranth

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Localization: PlayerPrefs gets wrong language
« on: August 02, 2013, 12:16:10 PM »
For some reason, my game always defaults to French, even though the starting language is English. It seems like the problem starts when I use this line:

currentLanguage = PlayerPrefs.GetString("Language", startingLanguage);

When I use this line, startingLanguage is "English" but the currentLanguage returned is "French". I also noticed that after I use this code, startingLanguage changes to "French". Any one know what's going on?

public class Localization : MonoBehaviour
  1.         void Awake ()
  2.         {
  3.                 if (mInstance == null)
  4.                 {
  5.                         ...
  6.                        
  7.                         print ("start language: " + startingLanguage);
  8.                        
  9.                         currentLanguage = PlayerPrefs.GetString("Language", startingLanguage);
  10.                        
  11.                         print ("start language: " + startingLanguage);
  12.                         print ("current language: " + currentLanguage);
  13.                        
  14.                         ...
  15.                 }
  16.         }
  17.  

Print Results:
start language: English
start language: French
current language: French

amaranth

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 21
    • View Profile
Re: Localization: PlayerPrefs gets wrong language
« Reply #1 on: August 02, 2013, 12:37:39 PM »
Well, that was odd, I fixed the problem, but I think this might be a PlayerPrefs bug in Unity.

(1) I changed this line of code and ran the game. English showed up.
Old:  currentLanguage = PlayerPrefs.GetString("Language", startingLanguage);
New: currentLanguage = startingLanguage;

(2) I stopped the game and changed the code back to the original set up and ran the game again. English showed up instead of French. Problem solved.

csykora

  • Guest
Re: Localization: PlayerPrefs gets wrong language
« Reply #2 on: August 21, 2013, 03:55:45 PM »
http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.GetString.html

GetString only uses the default value if it cannot get the value from the playerPrefs i.e. the key doesn't exist in PlayerPrefs. PlayerPrefs must have "Language" set as French. I'd recommend getting a better understanding of how PlayerPrefs work.

nilton_felicio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Localization: PlayerPrefs gets wrong language
« Reply #3 on: September 26, 2013, 07:24:51 AM »
I'm having the same problem. I tried the solution of amaranth, but for me it did not work. Ngui support could give a solution to this?
« Last Edit: September 26, 2013, 07:41:31 AM by nilton_felicio »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Localization: PlayerPrefs gets wrong language
« Reply #4 on: September 26, 2013, 03:46:03 PM »
Look at the Localization example. See the drop-down where it saves the language? Emphasis on saves. As in, your choice is saved in PlayerPrefs, so the next time you attempt to retrieve this value, it will retrieve the saved value.

You can resolve this by either choosing English, or just by clearing player prefs.

nilton_felicio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Localization: PlayerPrefs gets wrong language
« Reply #5 on: September 27, 2013, 09:05:28 AM »
I'm sorry. But it is still not working. He's not wiping player prefs in a legal way. Localization used in a final project has not even had a problem, I was super happy with the result. But now I'm having this problem, it persists in one language. Starting in Language he me the right language I queronaquele moment, but the game screen it prints a language that somehow this in memory. If you can arrange another solution thanks.  :-\

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Localization: PlayerPrefs gets wrong language
« Reply #6 on: September 27, 2013, 01:35:22 PM »
If you don't want it saved at all, just call Localization.currentLanguage = null;

nilton_felicio

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Localization: PlayerPrefs gets wrong language
« Reply #7 on: September 27, 2013, 02:03:15 PM »
Worked. Thank you.
Changed here:

before:

void Awake ()
    {
        if (mInstance == null)
        {
            ...
           
            print ("start language: " + startingLanguage);
           
            currentLanguage = PlayerPrefs.GetString("Language", startingLanguage);
           
            print ("start language: " + startingLanguage);
            print ("current language: " + currentLanguage);
           
            ...
        }
    }

Later:

void Awake ()
    {
        if (mInstance == null)
        {
            ...
           
            print ("start language: " + startingLanguage);
           
            currentLanguage = PlayerPrefs.GetString(null, startingLanguage);
           
            print ("start language: " + startingLanguage);
            print ("current language: " + currentLanguage);
           
            ...
        }
    }


 ;D ;D