File tree Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Expand file tree Collapse file tree 3 files changed +51
-0
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
@@ -78,6 +79,7 @@ export class FluentBundle {
7879 } ;
7980 this . _useIsolating = useIsolating ;
8081 this . _transform = transform ;
82+ this . _intls = getMemoizerForLocale ( locales ) ;
8183 }
8284
8385 /**
Original file line number Diff line number Diff line change 1+ type IntlCache = WeakMap <
2+ | typeof Intl . NumberFormat
3+ | typeof Intl . DateTimeFormat
4+ | typeof Intl . PluralRules ,
5+ Record < string , Intl . NumberFormat | Intl . DateTimeFormat | Intl . PluralRules >
6+ > ;
7+
8+ const cache = new Map < string , IntlCache > ( ) ;
9+
10+ export function getMemoizerForLocale ( locales : string | string [ ] ) : IntlCache {
11+ const stringLocale = Array . isArray ( locales ) ? locales . join ( " " ) : locales ;
12+ let memoizer = cache . get ( stringLocale ) ;
13+ if ( memoizer === undefined ) {
14+ memoizer = new WeakMap ( ) ;
15+ cache . set ( stringLocale , memoizer ) ;
16+ }
17+
18+ return memoizer ;
19+ }
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.js' ;
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