Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support cjs and esm both by tshy #14

Merged
merged 5 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions .autod.conf.js

This file was deleted.

2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/fixtures
coverage
13 changes: 4 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
{
"extends": "eslint-config-egg",
"rules": {
"no-console": "off",
"no-magic-numbers": "off",
"generator-star-spacing": "off",
"prefer-template": "off",
"max-len": "off",
"dot-notation": "off"
}
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
16 changes: 16 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
version: '18.19.0, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly --if-present

- run: npx pkg-pr-new publish
13 changes: 13 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Release

on:
push:
branches: [ master ]

jobs:
release:
name: Node.js
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
55 changes: 11 additions & 44 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,44 +1,11 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

!.gitignore
!.eslintrc
!.eslintignore
!.travis.yml

run
logs/
npm-debug.log
node_modules/
coverage/
test/fixtures/**/run
.DS_Store
.tshy*
.eslintcache
dist
package-lock.json
.package-lock.json
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

File renamed without changes.
132 changes: 74 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
# egg-i18n
# @eggjs/i18n

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Node.js CI](https://github.com/eggjs/i18n/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/i18n/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-i18n.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-i18n
[travis-image]: https://img.shields.io/travis/eggjs/egg-i18n.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-i18n
[codecov-image]: https://codecov.io/github/eggjs/egg-i18n/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/eggjs/egg-i18n?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-i18n.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-i18n
[snyk-image]: https://snyk.io/test/npm/egg-i18n/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-i18n
[download-image]: https://img.shields.io/npm/dm/egg-i18n.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-i18n
[![Node.js Version](https://img.shields.io/node/v/@eggjs/i18n.svg?style=flat)](https://nodejs.org/en/download/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)

[npm-image]: https://img.shields.io/npm/v/@eggjs/i18n.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/i18n
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/i18n.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/i18n?branch=master
[snyk-image]: https://snyk.io/test/npm/@eggjs/i18n/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/@eggjs/i18n
[download-image]: https://img.shields.io/npm/dm/@eggjs/i18n.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/i18n

fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
可以为你的应用提供多语言的特性

Expand All @@ -30,50 +27,54 @@

## 配置

默认处于关闭状态,你需要在 `config/plugin.js` 开启它:
默认处于关闭状态,你需要在 `config/plugin.ts` 开启它:

```js
// config/plugin.js
exports.i18n = {
enable: true,
package: 'egg-i18n',
```ts
// config/plugin.ts
export default {
i18n: {
enable: true,
package: 'egg-i18n',
},
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
};
```

你可以修改 `config/config.default.js` 来设定 i18n 的配置项:

```js
// config/config.default.js
exports.i18n = {
// 默认语言,默认 "en_US"
defaultLocale: 'zh-CN',
// URL 参数,默认 "locale"
queryField: 'locale',
// Cookie 记录的 key, 默认:"locale"
cookieField: 'locale',
// Cookie 的 domain 配置,默认为空,代表当前域名有效
cookieDomain: '',
// Cookie 默认 `1y` 一年后过期, 如果设置为 Number,则单位为 ms
cookieMaxAge: '1y',
你可以修改 `config/config.default.ts` 来设定 i18n 的配置项:

```ts
// config/config.default.ts
export default {
i18n: {
// 默认语言,默认 "en_US"
defaultLocale: 'zh-CN',
// URL 参数,默认 "locale"
queryField: 'locale',
// Cookie 记录的 key, 默认:"locale"
cookieField: 'locale',
// Cookie 的 domain 配置,默认为空,代表当前域名有效
cookieDomain: '',
// Cookie 默认 `1y` 一年后过期, 如果设置为 Number,则单位为 ms
cookieMaxAge: '1y',
},
};
```

其实大部分时候,你只需要修改一下 `defaultLocale` 设定默认的语言。

## 编写你的 I18n 多语言文件

```js
// config/locale/zh-CN.js
module.exports = {
```ts
// config/locale/zh-CN.ts
export default {
"Email": "邮箱",
"Welcome back, %s!": "欢迎回来,%s!",
"Hello %s, how are you today?": "你好 %s, 今天过得咋样?",
};
```

```js
// config/locale/en-US.js
module.exports = {
```ts
// config/locale/en-US.ts
export default {
"Email": "Email",
};
```
Expand All @@ -95,25 +96,26 @@ I18n 为你提供 `__` (Alias: `gettext`) 函数,让你可以轻松获得 loca

> NOTE: __ 是两个下划线哦!

- ctx.__ = function (key, value[, value2, ...]): 类似 util.format 接口
- ctx.__ = function (key, values): 支持数组下标占位符方式,如
- `ctx.__ = function (key, value[, value2, ...])`: 类似 util.format 接口
- `ctx.__ = function (key, values)`: 支持数组下标占位符方式,如

```ts
ctx.__('{0} {0} {1} {1}'), ['foo', 'bar']);
ctx.gettext('{0} {0} {1} {1}'), ['foo', 'bar']);

```js
ctx.__('{0} {0} {1} {1}'), ['foo', 'bar'])
ctx.gettext('{0} {0} {1} {1}'), ['foo', 'bar'])
=>
foo foo bar bar
```

### Controllers 下的使用示例

```js
module.exports = function* () {
this.body = {
message: this.__('Welcome back, %s!', this.user.name)
```ts
export default ctx => {
ctx.body = {
message: ctx.__('Welcome back, %s!', ctx.user.name)
// 或者使用 gettext,如果觉得 __ 不好看的话
// message: this.gettext('Welcome back, %s!', this.user.name)
user: this.user,
// message: this.gettext('Welcome back, %s!', ctx.user.name)
user: ctx.user,
};
};
```
Expand All @@ -132,10 +134,24 @@ module.exports = function* () {

### 修改应用的默认语言

你可以用下面几种方式修改应用的当前语言(修改或会记录到 Cookie),下次请求直接用设定好的语言。
你可以用下面几种方式修改应用的当前语言(修改或会记录到 Cookie,下次请求直接用设定好的语言。

优先级从上到下:

- query: /?locale=en-US
- cookie: locale=zh-TW
- header: Accept-Language: zh-CN,zh;q=0.5
- query: `/?locale=en-US`
- cookie: `locale=zh-TW`
- header: `Accept-Language: zh-CN,zh;q=0.5`

## Questions & Suggestions

Please open an issue [here](https://github.com/eggjs/egg/issues).

## License

[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=eggjs/i18n)](https://github.com/eggjs/i18n/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).
Loading
Loading