Skip to content

Commit

Permalink
cache message formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
meadowsys committed Apr 1, 2023
1 parent 0030680 commit c7b5d7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/i18n-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,6 @@ module.exports = {
I18nCacheHelper,
walkStrings,
travelDownObjectPath,
optionalTravelDownObjectPath
optionalTravelDownObjectPath,
travelDownOrMakePath
};
16 changes: 13 additions & 3 deletions src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const {
I18nCacheHelper,
walkStrings,
travelDownObjectPath,
optionalTravelDownObjectPath
optionalTravelDownObjectPath,
travelDownOrMakePath
} = require("./i18n-helpers");

class I18n {
Expand Down Expand Up @@ -215,9 +216,18 @@ class I18n {
* formats a string with opts,
* and caches the message formatter for the provided path.
*/
format(ns, path, str, lang, opts) {
let ast = this.cacheHelper.fetchAST(ns, path, str, lang);
format(ns, _path, str, lang, opts) {
let path = [ns, lang, ..._path];

let cachedFormatter = optionalTravelDownObjectPath(this.cachedFormatters, path);
if (cachedFormatter) return cachedFormatter.format(opts);

let ast = this.cacheHelper.fetchAST(ns, _path, str, lang);
let formatter = new IntlMessageFormat(ast, lang);

let last = path.pop();
let cachePath = travelDownOrMakePath(this.cachedFormatters, path);
cachePath[last] = formatter;
return formatter.format(opts);
}
}
Expand Down

0 comments on commit c7b5d7c

Please sign in to comment.