記錄一下, 如何在Typescript下使用i18n

昨天为了找怎么在Typescript底下使用i18n花了点时间, 在这边记录一下, 希望能帮到以后遇到同样问题的人.

  1. 手动下载i18n插件包, 解开到项目的packages, 目录名从i18n-master改成i18n.(我不爱全局安装)
  2. 直接在编辑器里使是是正常的, 没有其它问题.
  3. 在脚本中使用, 先把tsconfig.ts 的compilerOptions 加上 “allowJs”: true , 不然import会报错.
  4. 在代码中加入 import * as i18n from “…/…/packages/i18n/runtime-scripts/LanguageData”;
  5. 使用就跟说明文档用法一样, 例如 i18n.init(‘zh’); cc.log(i18n.t(“Menu”));

ps.旧版本的Typescript, tsconfig.ts 的 compilerOptions 加上 “allowJs”: true会没有作用,
有被报bug, 不过我目前用的版本是Cocos Creator 1.5.2-beta2是没问题的.

3赞

Cocos Creator 2.0这样还是会import报错

2.0.9 用不来