Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string hints in different languages

I am creating a game in Unity3d.

Somewhere I have hints appearing in the bottom of the HUD (Something like "press A for Action"). I want my game to support more languages and I don't want these hints to be hard-coded in the script.

What is the most elegant way to solve this task? I am thinking about txt file, where I will have all my hints in all languages. But I am not sure, if it is a good idea.

Thanks in advance.

like image 470
Mad Avatar asked Sep 22 '26 00:09

Mad


2 Answers

A common way that is used is to have an XML-file for each language. The XML-file should contain every phrase you use in your game with a unique ID.

In your code, you can then get the correct phrase out of the current language's XML, using the correct ID.

If you don't know about XML files, read this tutorial about using XML files in Unity: http://xeophin.net/en/blog/2010/05/13/reading-strings-out-xml-file-using-c-unity-3d

However if you are willing to spend some money on your game, you should check this out: http://u3d.as/content/rodrigo-barros/my-menu/32E

I haven't tried that yet but it seems good.

like image 129
Jay Kazama Avatar answered Sep 28 '26 10:09

Jay Kazama


Hardcoding is not that bad if you do it correctly.

For example create an abstract class with all the messages that you want to have and language autodetection:

public abstract class Lang {

  static Lang currentLang;

  public static Lang Get {
    get {
      if(currentLang == null)
        switch(Application.systemLanguage) {
        case SystemLanguage.Polish:
          currentLang=new LangPL();
          break;
        default:
        case SystemLanguage.English:
          currentLang=new LangEN();
          break;
        }

      return currentLang;
    }
  }

  public abstract string MenuTime {get;}
  public abstract string MenuPoints {get;}
  public abstract string YouWon {get;}
  public abstract string YouLost {get;}
  public abstract string Point(int p);

}

And then implement each language as a separate class:

public class LangPL:Lang {
  public override string MenuTime {get {return "CZAS";}}
  public override string MenuPoints {get {return "WYNIK";}}
  public override string YouWon {get {return "Gratuluję, wygrałeś!";}}
  public override string YouLost{get {return "Może następnym razem...";}}
  public override string Point(int p) {
    if(p == 1)
      return "1 punkt";
    return p+" punktów";
  }
}


public class LangEN:Lang {
  public override string MenuTime {get {return "TIME";}}
  public override string MenuPoints {get {return "SCORE";}}
  public override string YouWon {get {return "You won!";}}
  public override string YouLost{get {return "Maybe next time...";}}
  public override string Point(int p) {
    if(p == 1)
      return "1 point";
    return p+" points";
  }
}

Usage is a simple:

GUILayout.Label(Lang.Get.YouWon);
GUILayout.Label(Lang.Get.Point(5));

This way you have most control, and can easily support complex translations. Also this approach helps to trim down on errors - if you forget or misstype a text anywhere it throws a syntax error.

like image 30
Krzysztof Bociurko Avatar answered Sep 28 '26 10:09

Krzysztof Bociurko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!