Coder Social home page Coder Social logo

tk-i18n's Introduction

i18n json to mjs

灵感来源 https://inlang.com/m/gerre34r/library-inlang-paraglideJs

将多语言的 json 文件转为 mjs,支持Tree-shaking减少代码体积

支持 ts 有完整的类型提示

npx tk-i18n

i18n.config.jsonc

{
  // 支持的语言
  "language": ["zh", "en"],
  // 默认 or 兜底 语言
  "defaultLanguage": "zh",
  // 多语言 文件夹
  "inputDir": "messages",
  // 输出文件夹
  "outputDir": "./src/i18n"
}

支持的格式 jsonc json5 json

支持的语法

  1. 基础类型、变量
{
  "name": "Thin Ke",
  "hello": "hello {user} !",
  "num": 123,
  "bool0": false,
  "bool1": true,
  "nil": null
}
  1. 数组、对象
{
  "arr": [1, 2, 3],
  "arr_str": ["T", "h", "i", "n"],
  "obj": {
    "txt": "hello",
    "var": "hello {user} .",
    "num": 123,
    "arr_str": ["T", "h", "i", "n"]
  }
}
  1. 自引用
{
  "name": "Thin Ke",
  "hello": "hello {@name} !" // => hello Thin Ke !
}
  1. 变量处理
// 支持链式、函数式; 支持任意全局函数或对应类型的方法
{
  // 1 链式调用
  "hello": "hello {name|..toUpperCase()} !",
  // 2 函数方式
  "fn": "max age is {age|>Math.max(99,#)}",
  // 支持混合使用
  "test1": "max age is {age|>Math.max(99,#)|..toFixed(2)}",
  // 自引用 也支持处理
  "name": "Thin Ke",
  "name_test": "hello {@name|..toUpperCase()} !"
}

注意点

  1. key 最终转为函数名,所有 要遵循 命名规则
  2. 多语言文件名 需要遵循 命名规则(同 1 所示)

tk-i18n's People

Contributors

thinke5 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.