update
This commit is contained in:
parent
e7318e2de4
commit
5ab0146e99
@ -4,6 +4,8 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
import { FormatterStore } from './formatterStore';
|
||||||
|
import type { VoerkaI18nScope } from './scope';
|
||||||
import { VoerkaI18nFormatter, VoerkaI18nFormatters, VoerkaI18nFormattersLoader, VoerkaI18nLanguageFormatters, SupportedDateTypes, VoerkaI18nFormatterConfigs } from './types';
|
import { VoerkaI18nFormatter, VoerkaI18nFormatters, VoerkaI18nFormattersLoader, VoerkaI18nLanguageFormatters, SupportedDateTypes, VoerkaI18nFormatterConfigs } from './types';
|
||||||
import { DataTypes } from './utils';
|
import { DataTypes } from './utils';
|
||||||
import { get as getByPath } from "flex-tools/object/get"
|
import { get as getByPath } from "flex-tools/object/get"
|
||||||
@ -26,14 +28,13 @@ const EmptyFormatters:any = {
|
|||||||
$types:{}
|
$types:{}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class VoerkaI18nFormatterRegistry{
|
export class VoerkaI18nFormatterRegistry extends FormatterStore{
|
||||||
// 由于语言的格式化器集合允许是一个异步加载块,所以需要一个ready标志
|
// 由于语言的格式化器集合允许是一个异步加载块,所以需要一个ready标志
|
||||||
// 当语言格式化器集合加载完成后,ready标志才会变为true
|
// 当语言格式化器集合加载完成后,ready标志才会变为true
|
||||||
#ready:boolean = false
|
#ready:boolean = false
|
||||||
#formatters:VoerkaI18nLanguageFormatters = {}
|
|
||||||
#language:string = 'zh'
|
#language:string = 'zh'
|
||||||
constructor(){
|
constructor(scope:VoerkaI18nScope){
|
||||||
|
super()
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 当前语言
|
* 当前语言
|
||||||
@ -46,42 +47,6 @@ export class VoerkaI18nFormatterRegistry{
|
|||||||
}
|
}
|
||||||
this.#ready = typeof(this.#formatters[language]) != 'function'
|
this.#ready = typeof(this.#formatters[language]) != 'function'
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 注册格式化器
|
|
||||||
|
|
||||||
格式化器是一个简单的同步函数value=>{...},用来对输入进行格式化后返回结果
|
|
||||||
|
|
||||||
register(name,value=>{...}) // 注册到所有语言
|
|
||||||
register(name,value=>{...},{langauge:"zh"}) // 注册到zh语言
|
|
||||||
register(name,value=>{...},{langauge:"en"}) // 注册到en语言
|
|
||||||
register("Date",value=>{...},{langauge:"en"}) // 注册到en语言的默认数据类型格式化器
|
|
||||||
register(name,value=>{...},{langauge:["zh","cht"]}) // 注册到zh和cht语言
|
|
||||||
register(name,value=>{...},{langauge:"zh,cht"})
|
|
||||||
@param {*} formatter 格式化器
|
|
||||||
@param language : 字符串或数组,声明该格式化器适用语言
|
|
||||||
|
|
||||||
*代表适用于所有语言
|
|
||||||
语言名称,语言名称数组,或者使用,分割的语言名称字符串
|
|
||||||
*/
|
|
||||||
register(name:string | SupportedDateTypes, formatter:VoerkaI18nFormatter, {language = "*"}:{ language: string | string[] } ) {
|
|
||||||
if (!isFunction(formatter) || typeof name !== "string") {
|
|
||||||
throw new TypeError("Formatter must be a function");
|
|
||||||
}
|
|
||||||
const languages = Array.isArray(language) ? language: language ? language.split(","): [];
|
|
||||||
languages.forEach((lngName:string) => {
|
|
||||||
if(!(lngName in this.#formatters)) this.#formatters[lngName] = {}
|
|
||||||
if(typeof(this.#formatters[lngName])!="function"){
|
|
||||||
let lngFormatters = this.#formatters[lngName] as any
|
|
||||||
if (DataTypes.includes(name)) {
|
|
||||||
if(!lngFormatters.$types) lngFormatters.$types = {}
|
|
||||||
lngFormatters.$types![name] = formatter
|
|
||||||
} else {
|
|
||||||
lngFormatters[name] = formatter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
registerLanguageFormatters(language:string,formatters:VoerkaI18nFormatters | VoerkaI18nFormattersLoader){
|
registerLanguageFormatters(language:string,formatters:VoerkaI18nFormatters | VoerkaI18nFormattersLoader){
|
||||||
this.#formatters[language] = formatters
|
this.#formatters[language] = formatters
|
||||||
}
|
}
|
||||||
|
37
packages/runtime/src/formatterStore.ts
Normal file
37
packages/runtime/src/formatterStore.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { isFunction } from "flex-tools/typecheck/isFunction";
|
||||||
|
import { SupportedDateTypes, VoerkaI18nFormatter, VoerkaI18nLanguageFormatters } from "./types";
|
||||||
|
import { DataTypes } from "./utils";
|
||||||
|
|
||||||
|
|
||||||
|
export class FormatterStore{
|
||||||
|
#formatters:VoerkaI18nLanguageFormatters = {}
|
||||||
|
constructor(formatters?:VoerkaI18nLanguageFormatters){
|
||||||
|
if(formatters) this.#formatters = formatters
|
||||||
|
}
|
||||||
|
get formatters(){ return this.#formatters }
|
||||||
|
register(name:string | SupportedDateTypes, formatter:VoerkaI18nFormatter, {language = "*"}:{ language: string | string[] } ) {
|
||||||
|
if (!isFunction(formatter) || typeof name !== "string") {
|
||||||
|
throw new TypeError("Formatter must be a function");
|
||||||
|
}
|
||||||
|
const languages = Array.isArray(language) ? language: language ? language.split(","): [];
|
||||||
|
languages.forEach((lngName:string) => {
|
||||||
|
if(!(lngName in this.#formatters)) this.#formatters[lngName] = {}
|
||||||
|
if(typeof(this.#formatters[lngName])!="function"){
|
||||||
|
let lngFormatters = this.#formatters[lngName] as any
|
||||||
|
if (DataTypes.includes(name)) {
|
||||||
|
if(!lngFormatters.$types) lngFormatters.$types = {}
|
||||||
|
lngFormatters.$types![name] = formatter
|
||||||
|
} else {
|
||||||
|
lngFormatters[name] = formatter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
getLanguageFormatters(language:string){
|
||||||
|
if(language in this.#formatters){
|
||||||
|
return this.#formatters[language]
|
||||||
|
}else{
|
||||||
|
return this.#formatters["*"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user