不同语言的字符串提示 [英] string hints in different languages

查看:88
本文介绍了不同语言的字符串提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Unity3d中创建一个游戏.

I am creating a game in Unity3d.

在HUD的底部出现了一些提示(类似于按A键进行操作").我希望我的游戏支持更多的语言,并且我不想在脚本中对这些提示进行硬编码.

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.

解决此任务的最优雅的方法是什么?我正在考虑txt文件,在这里我会以所有语言显示所有提示.但是我不确定这是否是一个好主意.

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.

谢谢.

推荐答案

如果正确执行,则硬编码还不错.

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";
  }
}

用法很简单:

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.

这篇关于不同语言的字符串提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆