diff --git a/fluent/src/bundle.js b/fluent/src/bundle.js index e81a6e633..4b48ded99 100644 --- a/fluent/src/bundle.js +++ b/fluent/src/bundle.js @@ -235,16 +235,4 @@ export default class FluentBundle { throw err; } } - - _memoizeIntlObject(ctor, opts) { - const cache = this._intls.get(ctor) || {}; - const id = JSON.stringify(opts); - - if (!cache[id]) { - cache[id] = new ctor(this.locales, opts); - this._intls.set(ctor, cache); - } - - return cache[id]; - } } diff --git a/fluent/src/resolver.js b/fluent/src/resolver.js index eb278c029..2ec408af0 100644 --- a/fluent/src/resolver.js +++ b/fluent/src/resolver.js @@ -48,7 +48,7 @@ const PDI = "\u2069"; // Helper: match a variant key to the given selector. -function match(bundle, selector, key) { +function match(scope, selector, key) { if (key === selector) { // Both are strings. return true; @@ -62,8 +62,8 @@ function match(bundle, selector, key) { } if (selector instanceof FluentNumber && typeof key === "string") { - let category = bundle - ._memoizeIntlObject(Intl.PluralRules, selector.opts) + let category = scope + .memoizeIntlObject(Intl.PluralRules, selector.opts) .select(selector.value); if (key === category) { return true; @@ -240,7 +240,7 @@ function SelectExpression(scope, {selector, variants, star}) { // Match the selector against keys of each variant, in order. for (const variant of variants) { const key = resolveExpression(scope, variant.key); - if (match(scope.bundle, sel, key)) { + if (match(scope, sel, key)) { return resolvePattern(scope, variant.value); } } diff --git a/fluent/src/scope.js b/fluent/src/scope.js index 14133afdf..63f1ea217 100644 --- a/fluent/src/scope.js +++ b/fluent/src/scope.js @@ -26,4 +26,17 @@ export default class Scope { } this.errors.push(error); } + + memoizeIntlObject(ctor, opts) { + let cache = this.bundle._intls.get(ctor); + if (!cache) { + cache = {}; + this.bundle._intls.set(ctor, cache); + } + let id = JSON.stringify(opts); + if (!cache[id]) { + cache[id] = new ctor(this.bundle.locales, opts); + } + return cache[id]; + } } diff --git a/fluent/src/types.js b/fluent/src/types.js index a2805e3ca..7c101fdbd 100644 --- a/fluent/src/types.js +++ b/fluent/src/types.js @@ -61,9 +61,7 @@ export class FluentNumber extends FluentType { toString(scope) { try { - const nf = scope.bundle._memoizeIntlObject( - Intl.NumberFormat, this.opts - ); + const nf = scope.memoizeIntlObject(Intl.NumberFormat, this.opts); return nf.format(this.value); } catch (e) { // XXX Report the error. @@ -79,9 +77,7 @@ export class FluentDateTime extends FluentType { toString(scope) { try { - const dtf = scope.bundle._memoizeIntlObject( - Intl.DateTimeFormat, this.opts - ); + const dtf = scope.memoizeIntlObject(Intl.DateTimeFormat, this.opts); return dtf.format(this.value); } catch (e) { // XXX Report the error.