Skip to content

Commit

Permalink
cache bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
agubler committed Feb 3, 2020
1 parent 90a5409 commit 5ab6911
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ function registerBundle<T extends Messages>(bundle: Bundle<T>): string {
return bundleId;
}

const cachedBundleMap = new WeakMap<Bundle<any>, Map<string, LocalizeResult<Bundle<any>>>>();

export function localizeBundle<T extends Messages>(
bundle: Bundle<T>,
options: LocalizeOptions
Expand Down Expand Up @@ -350,18 +352,25 @@ export function localizeBundle<T extends Messages>(
return getPlaceholderBundle(bundle);
}

return {
messages: Object.keys(bundle.messages).reduce(
(messages, key) => {
const message = globalize.cldr.get(`${MESSAGE_BUNDLE_PATH}/${bundleId}/${key}`);
messages[key] = message;
return messages;
},
{} as any
),
isPlaceholder: false,
format: (key: any, options: {}) => {
return globalize.formatMessage(`${bundleId}/${key}`, options);
}
};
const cachedLocaleMessagesMap = cachedBundleMap.get(bundle) || new Map<string, LocalizeResult<Bundle<any>>>();
let localizedBundleMessages = cachedLocaleMessagesMap.get(locale);
if (!localizedBundleMessages) {
localizedBundleMessages = {
messages: Object.keys(bundle.messages).reduce(
(messages, key) => {
const message = globalize.cldr.get(`${MESSAGE_BUNDLE_PATH}/${bundleId}/${key}`);
messages[key] = message;
return messages;
},
{} as any
),
isPlaceholder: false,
format: (key: any, options: {}) => {
return globalize.formatMessage(`${bundleId}/${key}`, options);
}
};
cachedLocaleMessagesMap.set(locale, localizedBundleMessages);
cachedBundleMap.set(bundle, cachedLocaleMessagesMap);
}
return localizedBundleMessages;
}

0 comments on commit 5ab6911

Please sign in to comment.