Skip to content

Commit

Permalink
fix: Incorrect group format is used in i18n component (#3931)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Aug 29, 2024
1 parent 427e308 commit e8abefe
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/i18n/src/i18nService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class MidwayI18nServiceSingleton {
* @param localeTextMapping
*/
public addLocale(locale: string, localeTextMapping: Record<string, any>) {
const currentLangMap = getMap(this.localeTextMap, locale);
const currentLangMap = getMap(this.localeTextMap, locale, true);

for (const key in localeTextMapping) {
if (typeof localeTextMapping[key] === 'string') {
Expand Down Expand Up @@ -288,8 +288,8 @@ function getES6Object(o) {
return o;
}

function getMap(o: Map<string, any>, key: string) {
key = formatLocale(key);
function getMap(o: Map<string, any>, key: string, formatKey = false) {
key = formatKey ? formatLocale(key) : key;
if (!o.has(key)) {
o.set(key, new Map());
}
Expand Down
41 changes: 41 additions & 0 deletions packages/i18n/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MidwayI18nService } from '../src';
import { createLightApp, close, createHttpRequest, createApp } from '@midwayjs/mock';
import { join } from 'path';
import { formatWithArray, formatWithObject } from '../src/utils';
import * as i18n from '../src';

describe('test/index.test.ts', () => {
it('i18n service', async () => {
Expand Down Expand Up @@ -426,4 +427,44 @@ describe('test/index.test.ts', () => {
await close(app);
});
});

it('should test group name', async () => {
const app = await createLightApp({
imports: [i18n],
globalConfig: {
i18n: {
defaultLocale: 'en_US',
localeTable: {
'zh_CN': {
'box_cate': {
'hello': '你好,美丽的世界'
}
},
'en-us': {
'box_cate': {
'hello': 'hello world'
}
}
},
fallbacks: {
'zh_*': 'zh_CN'
}
}
}
});

const i18nService = await app.getApplicationContext().getAsync(MidwayI18nService);

expect(i18nService.getDefaultLocale()).toEqual('en-us');

expect(i18nService.translate('hello', {
group: 'box_cate'
})).toEqual('hello world');
expect(i18nService.translate('hello', {
locale: 'zh_CN',
group: 'box_cate'
})).toEqual('你好,美丽的世界');

await close(app);
});
});
4 changes: 2 additions & 2 deletions site/docs/extensions/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ async index(@Query('username') username: string) {

## 通过参数指定当前语言

一般情况下,我们的默认语言为 `en_US`如果每次调用需要用户手动指定 `locale` 参数显然不合理。在 HTTP 场景下,我们提供了通过参数指定当前语言的能力
一般情况下,默认语言为 `en_US`用户的浏览器访问一般会自带 `Accept-Language` 头,所以会正确识别语言。比如用中文浏览器访问,就能正常显示中文

默认情况下,可以通过 URL Query,Cookie,Header 来指定
除此之外,在 HTTP 场景下可以通过 URL Query,Cookie,Header 来指定语言

优先级从上到下:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ async index(@Query('username') username: string) {

## Specify the current language through parameters

In general, the default language is `en_US`. If you need to manually specify the `locale` parameter for each call, it is not reasonable. In the HTTP scenario, we provide the ability to specify the current language through parameters.
In general, the default language is `en_US`, and the user's browser will usually have an `Accept-Language` header, so the language will be correctly identified. For example, if you use a Chinese browser to access, Chinese can be displayed normally.

In addition, in HTTP scenarios, you can specify the language through URL Query, Cookie, and Header.

By default, you can specify URL Query,Cookie, and Header.

Expand Down

0 comments on commit e8abefe

Please sign in to comment.