Author Topic: Localise a string with a variable  (Read 4498 times)

Meltdown

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 56
    • View Profile
Localise a string with a variable
« on: March 08, 2014, 11:23:58 PM »
I'm trying to localise a string with a variable using String.Format  (as mentioned by Aaron in another post)...

This is my localisation string

HELLO_STRING=Hello {0}, welcome to the club

This is the code I'm using to set the variable

  1. _speechLabel.GetComponent<UILocalize>().key = "HELLO_STRING";
  2. _speechLabel.GetComponent<UILocalize>().Localize();
  3. _speechLabel.text = String.Format(_speechLabel.text, "Bob");

But no matter what I try, the {0} still always appear in my game and never gets replace with 'Bob'.

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Localise a string with a variable
« Reply #1 on: March 09, 2014, 09:33:58 AM »
  1. _speechLabel.text = string.Format(Localization.Get("HELLO_STRING"), "Bob");

Meltdown

  • Jr. Member
  • **
  • Thank You
  • -Given: 10
  • -Receive: 0
  • Posts: 56
    • View Profile
Re: Localise a string with a variable
« Reply #2 on: April 04, 2014, 05:53:34 PM »
Thanks for that. Although it seems there is a bug.

If I have...

  1. private UILocalize _speechLabelLocalise;
  2.  
  3. void Start()
  4. {
  5. if (_speechLabel != null)
  6.             _speechLabelLocalise = _speechLabel.GetComponent<UILocalize>();
  7. }
  8.  
  9. void SetText(string key)
  10. {
  11. _speechLabelLocalise.key = key;
  12. _speechLabel.text = string.Format(Localization.instance.Get("HELLO_STRING"), "Bob");
  13. }

It never applies the Bob value to the string.

I need to remove this line if I want it to work.
  1. _speechLabelLocalise.key = key;
  2.  


How can I set this variable and set the key of the label too?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Localise a string with a variable
« Reply #3 on: April 04, 2014, 10:43:45 PM »
Don't even bother attaching UILocalize.

You are doing localization manually via code. UILocalize is for automatic localization. The line I posted is all you need.