Ray开发指南——多语言介绍

使用

面板小程序的国际化多语言是开发过程中必不可少的一部分,因此在 ray 中内置了 I18N 模块,该模块依赖了智能小程序内置 I18n 对象。

定义多语言

请勿在本地代码内定义除中英文外的多语言。其他语种涉及多地区,本地代码无法全兼容,请在 涂鸦 IoT 平台 的 产品多语言 页面设定 设备面板 的多语言配置。

在 src/i18n/strings.{js,ts} 文件中进行编辑。

// en 和 zh 必须都定义
export default {
  en: {
    hello: 'Hello',
  },
  zh: {
    hello: '您好',
  },
};

多语言规范

bitmap 类型使用详情参见 bitmap 型如何使用

export default {
  en: {
    dsc_edit: 'Edit', // Basic multi language with dsc_ start and name it semantically
    dsc_hour: 'Hour',
    dsc_minute: 'Minute',
    dsc_countdown_on: 'Turn on after {0}',
    dp_switch: 'Switch', // Datapoint (DP) related multi language should be in accordance with the `dp_${dpCode}`
    dp_switch_on: 'Switch is On', // Boolean type datapoint related multi language should be in accordance with the `dp_${dpCode}_${'on' || 'off'}`
    dp_mode_smart: 'Smart Mode', // Enum type datapoint related multi language should be in accordance with the `dp_${dpCode}_${enumValue}`
    dp_fault_0: 'Binary first bit is faulty', // Bitmap type datapoint related multi language should be in accordance with the `dp_${dpCode}_${bit}`
    dp_fault_1: 'Binary second bit is faulty',
  },
  zh: {
    dsc_edit: '编辑', //  基础多语言以 dsc_ 开头并命名语义化即可
    dsc_hour: '时',
    dsc_minute: '分',
    dsc_countdown_on: '设备将在{0}后开启',
    dp_switch: '开关', // 功能点(DP)相关多语言需按照 `dp_${dpCode}` 进行命名
    dp_switch_on: '开关开', // 布尔类型功能点状态相关多语言需按照 `dp_${dpCode}_${'on' || 'off'}` 进行命名
    dp_mode_smart: '智能模式', // 枚举类型功能点状态相关多语言需按照 `dp_${dpCode}_${enumValue}` 进行命名
    dp_fault_0: '第一位故障', // Bitmap 类型功能点状态相关多语言需按照 `dp_${dpCode}_${bit}` 进行命名
    dp_fault_1: '第二位故障',
  },
};

导出多语言

在 src/i18n/index.{js,ts} 文件中进行实例化导出。

// @ray-js/ray^1.2.5 导入方式(推荐)
import { I18N } from '@ray-js/ray';
 
// @ray-js/ray 低于 1.2.5,需要使用 @ray-js/panel-sdk
import { kit } from '@ray-js/panel-sdk'
const { I18N } = kit;
 
import Strings from './strings';
 
export default new I18N(Strings);
 

使用多语言

可在任意需要使用多语言的场景进行调用。

import Strings from '../i18n';
 
const localizedText = Strings.getLang('hello');
 
console.log(localizedText);

上传多语言

仅支持大小小于 1M 的 JSON 文件,此外注意在 涂鸦 IoT 平台 上传的多语言优先级比本地代码定义的优先级高。

  1. 找到上传多语言区块。
  2. 点击上传多语言。
  3. 上传如以下 JSON 格式的中英文多语言文件即可,请注意保证 JSON 文件格式的正确性。
{
  "en": {
    "hello": "Hello"
  },
  "zh": {
    "hello": "您好"
  }
}

👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。 

  • 20
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT砖家涂拉拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值