1

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(Globalize.load)」の後に、このメソッドを「then」チェーンに含めました。これは適切な方法ですか?

更新: 最終作業バージョン - @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); });
4

1 に答える 1

1

このソリューションは、ラテン語とは異なる数字を使用するアラビア語やその他の言語を処理しないことに注意してください。したがって、お勧めしません。これよりも優れたアルゴリズムがあるはずです。

Globalize クラスを拡張する方法を示す一般的な目的のために、以下に進みます。

まず、core モジュールと number モジュールが含まれていることを確認します。次に、次のことができます。

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" );
于 2015-08-28T11:59:30.620 に答える