2
头图

I haven't posted much recently. It turns out that the product and development have been busy iterating, and EOAPI has added some new features: such as pre- and post-scripting , multi-language , and remote data sources . Then there will definitely be some friends who ask, do the plugins in your plugin square also support multiple languages?

Must support! After reading this article, you will know how to configure Eoapi to support multiple languages. The final effect is as follows:

image.png
Chinese version plugin

image.png
English version plugin

sample code

https://github.com/eolinker/eoapi-extensions/tree/main/packages/feature/i18n

You can directly debug the plugin through the link of the sample code to see the display effect more intuitively

image.png

configure

package.json

Configure the i18n field in package.json

 // package.json
{
  "name": "eoapi-i18n",
  "version": "1.0.0",
  "description": "The i18n example extension of eoapi",
  "author": "scar",
  "main": "dist/index.js",
  "moduleID": "eoapi-i18n",
  "moduleName": "eoapi-i18n",
  "moduleType": "feature",
  "logo": "http://dummyimage.com/125x125",
  "scripts": {
    "build": "rollup -c rollup.config.js",
    "build:watch": "rollup -w -c rollup.config.js"
  },
  "devDependencies": {
    "rollup": "^2.70.2"
  },
  "features": {
    "apimanage.export": {
      "action": "exportFunc",
      "label": "${function.label}", //在 feature 中通过 ${i18n name} 使用
      "description": "${function.description}",
      "icon": "http://dummyimage.com/125x125",
      "filename": "data.json"
    },
    //i18n 配置
    "i18n": {
      "sourceLocale": "en-US", //默认语言,使用 eoapi 规定的 localeID
      "locales": [
        "zh-Hans" //支持语言
      ]
    }
  }
}

Configure language packs

Create a new folder named i18n in the root directory of the plugin, and the language package name is LocaleID

A B
1 Simplified Chinese en-Hans
2 traditional Chinese en-Hant
3 English - United States en-US
4 French fr-FR
5 Japanese ja-JP
6 Russia ru-RU
 //en-US.json
{
  "moduleName": "eoapi-i18n",
  "description": "The i18n example extension of eoapi", //如果语言包 json 里面找不到,会使用 package.json 的文件
  "logo": "http://dummyimage.com/125x125",
  "function.label": "en Data(.json)",
  "function.description": "export api data",
  "now-lanuage": "Now lanuage is: {0}"
}
 //zh-Hans.json
{
  "moduleName": "eoapi-国际化",
  "description": "国际化插件示例",
  "author": "秦一",
  "logo": "http://dummyimage.com/200x100",
  "function.description": "导出 API 数据",
  "function.label": "zh Data(.json)",
  "now-lanuage": "当前语言是:{0}",
  "multiple-variable": "替换多个变量: {0} {1}"
}

use in Javascript

Via the eo.i18n function

 eo.i18n.localize:(originText:string,replacement:string,...variable)=>string;

export const exportFunc = (data = {}) => {
  console.log(eo.i18n.localize("export-id", "I am origin text"));
  //localize(id:string,originText:string,...args)
  console.log(
    eo.i18n.localize(
      "now-lanuage",
      "Now lanuage is: {0}",
      eo.i18n.getSystemLanguage()
    )
  );

  //id can be ignore in default localize i18n json
  console.log(
    eo.i18n.localize(
      "multiple-variable",
      "Replace multiple variable: {0} {1}",
      0,
      "1"
    )
  );
  return {
    name: "eoapi",
  };
};

If you are interested in how the plug-in system implements plug-in multilingualism, you can take a look at the corresponding implementation PR: https://github.com/eolinker/eoapi/pull/90

Finally, let me introduce myself.

Eoapi is an open source API tool similar to Postman, which is lighter and extensible.

Project address :
https://github.com/eolinker/eoapi

Official website address :
https://www.eoapi.io/?utm_source=SF-0701

If you have any questions or suggestions about Eoapi , you can go to Github or come here to find me, raise an Issue, and I will reply in time when I see it.


Postcat
320 声望14 粉丝

我叫 Postcat,我是一款开源 API 工具,我更轻量,同时可拓展!我可以简化你的 API 开发工作,让你更快更好地创建 API。