在 Globalize 中扩展的正确方法 1.x
Proper way of extending in Globalize 1.x
我有以下代码通过 Globalize 添加自定义功能 0.x:
Globalize.parseFloatAcceptDotAndComma =
function (value, radix, cultureSelector) {
value = value.replace(Globalize.locale(lang).numberFormat['.'] === '.' ? ',' : '.', Globalize.locale(lang).numberFormat['.']);
return Globalize.parseFloat.call(this, value, radix, cultureSelector);
}
由于 Globalize 1.x 插件的 API 不同,我很想知道如何在新版本的插件中实现相同的结果?
谢谢。
顺便说一句,我已将此方法包含在 "then" 链中,在 "then(Globalize.load)" 之后 - 这是正确的方法吗?
更新:最终工作版本 - 感谢@rxaviers
var lang = '@Thread.CurrentThread.CurrentUICulture.Name';
Promise.all([
// Core
fetch('/Scripts/cldr/supplemental/likelySubtags.json'),
// Date
fetch('/Scripts/cldr/main/' + lang + '/ca-gregorian.json'),
fetch('/Scripts/cldr/main/' + lang + '/timeZoneNames.json'),
fetch('/Scripts/cldr/supplemental/timeData.json'),
fetch('/Scripts/cldr/supplemental/weekData.json'),
// Number
fetch('/Scripts/cldr/main/' + lang + '/numbers.json'),
fetch('/Scripts/cldr/supplemental/numberingSystems.json')
])
.then(function(responses) {
return Promise.all(responses.map(function(response) {
return response.json();
}));
})
.then(Globalize.load)
.then(function () {
Globalize.parseFloatAcceptDotAndComma =
Globalize.prototype.parseFloatAcceptDotAndComma = function(value, options) {
// Assert that value and options are valid.
// Assert that this.cldr is present
if (value.indexOf('.') >= 0 && value.indexOf(',') >= 0) {
throw new Error('Both separators are present');
}
value = value.replace(/[,.]/, this.cldr.main('numbers/symbols-numberSystem-latn/decimal'));
return this.parseNumber(value, options);
}
})
.then(function() { Globalize.locale(lang); });
请注意,您的解决方案无法处理阿拉伯语和其他使用与拉丁语不同的数字的语言。因此,我不推荐它。我相信应该有比这个更好的算法。
为了展示如何扩展 Globalize class,我们开始:
首先,确保您已经包含了核心模块和数字模块。然后,您可以:
Globalize.parseFloatAcceptDotAndComma =
Globalize.prototype.parseFloatAcceptDotAndComma = function( value, options ) {
// Assert that value and options are valid.
// Assert that this.cldr is present
// I guess you should throw when both . and , are present.
if ( value.indexOf( "." ) >= 0 && value.indexOf( "," ) >= 0 ) {
throw new Error("Whops");
}
// Important:
// Note your solution won't handle Arabic and other languages that
// uses different digits than latin. Therefore, I DO NOT personally
// recommend it.
value = value.replace( /[,.]/, this.cldr.main( "numbers/symbols-numberSystem-latn/decimal" ));
return this.parseNumber( value, options );
};
您将可以像这样使用它:
Globalize.locale( "en" );
Globalize.parseFloatAcceptDotAndComma( "3,14" );
// Or
var en = new Globalize( "en" );
en.parseFloatAcceptDotAndComma( "3,14" );
我有以下代码通过 Globalize 添加自定义功能 0.x:
Globalize.parseFloatAcceptDotAndComma =
function (value, radix, cultureSelector) {
value = value.replace(Globalize.locale(lang).numberFormat['.'] === '.' ? ',' : '.', Globalize.locale(lang).numberFormat['.']);
return Globalize.parseFloat.call(this, value, radix, cultureSelector);
}
由于 Globalize 1.x 插件的 API 不同,我很想知道如何在新版本的插件中实现相同的结果?
谢谢。
顺便说一句,我已将此方法包含在 "then" 链中,在 "then(Globalize.load)" 之后 - 这是正确的方法吗?
更新:最终工作版本 - 感谢@rxaviers
var lang = '@Thread.CurrentThread.CurrentUICulture.Name';
Promise.all([
// Core
fetch('/Scripts/cldr/supplemental/likelySubtags.json'),
// Date
fetch('/Scripts/cldr/main/' + lang + '/ca-gregorian.json'),
fetch('/Scripts/cldr/main/' + lang + '/timeZoneNames.json'),
fetch('/Scripts/cldr/supplemental/timeData.json'),
fetch('/Scripts/cldr/supplemental/weekData.json'),
// Number
fetch('/Scripts/cldr/main/' + lang + '/numbers.json'),
fetch('/Scripts/cldr/supplemental/numberingSystems.json')
])
.then(function(responses) {
return Promise.all(responses.map(function(response) {
return response.json();
}));
})
.then(Globalize.load)
.then(function () {
Globalize.parseFloatAcceptDotAndComma =
Globalize.prototype.parseFloatAcceptDotAndComma = function(value, options) {
// Assert that value and options are valid.
// Assert that this.cldr is present
if (value.indexOf('.') >= 0 && value.indexOf(',') >= 0) {
throw new Error('Both separators are present');
}
value = value.replace(/[,.]/, this.cldr.main('numbers/symbols-numberSystem-latn/decimal'));
return this.parseNumber(value, options);
}
})
.then(function() { Globalize.locale(lang); });
请注意,您的解决方案无法处理阿拉伯语和其他使用与拉丁语不同的数字的语言。因此,我不推荐它。我相信应该有比这个更好的算法。
为了展示如何扩展 Globalize class,我们开始:
首先,确保您已经包含了核心模块和数字模块。然后,您可以:
Globalize.parseFloatAcceptDotAndComma =
Globalize.prototype.parseFloatAcceptDotAndComma = function( value, options ) {
// Assert that value and options are valid.
// Assert that this.cldr is present
// I guess you should throw when both . and , are present.
if ( value.indexOf( "." ) >= 0 && value.indexOf( "," ) >= 0 ) {
throw new Error("Whops");
}
// Important:
// Note your solution won't handle Arabic and other languages that
// uses different digits than latin. Therefore, I DO NOT personally
// recommend it.
value = value.replace( /[,.]/, this.cldr.main( "numbers/symbols-numberSystem-latn/decimal" ));
return this.parseNumber( value, options );
};
您将可以像这样使用它:
Globalize.locale( "en" );
Globalize.parseFloatAcceptDotAndComma( "3,14" );
// Or
var en = new Globalize( "en" );
en.parseFloatAcceptDotAndComma( "3,14" );