File tree Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Expand file tree Collapse file tree 3 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import { FluentResource } from "./resource.js";
44import { FluentValue , FluentNone , FluentFunction } from "./types.js" ;
55import { Message , Term , Pattern } from "./ast.js" ;
66import { NUMBER , DATETIME } from "./builtins.js" ;
7+ import { getMemoizerForLocale } from "./memoizer.js" ;
78
89export 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 /**
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments