Skip to content

Commit

Permalink
memoize option for fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
jamuhl committed Jun 14, 2018
1 parent 495dd17 commit 133d297
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 0.4.0
- with i18next >=v11.3.3 only memoize if successful lookup

### 0.3.0
- allow to pass in localeData in init options

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ i18next
// per default icu functions are parsed once and cached for subsequent calls
memoize: true,

// memoize if not having a lookup and just using the key fallback as value
memoizeFallback: false,

// array or single object of loaded 'i18next-icu/locale-data
localeData: null
}
Expand Down
9 changes: 6 additions & 3 deletions i18nextICU.js
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

function getDefaults() {
return {
memoize: true
memoize: true,
memoizeFallback: false
};
}

Expand Down Expand Up @@ -2041,14 +2042,16 @@ var ICU = function () {
}
}, {
key: 'parse',
value: function parse(res, options, lng, ns, key) {
value: function parse(res, options, lng, ns, key, info) {
var hadSuccessfulLookup = info && info.resolved && info.resolved.res;

var fc = void 0;
if (this.options.memoize) {
fc = getPath(this.mem, lng + '.' + ns + '.' + key);
}
if (!fc) {
fc = new MessageFormat(res, lng);
if (this.options.memoize) setPath(this.mem, lng + '.' + ns + '.' + key, fc);
if (this.options.memoize && (this.options.memoizeFallback || !info || hadSuccessfulLookup)) setPath(this.mem, lng + '.' + ns + '.' + key, fc);
}
return fc.format(options);
}
Expand Down
2 changes: 1 addition & 1 deletion i18nextICU.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "i18next-icu",
"version": "0.3.0",
"version": "0.4.0",
"description": "i18nFormat plugin to use ICU format with i18next",
"main": "./index.js",
"jsnext:main": "dist/es/index.js",
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import IntlMessageFormat from 'intl-messageformat';

function getDefaults() {
return {
memoize: true
memoize: true,
memoizeFallback: false
};
}

Expand Down Expand Up @@ -38,14 +39,16 @@ class ICU {
});
}

parse(res, options, lng, ns, key) {
parse(res, options, lng, ns, key, info) {
const hadSuccessfulLookup = info && info.resolved && info.resolved.res;

let fc;
if (this.options.memoize) {
fc = utils.getPath(this.mem, `${lng}.${ns}.${key}`);
}
if (!fc) {
fc = new IntlMessageFormat(res, lng);
if (this.options.memoize) utils.setPath(this.mem, `${lng}.${ns}.${key}`, fc);
if (this.options.memoize && (this.options.memoizeFallback || !info || hadSuccessfulLookup)) utils.setPath(this.mem, `${lng}.${ns}.${key}`, fc);
}
return fc.format(options);
}
Expand Down

0 comments on commit 133d297

Please sign in to comment.