0.1.6 • Published 4 years ago

herb-i18n v0.1.6

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

herb-i18n

安装配置

安装

npm i herb-i18n -S

插件配置

herb.config.js

// herb.config.js
module.exports = {
  dev: {
    plugins: []
  },
  plugins: ["herb-i18n/lib/plugin"]
}

Structure

  • src 目录下约定存在 i18n 文件夹
  • i18n 文件夹内需要存在 index.js 文件, index.js 内导出一个对象,语法不限 cjsesm
src
├── config.js
├── app.js
├── app.less
├── store.js
├── herb.config.js
├── pages
│    └── page1
│    └── page2
├── i18n                    // 约束文件夹
│    └── index.js           // 约束导出的脚本文件
│    └── cn.js           // 中文
│    └── ......

Usage

介绍

herb-i18n 默认读取语言为 zh-Hans,即简体中文

API

API参数示例
t参数1为字符串,参数2为语言,默认为 locale 设置过的值,不设置则为 zh-Hanst('你好'); t('你好', 'en');
locale参数为语言,设置后会切换到对应的语言locale('en');

快速初始化

# 首页进入项目根目录,执行以下命令,则可以快速生成模版代码
npx herb-i18n init

js 内使用

// i18n/index.js
export default {
  'zh-Hans': {
    '你好': '你好',
  },
  'en': {
    '你好': 'hello',
  },
};
// page/index.js
import { t } from 'herb-i18n';

Page({
  data: {
    text: '',
  },
  onLoad() {
    this.setData({
      text: t('你好'),
    });
  },
  onButtonClick() {
    this.setData({
      text: t('你好', 'en'), // 将 "你好" 转换成 'hello'
    });
  },
});

axml 内使用(暂不支持)

HerbJS 提供给你最舒服的在 axml 内直接使用 i18n 的能力!

需要自行确保没有与 $t 重复的变量名

<view>
  <view>zh-Hans:{{ $t('你好') }}</view>
  <view>en:{{ $t('你好', 'en') }}</view>
</view>

常见问题

如何使用国际化

// app.js
import { t } from 'herb-i18n';

App({
  onLaunch() {
    t('hello');
  },
});
0.1.4

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago