Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support add locals and add case (#2289)
- Loading branch information
1 parent
159184c
commit fc373d9
Showing
20 changed files
with
314 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,5 @@ export const i18n: I18nOptions = { | |
cookieMaxAge: FORMAT.MS.ONE_YEAR, | ||
}, | ||
}, | ||
localsField: 'i18n', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/i18n/test/fixtures/base-app-express-req-locals/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "ali-demo" | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/i18n/test/fixtures/base-app-express-req-locals/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Configuration, Inject } from '@midwayjs/decorator'; | ||
import * as express from '@midwayjs/express'; | ||
|
||
@Configuration({ | ||
imports: [ | ||
express, | ||
require('../../../../src') | ||
], | ||
importConfigs: [ | ||
{ | ||
default: { | ||
keys: '12345', | ||
i18n: { | ||
defaultLocale: 'en_US', | ||
localeTable: { | ||
en_US: { | ||
"HELLO_MESSAGE": "Hello {username}", | ||
}, | ||
zh_CN: { | ||
"HELLO_MESSAGE": "浣犲ソ {username}", | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
] | ||
}) | ||
export class AutoConfiguration { | ||
@Inject() | ||
framework: express.Framework; | ||
|
||
async onReady() { | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/i18n/test/fixtures/base-app-express-req-locals/src/user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Controller, Get, Inject, Query } from '@midwayjs/decorator'; | ||
import { MidwayI18nService } from '../../../../src'; | ||
|
||
@Controller('/') | ||
export class UserController { | ||
|
||
@Inject() | ||
i18nService: MidwayI18nService; | ||
|
||
@Inject() | ||
res; | ||
|
||
@Get('/') | ||
async index(@Query('username') username: string) { | ||
const i18n = this.res.locals['i18n']; | ||
|
||
return i18n('HELLO_MESSAGE', { | ||
username | ||
}); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/i18n/test/fixtures/base-app-koa-ctx-locals/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "ali-demo" | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/i18n/test/fixtures/base-app-koa-ctx-locals/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Configuration, Inject } from '@midwayjs/decorator'; | ||
import * as koa from '@midwayjs/koa'; | ||
|
||
@Configuration({ | ||
imports: [ | ||
koa, | ||
require('../../../../src') | ||
], | ||
importConfigs: [ | ||
{ | ||
default: { | ||
keys: '12345', | ||
i18n: { | ||
defaultLocale: 'en_US', | ||
localeTable: { | ||
en_US: { | ||
"HELLO_MESSAGE": "Hello {username}", | ||
}, | ||
zh_CN: { | ||
"HELLO_MESSAGE": "浣犲ソ {username}", | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
] | ||
}) | ||
export class AutoConfiguration { | ||
@Inject() | ||
framework: koa.Framework; | ||
|
||
async onReady() { | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/i18n/test/fixtures/base-app-koa-ctx-locals/src/user.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Controller, Get, Inject, Query } from '@midwayjs/decorator'; | ||
|
||
@Controller('/') | ||
export class UserController { | ||
|
||
@Inject() | ||
ctx; | ||
|
||
@Get('/') | ||
async index(@Query('username') username: string) { | ||
const i18n = this.ctx.locals['i18n']; | ||
|
||
return i18n('HELLO_MESSAGE', { | ||
username | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/web-koa/test/fixtures/base-app-state-locals/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "ali-demo" | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/web-koa/test/fixtures/base-app-state-locals/src/config/config.default.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
export const keys = 'key'; | ||
|
||
export const hello = { | ||
a: 1, | ||
b: 2, | ||
d: [1, 2, 3], | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/web-koa/test/fixtures/base-app-state-locals/src/config/config.unittest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
exports.hello = { | ||
b: 4, | ||
c: 3, | ||
}; |
Oops, something went wrong.