Skip to content

Commit 3fe8b97

Browse files
committed
Add per locale Intl memoizer
1 parent f41d910 commit 3fe8b97

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

fluent-bundle/src/bundle.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FluentResource } from "./resource.js";
44
import { FluentValue, FluentNone, FluentFunction } from "./types.js";
55
import { Message, Term, Pattern } from "./ast.js";
66
import { NUMBER, DATETIME } from "./builtins.js";
7+
import { getMemoizerForLocale } from "./memoizer.js";
78

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

@@ -22,7 +23,7 @@ export class FluentBundle {
2223
public _functions: Record<string, FluentFunction>;
2324
public _useIsolating: boolean;
2425
public _transform: TextTransform;
25-
public _intls: WeakMap<object, Record<string, object>> = new WeakMap();
26+
public _intls: WeakMap<object, Record<string, object>>;
2627

2728
/**
2829
* Create an instance of `FluentBundle`.
@@ -73,6 +74,7 @@ export class FluentBundle {
7374
};
7475
this._useIsolating = useIsolating;
7576
this._transform = transform;
77+
this._intls = getMemoizerForLocale(locales);
7678
}
7779

7880
/**

fluent-bundle/src/memoizer.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type IntlCache = WeakMap<object, Record<string, object>>;
2+
3+
const cache = new Map<string, IntlCache>();
4+
5+
export function getMemoizerForLocale(locales: string | string[]): IntlCache {
6+
const stringLocale = Array.isArray(locales) ? locales.join(" ") : locales;
7+
let memoizer = cache.get(stringLocale);
8+
if (memoizer === undefined) {
9+
memoizer = new WeakMap();
10+
cache.set(stringLocale, memoizer);
11+
}
12+
13+
return memoizer;
14+
}

fluent-bundle/test/memoizer_test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
import assert from 'assert';
4+
import {getMemoizerForLocale} from '../esm/memoizer';
5+
6+
suite('Memoizer', function() {
7+
test('returns same instance for same locale', function() {
8+
const memoizer1 = getMemoizerForLocale('en')
9+
const memoizer2 = getMemoizerForLocale('en')
10+
assert.strictEqual(memoizer1, memoizer2)
11+
});
12+
13+
test('returns different instance for different locale', function() {
14+
const memoizer1 = getMemoizerForLocale('en')
15+
const memoizer2 = getMemoizerForLocale('uk')
16+
assert.notStrictEqual(memoizer1, memoizer2)
17+
});
18+
19+
test('works with array of locales', function() {
20+
const memoizer1 = getMemoizerForLocale(['en'])
21+
const memoizer2 = getMemoizerForLocale(['en'])
22+
assert.strictEqual(memoizer1, memoizer2)
23+
});
24+
25+
test('works with string and array of locales', function() {
26+
const memoizer1 = getMemoizerForLocale(['en'])
27+
const memoizer2 = getMemoizerForLocale('en')
28+
assert.strictEqual(memoizer1, memoizer2)
29+
})
30+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy