Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add per locale Intl memoizer #504

Merged
merged 2 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions fluent-bundle/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FluentResource } from "./resource.js";
import { FluentValue, FluentNone, FluentFunction } from "./types.js";
import { Message, Term, Pattern } from "./ast.js";
import { NUMBER, DATETIME } from "./builtins.js";
import { getMemoizerForLocale, IntlCache } from "./memoizer.js";

export type TextTransform = (text: string) => string;

Expand All @@ -22,12 +23,7 @@ export class FluentBundle {
public _functions: Record<string, FluentFunction>;
public _useIsolating: boolean;
public _transform: TextTransform;
public _intls = new WeakMap<
| typeof Intl.NumberFormat
| typeof Intl.DateTimeFormat
| typeof Intl.PluralRules,
Record<string, Intl.NumberFormat | Intl.DateTimeFormat | Intl.PluralRules>
>();
public _intls: IntlCache;

/**
* Create an instance of `FluentBundle`.
Expand Down Expand Up @@ -78,6 +74,7 @@ export class FluentBundle {
};
this._useIsolating = useIsolating;
this._transform = transform;
this._intls = getMemoizerForLocale(locales);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions fluent-bundle/src/memoizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type IntlCache = Map<
| typeof Intl.NumberFormat
| typeof Intl.DateTimeFormat
| typeof Intl.PluralRules,
Record<string, Intl.NumberFormat | Intl.DateTimeFormat | Intl.PluralRules>
>;

const cache = new Map<string, IntlCache>();

export function getMemoizerForLocale(locales: string | string[]): IntlCache {
const stringLocale = Array.isArray(locales) ? locales.join(" ") : locales;
let memoizer = cache.get(stringLocale);
if (memoizer === undefined) {
memoizer = new Map();
cache.set(stringLocale, memoizer);
}

return memoizer;
}
30 changes: 30 additions & 0 deletions fluent-bundle/test/memoizer_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

import assert from 'assert';
import {getMemoizerForLocale} from '../esm/memoizer.js';

suite('Memoizer', function() {
test('returns same instance for same locale', function() {
const memoizer1 = getMemoizerForLocale('en')
const memoizer2 = getMemoizerForLocale('en')
assert.strictEqual(memoizer1, memoizer2)
});

test('returns different instance for different locale', function() {
const memoizer1 = getMemoizerForLocale('en')
const memoizer2 = getMemoizerForLocale('uk')
assert.notStrictEqual(memoizer1, memoizer2)
});

test('works with array of locales', function() {
const memoizer1 = getMemoizerForLocale(['en'])
const memoizer2 = getMemoizerForLocale(['en'])
assert.strictEqual(memoizer1, memoizer2)
});

test('works with string and array of locales', function() {
const memoizer1 = getMemoizerForLocale(['en'])
const memoizer2 = getMemoizerForLocale('en')
assert.strictEqual(memoizer1, memoizer2)
})
})