npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ibird-i18n

v1.0.0-alpha.15

Published

i18n addon for ibird.

Downloads

8

Readme

ibird-i18n

国际化插件。

安装

npm install ibird-i18n

引用

const app = require('ibird').newApp();
const i18n = require('ibird-i18n');

app.import(i18n, { defaultLocale: 'zh_CN' });

配置示例

国际化配置本质上是JSON结构,其中key为占位符,value为不同语言环境下的显示值;多个语言环境即是存在多份相同key的配置文件,例如en_US.json

{
  "locale": "English",
  "app": "ibird",
  "author": "Daniel Yin",
  "license": "Apache-2.0",
  "home": "http://ibird.yinfxs.com",
  "repository": "https://github.com/yinfxs/ibird.git"
}

其中locale是个特别的key,它表示语言环境名称,一般即为对应语言的本地化翻译。

插件信息

  • 命名空间 - ibird-i18n
  • 引用参数
    • locales - 可选,对象类型,国际化配置对象,key为语言环境编码,value为对应的国际化配置
    • defaultLocale - 可选,默认语言环境编码
    • dir - 可选,字符串类型,国际化资源文件所在目录,指定后,该目录下所有文件都会被自动注册为国际化配置,且以文件名作为语言编码
  • API
    • enabledLocale - 对象类型,表示当前默认启用的语言环境,共包含name(语言编码)和locale(国际化配置)两个信息
    • addLocale(name, locale) - 新增国际化配置函数,需传递两个参数:name(语言编码)和locale(国际化配置)
    • addLocaleDir(dir) - 新增国际化配置目录
    • switchLocale(name) - 切换默认语言环境,需指定语言编码作为参数
    • removeLocale(name) - 删除指定的语言环境,需指定语言编码作为参数
    • clearLocale() - 清空所有语言配置
    • getLocale(name) - 根据语言编码,获取对应的国际化配置
    • getLocaleList() - 获取已注册的所有国际化配置列表
    • getLocaleString(key, params, localeOrName) - 常用API,获取指定占位符在指定语言环境下的国际化显示值,支持模板渲染
  • 路由
    • 根据语言编码查询对应的国际化配置接口:
      • name - i18n_name
      • method - GET
      • path - /i18n/:name
    • 切换默认国际化配置接口:
      • name - i18n_switch
      • method - GET
      • path - /i18n/:name/switch
    • 国际化列表查询接口:
      • name - i18n_list
      • method - GET
      • path - /i18n

模板渲染

在实际应用中,我们的国际化显示信息有时候不是固定值,例如以下国际化配置:

en_US.json

{
  ...
  "login_error": "Username {{login_user}} login failed.",
  ...
}

zh_CN.json

{
  ...
  "login_error": "抱歉,账号 {{login_user}} 登录失败,请稍后重试~",
  ...
}

这里的login_user即为动态值,是需要在使用该国际化占位符时才传递过来的信息,例如:

app.getLocaleString('login_error', { login_user: 'admin' }, 'en_US')
// return: Username admin login failed.

app.getLocaleString('login_error', { login_user: 'admin' }, 'zh_CN')
// return: 抱歉,账号 admin 登录失败,请稍后重试~

模板渲染底层引擎为mustache.js