-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(locale): 添加国际化 & 单测 * refactor: 暂时不暴露 GLOBAL 变量,修改 cr 建议 * fix(locale): 找不到翻译时,返回 key 值 & 修复单测
- Loading branch information
Showing
19 changed files
with
283 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { GLOBAL, setGlobal } from '../../../src/core/global'; | ||
|
||
describe('global variables', () => { | ||
it('global locale', () => { | ||
// 单测环境,默认 中文 | ||
expect(GLOBAL.locale).toBe('zh-CN'); | ||
}); | ||
|
||
it('setGlobal', () => { | ||
setGlobal({ locale: 'en-US' }); | ||
expect(GLOBAL.locale).toBe('en-US'); | ||
}); | ||
}); |
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,42 @@ | ||
// 得引一下 src,否则无法注册国际化 | ||
import '../../../src'; | ||
import { GLOBAL, setGlobal } from '../../../src/core/global'; | ||
import { getLocale, registerLocale } from '../../../src/core/locale'; | ||
import { EN_US_LOCALE } from '../../../src/locales/en_US'; | ||
import { ZH_CN_LOCALE } from '../../../src/locales/zh_CN'; | ||
|
||
describe('locale', () => { | ||
it('getLocale', () => { | ||
// 单测环境,默认 中文 | ||
expect(GLOBAL.locale).toBe('zh-CN'); | ||
expect(getLocale('xxx').get('general')).toEqual(ZH_CN_LOCALE.general); | ||
|
||
expect(getLocale('en-US').get('general')).toEqual(EN_US_LOCALE.general); | ||
|
||
setGlobal({ locale: 'en-US' }); | ||
expect(getLocale('xxx').get('general')).toEqual(EN_US_LOCALE.general); | ||
}); | ||
|
||
it('registerLocale', () => { | ||
registerLocale('custom', { | ||
locale: 'custom', | ||
general: { increase: 'INCREASE', decrease: 'DECREASE', root: 'ROOT' }, | ||
statistic: { total: '统计' }, | ||
conversionTag: { label: '转化率' }, | ||
waterfall: { total: '累计值' }, | ||
}); | ||
expect(getLocale('custom').get(['statistic', 'total'])).toBe('统计'); | ||
expect(getLocale('custom').get('statistic.total')).toBe('统计'); | ||
// 返回 key 值 | ||
expect(getLocale('custom').get('statistic-total')).toBe('statistic-total'); | ||
// 找不到语言包,则使用全局语言包 | ||
expect(getLocale('---').get('locale')).toBe('en-US'); | ||
expect(getLocale('---').get('locale')).toBe('en-US'); | ||
|
||
setGlobal({ locale: 'custom' }); | ||
expect(getLocale('---').get('locale')).toBe('custom'); | ||
expect(getLocale('---').get(['statistic', 'total'])).toBe('统计'); | ||
expect(getLocale('---').get('statistic.total')).toBe('统计'); | ||
expect(getLocale('---').get('statistic-total')).toBe('statistic-total'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { setGlobal } = require('./src/core/global'); | ||
|
||
setGlobal({ locale: 'zh-CN' }); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { each } from '@antv/util'; | ||
/** | ||
* @file 全局的一些变量定义:含国际化、主题... | ||
*/ | ||
export const GLOBAL = { | ||
/** 全局语言 */ | ||
locale: 'en-US', | ||
}; | ||
|
||
/** | ||
* 全局变量设置 | ||
* @param key | ||
* @param value | ||
*/ | ||
export function setGlobal(datum: Record<string, any>): void { | ||
each(datum, (v, k) => (GLOBAL[k] = v)); | ||
} |
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,31 @@ | ||
import { get } from '@antv/util'; | ||
import { Locale } from '../types/locale'; | ||
import { template } from '../utils'; | ||
import { GLOBAL } from './global'; | ||
|
||
const LocaleMap = {}; | ||
|
||
/** | ||
* register a locale | ||
* @param locale | ||
* @param localeObj | ||
*/ | ||
export function registerLocale(locale: string, localeObj: Locale) { | ||
LocaleMap[locale] = localeObj; | ||
} | ||
|
||
/** | ||
* get locale of specific language | ||
* @param lang | ||
* @returns | ||
*/ | ||
export function getLocale(locale: string) { | ||
return { | ||
get: (key: string | string[], obj?: Record<string, any>) => { | ||
return template( | ||
get(LocaleMap[locale], key) || get(LocaleMap[GLOBAL.locale], key) || get(LocaleMap['en-US'], key) || key, | ||
obj | ||
); | ||
}, | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Locale } from '../types/locale'; | ||
|
||
export const EN_US_LOCALE: Locale = { | ||
locale: 'en-US', | ||
|
||
// General | ||
general: { | ||
increase: 'Increase', | ||
decrease: 'Decrease', | ||
root: 'Root', | ||
}, | ||
|
||
// Plot Components | ||
/** statistic text component */ | ||
statistic: { | ||
total: 'Total', | ||
}, | ||
/** conversionTag component */ | ||
conversionTag: { | ||
label: 'Conversion rate', | ||
}, | ||
legend: {}, | ||
tooltip: {}, | ||
slider: {}, | ||
scrollbar: {}, | ||
|
||
// Plots | ||
waterfall: { | ||
total: 'Total', | ||
}, | ||
}; |
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,31 @@ | ||
import { Locale } from '../types/locale'; | ||
|
||
export const ZH_CN_LOCALE: Locale = { | ||
locale: 'zh-CN', | ||
|
||
// 通用 | ||
general: { | ||
increase: '增加', | ||
decrease: '减少', | ||
root: '初始', | ||
}, | ||
|
||
// 按照图表组件 | ||
/** 中心文本 */ | ||
statistic: { | ||
total: '总计', | ||
}, | ||
/** 转化率组件 */ | ||
conversionTag: { | ||
label: '转化率', | ||
}, | ||
legend: {}, | ||
tooltip: {}, | ||
slider: {}, | ||
scrollbar: {}, | ||
|
||
// 按照图表类型 | ||
waterfall: { | ||
total: '总计', | ||
}, | ||
}; |
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
Oops, something went wrong.