update docs and fix small bugs

This commit is contained in:
wxzhang 2022-08-26 17:40:22 +08:00
parent 81f5968d39
commit 74096ea312
6 changed files with 25 additions and 15 deletions

View File

@ -75,6 +75,10 @@ function addSplitChars(str,bits=3){
.replace("{unit}",unitName)
}
const currencyFormatter = FlexFormatter((value,params={},$config)=>{
// format可以取预设值的名称如long,short等
if(params.format in $config){
params.format = $config[params.format]
}
params.unit = parseInt(params.unit) || 0
if(params.unit>$config.units.length-1) params.unit = $config.units.length-1
if(params.unit<0) params.unit = 0

View File

@ -297,7 +297,7 @@ const formatterNestingParamsRegex = String.raw`((([\'\"])(.*?)\3))|__TAG_REGEXP_
}
return fn.call(this,finalValue,...finalArgs,formatterConfig)
}
$formatter.configurable = true // 当函数是可配置时才在最后一个参数中传入$config
$formatter.configurable = true
return $formatter
}
@ -339,10 +339,6 @@ const createFlexFormatter = function(fn,options={},defaultParams={}){
if(args[i]!==undefined) finalParams[options.params[i]] = args[i]
}
}
if(finalParams.format in $config){
finalParams.format = $config[finalParams.format]
}
return fn.call(this,value,finalParams,$config)
},{...options,params:null}) // 变参工式化器需要指定params=null
return $flexFormatter

View File

@ -125,8 +125,7 @@ module.exports = {
prefix : "USD", // 前缀
suffix : "", // 后缀
division : 3, // ,分割位
precision : 2, // 精度
precision : 2, // 精度
},
number : {
division : 3, // , 分割位3代表每3位添加一个,

View File

@ -208,6 +208,7 @@ function getFormatter(scope, activeLanguage, name) {
const range = [
scope.activeFormatters,
scope.formatters[fallbackLanguage], // 如果指定了回退语言时,也在该回退语言中查找
scope.formatters["*"],
scope.global.formatters[activeLanguage], // 适用于activeLanguage全局格式化器
scope.global.formatters[fallbackLanguage],
scope.global.formatters["*"], // 适用于所有语言的格式化器

View File

@ -1,6 +1,6 @@
{
"name": "@voerkai18n/runtime",
"version": "1.1.5",
"version": "1.1.6",
"description": "核心运行时",
"main": "./dist/index.cjs",
"module": "dist/index.esm.js",
@ -36,5 +36,5 @@
"rollup-plugin-clear": "^2.0.7",
"rollup-plugin-terser": "^7.0.2"
},
"lastPublish": "2022-08-25T22:07:01+08:00"
"lastPublish": "2022-08-26T17:39:39+08:00"
}

View File

@ -96,6 +96,7 @@ module.exports = class i18nScope {
this.global.registerFormatter(name, formatter, { language });
} else {
language.forEach((lng) => {
if(!(lng in this._formatters)) this._formatters[lng] = {}
if (DataTypes.includes(name)) {
this._formatters[lng].$types[name] = formatter;
} else {
@ -182,16 +183,25 @@ module.exports = class i18nScope {
* - scope.activeFormatters.$config 当前优先
*/
_generateFormatterConfig(language){
let options
try{
options = deepClone(getByPath(this._global.formatters,`*.$config`,{}))
deepMixin(options,getByPath(this._global.formatters,`${language}.$config`,{}))
deepMixin(options,getByPath(this._activeFormatters,"$config",{}))
const fallbackLanguage = this.getLanguage(language).fallback;
let configSources = [
getByPath(this._global.formatters,`${fallbackLanguage}.$config`,{}),
getByPath(this._global.formatters,"*.$config",{}),
getByPath(this._formatters,`${fallbackLanguage}.$config`,{}),
getByPath(this._formatter,"*.$config",{}),
getByPath(this._global.formatters,`${language}.$config`,{}),
getByPath(this._activeFormatters,"$config",{})
]
return this._activeFormatterConfig = configSources.reduce((finalConfig, config)=>{
if(isPlainObject(config)) deepMixin(finalConfig,config)
return finalConfig
},deepClone(getByPath(this._global.formatters,`*.$config`,{})))
}catch(e){
if(this.debug) console.error(`Error while generate <${language}> formatter options: `,e)
if(!options) options = this._activeFormatters.$config || {}
return this._activeFormatters.$config || {}
}
return this._activeFormatterConfig = options
}
/**