Skip to content

Commit

Permalink
Address feedback on PR + clear cache before test case
Browse files Browse the repository at this point in the history
  • Loading branch information
heharm committed Aug 14, 2024
1 parent a14bdc8 commit cd91e4a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 47 deletions.
17 changes: 10 additions & 7 deletions dist/cjs/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -1131,28 +1131,31 @@ class PluralResolver {
this.logger.error('Your environment seems not to be Intl API compatible, use an Intl.PluralRules polyfill. Will fallback to the compatibilityJSON v3 format handling.');
}
this.rules = createRules();
this.pluralRulesCache = new Map();
this.pluralRulesCache = {};
}
addRule(lng, obj) {
this.rules[lng] = obj;
}
clearCache() {
this.pluralRulesCache = {};
}
getRule(code) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.shouldUseIntlApi()) {
try {
const locales = getCleanedCode(code === 'dev' ? 'en' : code);
const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
const type = options.ordinal ? 'ordinal' : 'cardinal';
const cacheKey = JSON.stringify({
locales,
cleanedCode,
type
});
if (this.pluralRulesCache.has(cacheKey)) {
return this.pluralRulesCache.get(cacheKey);
if (cacheKey in this.pluralRulesCache) {
return this.pluralRulesCache[cacheKey];
}
const rule = new Intl.PluralRules(locales, {
const rule = new Intl.PluralRules(cleanedCode, {
type
});
this.pluralRulesCache.set(cacheKey, rule);
this.pluralRulesCache[cacheKey] = rule;
return rule;
} catch (err) {
return;
Expand Down
17 changes: 10 additions & 7 deletions dist/esm/i18next.bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,28 +1512,31 @@ class PluralResolver {
);
}
this.rules = createRules();
this.pluralRulesCache = new Map();
this.pluralRulesCache = {};
}
addRule(lng, obj) {
this.rules[lng] = obj;
}
clearCache() {
this.pluralRulesCache = {};
}
getRule(code) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.shouldUseIntlApi()) {
try {
const locales = getCleanedCode(code === 'dev' ? 'en' : code);
const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
const type = options.ordinal ? 'ordinal' : 'cardinal';
const cacheKey = JSON.stringify({
locales,
cleanedCode,
type,
});
if (this.pluralRulesCache.has(cacheKey)) {
return this.pluralRulesCache.get(cacheKey);
if (cacheKey in this.pluralRulesCache) {
return this.pluralRulesCache[cacheKey];
}
const rule = new Intl.PluralRules(locales, {
const rule = new Intl.PluralRules(cleanedCode, {
type,
});
this.pluralRulesCache.set(cacheKey, rule);
this.pluralRulesCache[cacheKey] = rule;
return rule;
} catch (err) {
return;
Expand Down
17 changes: 10 additions & 7 deletions dist/esm/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -1129,28 +1129,31 @@ class PluralResolver {
this.logger.error('Your environment seems not to be Intl API compatible, use an Intl.PluralRules polyfill. Will fallback to the compatibilityJSON v3 format handling.');
}
this.rules = createRules();
this.pluralRulesCache = new Map();
this.pluralRulesCache = {};
}
addRule(lng, obj) {
this.rules[lng] = obj;
}
clearCache() {
this.pluralRulesCache = {};
}
getRule(code) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.shouldUseIntlApi()) {
try {
const locales = getCleanedCode(code === 'dev' ? 'en' : code);
const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
const type = options.ordinal ? 'ordinal' : 'cardinal';
const cacheKey = JSON.stringify({
locales,
cleanedCode,
type
});
if (this.pluralRulesCache.has(cacheKey)) {
return this.pluralRulesCache.get(cacheKey);
if (cacheKey in this.pluralRulesCache) {
return this.pluralRulesCache[cacheKey];
}
const rule = new Intl.PluralRules(locales, {
const rule = new Intl.PluralRules(cleanedCode, {
type
});
this.pluralRulesCache.set(cacheKey, rule);
this.pluralRulesCache[cacheKey] = rule;
return rule;
} catch (err) {
return;
Expand Down
17 changes: 10 additions & 7 deletions dist/umd/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,28 +1135,31 @@
this.logger.error('Your environment seems not to be Intl API compatible, use an Intl.PluralRules polyfill. Will fallback to the compatibilityJSON v3 format handling.');
}
this.rules = createRules();
this.pluralRulesCache = new Map();
this.pluralRulesCache = {};
}
addRule(lng, obj) {
this.rules[lng] = obj;
}
clearCache() {
this.pluralRulesCache = {};
}
getRule(code) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.shouldUseIntlApi()) {
try {
const locales = getCleanedCode(code === 'dev' ? 'en' : code);
const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
const type = options.ordinal ? 'ordinal' : 'cardinal';
const cacheKey = JSON.stringify({
locales,
cleanedCode,
type
});
if (this.pluralRulesCache.has(cacheKey)) {
return this.pluralRulesCache.get(cacheKey);
if (cacheKey in this.pluralRulesCache) {
return this.pluralRulesCache[cacheKey];
}
const rule = new Intl.PluralRules(locales, {
const rule = new Intl.PluralRules(cleanedCode, {
type
});
this.pluralRulesCache.set(cacheKey, rule);
this.pluralRulesCache[cacheKey] = rule;
return rule;
} catch (err) {
return;
Expand Down
2 changes: 1 addition & 1 deletion dist/umd/i18next.min.js

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,28 +1135,31 @@
this.logger.error('Your environment seems not to be Intl API compatible, use an Intl.PluralRules polyfill. Will fallback to the compatibilityJSON v3 format handling.');
}
this.rules = createRules();
this.pluralRulesCache = new Map();
this.pluralRulesCache = {};
}
addRule(lng, obj) {
this.rules[lng] = obj;
}
clearCache() {
this.pluralRulesCache = {};
}
getRule(code) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.shouldUseIntlApi()) {
try {
const locales = getCleanedCode(code === 'dev' ? 'en' : code);
const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
const type = options.ordinal ? 'ordinal' : 'cardinal';
const cacheKey = JSON.stringify({
locales,
cleanedCode,
type
});
if (this.pluralRulesCache.has(cacheKey)) {
return this.pluralRulesCache.get(cacheKey);
if (cacheKey in this.pluralRulesCache) {
return this.pluralRulesCache[cacheKey];
}
const rule = new Intl.PluralRules(locales, {
const rule = new Intl.PluralRules(cleanedCode, {
type
});
this.pluralRulesCache.set(cacheKey, rule);
this.pluralRulesCache[cacheKey] = rule;
return rule;
} catch (err) {
return;
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions src/PluralResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,31 @@ class PluralResolver {
}

this.rules = createRules();
this.pluralRulesCache = new Map();

// Cache calls to Intl.PluralRules, since repeated calls can be slow in runtimes like React Native
// and the memory usage difference is negligible
this.pluralRulesCache = {};
}

addRule(lng, obj) {
this.rules[lng] = obj;
}

clearCache() {
this.pluralRulesCache = {};
}

getRule(code, options = {}) {
if (this.shouldUseIntlApi()) {
try {
const locales = getCleanedCode(code === 'dev' ? 'en' : code);
const cleanedCode = getCleanedCode(code === 'dev' ? 'en' : code);
const type = options.ordinal ? 'ordinal' : 'cardinal';
const cacheKey = JSON.stringify({locales, type});
if (this.pluralRulesCache.has(cacheKey)) {
return this.pluralRulesCache.get(cacheKey);
const cacheKey = JSON.stringify({ cleanedCode, type });
if (cacheKey in this.pluralRulesCache) {
return this.pluralRulesCache[cacheKey];
}

const rule = new Intl.PluralRules(locales, {
type
});
this.pluralRulesCache.set(cacheKey, rule)
const rule = new Intl.PluralRules(cleanedCode, { type });
this.pluralRulesCache[cacheKey] = rule;
return rule;
} catch (err) {
return;
Expand Down
1 change: 1 addition & 0 deletions test/runtime/pluralResolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('PluralResolver', () => {
});

it('correctly returns getRule for an unsupported locale', () => {
pr.clearCache();
const pluralRulesSpy = vitest.spyOn(Intl, 'PluralRules').mockImplementation(() => {
throw Error('mock error');
});
Expand Down

0 comments on commit cd91e4a

Please sign in to comment.