Skip to content

Latest commit

 

History

History
122 lines (84 loc) · 4.63 KB

README-zh_CN.md

File metadata and controls

122 lines (84 loc) · 4.63 KB

MultiLangText

Build Status

English | 中文

LayaAir 引擎多语言 Text(Multi-language Text) Library.

截图

MultiLangText_screen_record_1.0.0.gif

使用

Step 1

  • 复制 /src/co/lujun/laya/component/MultiLangText.js{LayaAir IDE 安装目录}/Resources/app/out/vs/layaEditor/renders/custom/ 目录
  • 复制 /src/co/lujun/laya/component/MultiLangText.xml{LayaAir IDE 安装目录}/Resources/app/out/vs/layaEditor/renders/custom/ 目录
  • 复制 /src/co/lujun/laya/component/multiLangText.png{LayaAir IDE 安装目录}/Resources/app/out/vs/layaEditor/laya/basics/Custom/ 目录
  • 复制 /src/co/lujun/laya/component/multiLangText.png{LayaAir IDE 安装目录}/Resources/app/out/vs/layaEditor/laya/icons/components/ 目录
  • 导入 /src/co/ 至你的项目中

Step 2

初始化,异步和同步两种方式:

同步初始化,先预加载 langfile.csv, 加载成功再调用 init 方法.

Laya.loader.load("res/langFile.csv", Laya.Handler.create(this, function(){
   LangManager.init(true, "res/langFile.csv");
   Laya.stage.addChild(new view.SampleView);
}));

异步初始化, 可以在使用之前任何地方调用 init 方法, 调用时第一个参数 preloadLangFile 需要传 false, 第二个参数 langFilePathlangfile.csv 路径.

Laya.loader.load(["res/atlas/comp.atlas"], Laya.Handler.create(this, function(){
    Laya.stage.addChild(new view.SampleView);
}));
LangManager.init(false, "res/langFile.csv");

Step 3

在 LayaAir IDE UI Editor 中使用 MultiLangText:

在 LayaAir IDE UI Editor 中找到 组件 并打开 Custom 目录, 就可以像使用 Text 一样使用 MultiLangText.

MultiLangText_screen_record_1.0.1.jpg MultiLangText_screen_record_1.0.2.jpg MultiLangText_screen_record_1.0.3.jpg

在代码中使用 MultiLangText, 同样类似在代码中使用 Text 一样使用 MultiLangText:

  • 单一 MultiLangText 使用
let multiLangText: MultiLangText = new MultiLangText();
multiLangText.pos(0, 0);
multiLangText.fontSize = 50;
multiLangText.color = "#ffffff";
multiLangText.multiLang("^BTN_SET", false);
Laya.stage.addChild(multiLangText);
  • MultiLangText 作为 Laya.Text 的子节点使用(多语言翻译为 Laya.Text 的 text)
let layaText: Laya.Text = new Laya.Text();
let layaTextMultiText: MultiLangText = new MultiLangText();
layaText.pos(300, 0);
layaText.fontSize = 50;
layaText.color = "#ffffff";
layaText.addChild(layaTextMultiText);
layaTextMultiText.multiLang("^BTN_SOUND", true);
Laya.stage.addChild(layaText);
  • LangManager.getInstance().getValue(langKey: string) 方法, 使用这个方法根据一个 key 获取翻译 value
let layaText1: Laya.Text = new Laya.Text();
layaText1.pos(600, 0);
layaText1.fontSize = 50;
layaText1.color = "#ffffff";
layaText1.text = LangManager.getInstance().getValue("^BTN_MUSIC");
Laya.stage.addChild(layaText1);
  • switchLang(code: LangCode) 方法, 使用此方法切换语言, 参数 LangCode
LangManager.getInstance().switchLang(LangCode.SC);

LangCode

翻译的枚举, 位于 co.lujun.laya.component.

enum value in .csv file column index(first column index is 0) description
EN 1 1 English
SC 2 2 简体中文
TC 3 3 繁體中文
JP 4 4 日本語

注意: 添加新语言翻译时,先在 LangCode 中添加一个 code,然后将该 code 添加到 co.lujun.laya.component.LangManager.mLangCodeArr 数组中. langFile.csv 文件同样需要用制表符(Tab 键)顺序相同分别写入.

Enjoy it😄!

更新

0.9.0(2018-12-4)

  • First release.

关于

If you have any questions, contact me: lujun.byte#gmail.com.

License

MIT