update
This commit is contained in:
parent
ffbf5ad9ad
commit
d1d34a146c
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
|
|
||||||
import {test,vi,describe,expect,afterAll,beforeAll} from 'vitest'
|
import {test,vi,describe,expect,afterAll,beforeAll, beforeEach} from 'vitest'
|
||||||
import { VoerkaI18nScope } from '../scope'
|
import { VoerkaI18nScope } from '../scope'
|
||||||
import zhFormatters from '../formatters/zh';
|
import zhFormatters from '../formatters/zh';
|
||||||
import enFormatters from '../formatters/en';
|
import enFormatters from '../formatters/en';
|
||||||
@ -10,6 +10,7 @@ import { VoerkaI18nLanguageMessages, VoerkaI18nFormatterConfigs } from '../types
|
|||||||
import { deepMerge } from 'flex-tools/object/deepMerge';
|
import { deepMerge } from 'flex-tools/object/deepMerge';
|
||||||
import { isPlainObject } from 'flex-tools/typecheck/isPlainObject';
|
import { isPlainObject } from 'flex-tools/typecheck/isPlainObject';
|
||||||
import { default as inlineFormatters } from '../formatters';
|
import { default as inlineFormatters } from '../formatters';
|
||||||
|
import { InvalidLanguageError } from '../errors';
|
||||||
|
|
||||||
function mergeFormattersConfigs(configSources:any[]){
|
function mergeFormattersConfigs(configSources:any[]){
|
||||||
return configSources.reduce((finalConfig, curConfig)=>{
|
return configSources.reduce((finalConfig, curConfig)=>{
|
||||||
@ -79,23 +80,26 @@ const formatters ={
|
|||||||
},
|
},
|
||||||
jp:()=>{}
|
jp:()=>{}
|
||||||
}
|
}
|
||||||
|
let scope:VoerkaI18nScope;
|
||||||
|
beforeAll(async ()=>{
|
||||||
|
return new Promise((resolve)=>{
|
||||||
|
scope = new VoerkaI18nScope({
|
||||||
|
id: "test",
|
||||||
|
languages,
|
||||||
|
idMap,
|
||||||
|
messages,
|
||||||
|
formatters,
|
||||||
|
callback:()=>{
|
||||||
|
resolve()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
beforeEach(async ()=>{
|
||||||
|
await scope.change("zh")
|
||||||
|
})
|
||||||
|
|
||||||
describe("VoerkaI18nScope", () => {
|
describe("VoerkaI18nScope", () => {
|
||||||
let scope:VoerkaI18nScope;
|
|
||||||
beforeAll(async ()=>{
|
|
||||||
return new Promise((resolve)=>{
|
|
||||||
scope = new VoerkaI18nScope({
|
|
||||||
id: "test",
|
|
||||||
languages,
|
|
||||||
idMap,
|
|
||||||
messages,
|
|
||||||
formatters,
|
|
||||||
callback:()=>{
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
test("成功创建实例", () => {
|
test("成功创建实例", () => {
|
||||||
expect(scope).toBeInstanceOf(VoerkaI18nScope)
|
expect(scope).toBeInstanceOf(VoerkaI18nScope)
|
||||||
expect(scope.activeLanguage).toBe("zh")
|
expect(scope.activeLanguage).toBe("zh")
|
||||||
@ -107,28 +111,53 @@ describe("VoerkaI18nScope", () => {
|
|||||||
// 全局管理器
|
// 全局管理器
|
||||||
expect(scope.global).toBeInstanceOf(VoerkaI18nManager)
|
expect(scope.global).toBeInstanceOf(VoerkaI18nManager)
|
||||||
})
|
})
|
||||||
|
test("切换语言", () => {
|
||||||
|
return new Promise<void>((resolve)=>{
|
||||||
|
scope.on((language:string) => {
|
||||||
|
expect(language).toBe("en")
|
||||||
|
expect(scope.activeLanguage).toBe("en")
|
||||||
|
expect(scope.defaultLanguage).toBe("zh")
|
||||||
|
expect(scope.messages).toEqual(messages)
|
||||||
|
expect(scope.default).toEqual(zhMessages)
|
||||||
|
expect(scope.current).toEqual(enMessages)
|
||||||
|
expect(scope.idMap).toEqual(idMap)
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
scope.change("en")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
test("格式化器配置", async () => {
|
test("切换到不存在的语言", async () => {
|
||||||
|
try{
|
||||||
|
await scope.change("xn")
|
||||||
|
}catch(e){
|
||||||
|
expect(e).toBeInstanceOf(InvalidLanguageError)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
describe("格式化化配置与参数", () => {
|
||||||
|
|
||||||
|
test("格式化器参数", async () => {
|
||||||
expect(scope.formatters).toBeInstanceOf(VoerkaI18nFormatterRegistry)
|
expect(scope.formatters).toBeInstanceOf(VoerkaI18nFormatterRegistry)
|
||||||
expect(scope.formatters.activeLanguage).toBe("zh")
|
expect(scope.formatters.activeLanguage).toBe("zh")
|
||||||
expect(scope.formatters.formatters).toEqual(formatters)
|
expect(scope.formatters.formatters).toEqual(formatters)
|
||||||
expect(scope.formatters.config).toBe(zhFormatters.$config)
|
|
||||||
expect(scope.formatters.types).toBe(zhFormatters.$types)
|
expect(scope.formatters.types).toBe(zhFormatters.$types)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("查找格式化器", async () => {
|
test("查找格式化器", async () => {
|
||||||
expect(scope.formatters.get("add")).toBe(formatters['*'].add)
|
expect(scope.formatters.get("add")).toBe(formatters['*'].add)
|
||||||
expect(scope.formatters.get("first")).toBe(formatters.zh.first)
|
expect(scope.formatters.get("first")).toBe(formatters.zh.first)
|
||||||
await scope.change("en")
|
await scope.change("en")
|
||||||
expect(scope.formatters.get("first")).toBe(formatters.en.first)
|
expect(scope.formatters.get("first")).toBe(formatters.en.first)
|
||||||
})
|
})
|
||||||
test("格式化器配置", async () => {
|
test("合并后的格式化器配置", async () => {
|
||||||
let fallbackLanguage = scope.getLanguage(scope.activeLanguage)?.fallback
|
let fallbackLanguage = scope.getLanguage(scope.activeLanguage)?.fallback
|
||||||
const globalFormatters = inlineFormatters
|
const globalFormatters = inlineFormatters
|
||||||
let scopeConfig = mergeFormattersConfigs([
|
let scopeConfig = mergeFormattersConfigs([
|
||||||
globalFormatters['*'].$config,
|
|
||||||
(fallbackLanguage! in globalFormatters) ? (globalFormatters as any)?.[fallbackLanguage!].$config:{},
|
(fallbackLanguage! in globalFormatters) ? (globalFormatters as any)?.[fallbackLanguage!].$config:{},
|
||||||
globalFormatters.zh.$config,
|
globalFormatters.zh.$config,
|
||||||
formatters['*'].$config,
|
|
||||||
(fallbackLanguage! in formatters) ? (formatters as any)?.[fallbackLanguage!]?.$config:{},
|
(fallbackLanguage! in formatters) ? (formatters as any)?.[fallbackLanguage!]?.$config:{},
|
||||||
formatters.zh.$config
|
formatters.zh.$config
|
||||||
])
|
])
|
||||||
@ -137,17 +166,17 @@ describe("VoerkaI18nScope", () => {
|
|||||||
await scope.change("en")
|
await scope.change("en")
|
||||||
fallbackLanguage = scope.getLanguage(scope.activeLanguage)?.fallback
|
fallbackLanguage = scope.getLanguage(scope.activeLanguage)?.fallback
|
||||||
scopeConfig = mergeFormattersConfigs([
|
scopeConfig = mergeFormattersConfigs([
|
||||||
globalFormatters['*'].$config,
|
|
||||||
(fallbackLanguage! in globalFormatters) ? (globalFormatters as any)?.[fallbackLanguage!].$config:{},
|
(fallbackLanguage! in globalFormatters) ? (globalFormatters as any)?.[fallbackLanguage!].$config:{},
|
||||||
// globalFormatters.zh.$config,
|
globalFormatters.en.$config,
|
||||||
formatters['*'].$config,
|
|
||||||
(fallbackLanguage! in formatters) ? (formatters as any)?.[fallbackLanguage!]?.$config:{},
|
(fallbackLanguage! in formatters) ? (formatters as any)?.[fallbackLanguage!]?.$config:{},
|
||||||
formatters.en.$config
|
formatters.en.$config
|
||||||
])
|
])
|
||||||
expect(scope.formatters.config).toEqual(scopeConfig)
|
expect(scope.formatters.config).toEqual(scopeConfig)
|
||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('translate', () => {})
|
describe('翻译函数', () => {
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
@ -69,8 +69,8 @@ export class VoerkaI18nFormatterRegistry{
|
|||||||
*/
|
*/
|
||||||
async change(language:string){
|
async change(language:string){
|
||||||
try {
|
try {
|
||||||
if (language in this. formatters) {
|
if (language in this.formatters) {
|
||||||
this.#language = language
|
|
||||||
const formatters = this.formatters[language]
|
const formatters = this.formatters[language]
|
||||||
if(isFunction(formatters)){
|
if(isFunction(formatters)){
|
||||||
this.#activeFormatters = await (formatters as Function)() // 如果格式化器集合是异步加载,则需要等待加载完成
|
this.#activeFormatters = await (formatters as Function)() // 如果格式化器集合是异步加载,则需要等待加载完成
|
||||||
@ -78,9 +78,9 @@ export class VoerkaI18nFormatterRegistry{
|
|||||||
this.#activeFormatters = formatters as VoerkaI18nFormatters
|
this.#activeFormatters = formatters as VoerkaI18nFormatters
|
||||||
}
|
}
|
||||||
// 合并生成格式化器的配置参数,当执行格式化器时该参数将被传递给格式化器
|
// 合并生成格式化器的配置参数,当执行格式化器时该参数将被传递给格式化器
|
||||||
this.generateFormattersConfigs(language)
|
|
||||||
// 清空缓存
|
|
||||||
this.#formatterCache = {typedFormatters:{},formatters:{}}
|
this.#formatterCache = {typedFormatters:{},formatters:{}}
|
||||||
|
this.generateFormattersConfigs(language)
|
||||||
|
this.#language = language
|
||||||
} else {
|
} else {
|
||||||
if (this.scope?.debug) console.warn(`Not configured <${language}> formatters.`);
|
if (this.scope?.debug) console.warn(`Not configured <${language}> formatters.`);
|
||||||
}
|
}
|
||||||
@ -93,12 +93,10 @@ export class VoerkaI18nFormatterRegistry{
|
|||||||
const configSources = [ ]
|
const configSources = [ ]
|
||||||
const fallbackLanguage = this.scope?.getLanguage(language)?.fallback ;
|
const fallbackLanguage = this.scope?.getLanguage(language)?.fallback ;
|
||||||
if(this.scope){ // 从全局Scope读取
|
if(this.scope){ // 从全局Scope读取
|
||||||
configSources.push(this.scope.global.formatters.getConfig('*'))
|
|
||||||
if(fallbackLanguage) configSources.push(this.scope.global.formatters.getConfig(fallbackLanguage))
|
if(fallbackLanguage) configSources.push(this.scope.global.formatters.getConfig(fallbackLanguage))
|
||||||
configSources.push(this.scope.global.formatters.getConfig(language))
|
configSources.push(this.scope.global.formatters.getConfig(language))
|
||||||
}
|
}
|
||||||
// 从当前Scope读取
|
// 从当前Scope读取
|
||||||
configSources.push(this.getConfig('*'))
|
|
||||||
if(fallbackLanguage) configSources.push(this.getConfig(fallbackLanguage))
|
if(fallbackLanguage) configSources.push(this.getConfig(fallbackLanguage))
|
||||||
configSources.push(this.getConfig(language))
|
configSources.push(this.getConfig(language))
|
||||||
// 合并当前语言的格式化器配置参数
|
// 合并当前语言的格式化器配置参数
|
||||||
|
@ -5,6 +5,7 @@ import inlineFormatters from "./formatters"
|
|||||||
import type { VoerkaI18nScope } from "./scope"
|
import type { VoerkaI18nScope } from "./scope"
|
||||||
import type { VoerkaI18nLanguageDefine, VoerkaI18nLanguageFormatters, VoerkaI18nDefaultMessageLoader, VoerkaI18nFormatter, VoerkaI18nTypesFormatters } from "./types"
|
import type { VoerkaI18nLanguageDefine, VoerkaI18nLanguageFormatters, VoerkaI18nDefaultMessageLoader, VoerkaI18nFormatter, VoerkaI18nTypesFormatters } from "./types"
|
||||||
import { VoerkaI18nFormatterRegistry } from "./formatterRegistry"
|
import { VoerkaI18nFormatterRegistry } from "./formatterRegistry"
|
||||||
|
import { InvalidLanguageError } from './errors';
|
||||||
|
|
||||||
// 默认语言配置
|
// 默认语言配置
|
||||||
const defaultLanguageSettings = {
|
const defaultLanguageSettings = {
|
||||||
@ -92,7 +93,7 @@ export class VoerkaI18nManager extends EventEmitter{
|
|||||||
await this.emit(language) // 触发语言切换事件
|
await this.emit(language) // 触发语言切换事件
|
||||||
return language
|
return language
|
||||||
}else{
|
}else{
|
||||||
throw new Error("Not supported language:"+language)
|
throw new InvalidLanguageError()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user