diff --git a/packages/runtime/datatypes/currency.js b/packages/runtime/datatypes/currency.js index fa32560..537c498 100644 --- a/packages/runtime/datatypes/currency.js +++ b/packages/runtime/datatypes/currency.js @@ -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 diff --git a/packages/runtime/formatter.js b/packages/runtime/formatter.js index cc95de7..ed2132e 100644 --- a/packages/runtime/formatter.js +++ b/packages/runtime/formatter.js @@ -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 diff --git a/packages/runtime/formatters/en.js b/packages/runtime/formatters/en.js index ba811db..a48f41a 100644 --- a/packages/runtime/formatters/en.js +++ b/packages/runtime/formatters/en.js @@ -125,8 +125,7 @@ module.exports = { prefix : "USD", // 前缀 suffix : "", // 后缀 division : 3, // ,分割位 - precision : 2, // 精度 - + precision : 2, // 精度 }, number : { division : 3, // , 分割位,3代表每3位添加一个, diff --git a/packages/runtime/interpolate.js b/packages/runtime/interpolate.js index 7caac27..3107eda 100644 --- a/packages/runtime/interpolate.js +++ b/packages/runtime/interpolate.js @@ -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["*"], // 适用于所有语言的格式化器 diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 0cfd893..31fb1d9 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -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" } \ No newline at end of file diff --git a/packages/runtime/scope.js b/packages/runtime/scope.js index 2c5f62f..323f7fd 100644 --- a/packages/runtime/scope.js +++ b/packages/runtime/scope.js @@ -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 } /**