Skip to content

Commit

Permalink
check only once for hasLoadedNamespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jamuhl committed Oct 26, 2019
1 parent 0cec587 commit 8b16945
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 18.0.1

- check loadedNamespace only once per lng-ns inside using `t` for better performance

### 18.0.0

- When calling `i18next.changeLanguage()` both `i18next.language` and `i18next.languages` will be set to the new language after calling `loadResources` -> means when accessing `t` function meanwhile you will get still the translations for the previous language instead of the fallback.
Expand Down
14 changes: 12 additions & 2 deletions src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import EventEmitter from './EventEmitter.js';
import postProcessor from './postProcessor.js';
import * as utils from './utils.js';

const checkedLoadedFor = {};

class Translator extends EventEmitter {
constructor(services, options = {}) {
super();
Expand Down Expand Up @@ -342,9 +344,17 @@ class Translator extends EventEmitter {
if (this.isValidLookup(found)) return;
usedNS = ns;

if (this.utils && this.utils.hasLoadedNamespace && !this.utils.hasLoadedNamespace(usedNS)) {
if (
!checkedLoadedFor[`${codes[0]}-${ns}`] &&
this.utils &&
this.utils.hasLoadedNamespace &&
!this.utils.hasLoadedNamespace(usedNS)
) {
checkedLoadedFor[`${codes[0]}-${ns}`] = true;
this.logger.warn(
`key "${usedKey}" for namespace "${usedNS}" won't get resolved as namespace was not yet loaded`,
`key "${usedKey}" for namespace "${usedNS}" for languages "${codes.join(
', ',
)}" won't get resolved as namespace was not yet loaded`,
'This means something IS WRONG in your application setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!',
);
}
Expand Down
32 changes: 16 additions & 16 deletions test/i18next.hasLoadedNamespace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,41 +96,41 @@ describe('i18next', () => {
expect(i18n.hasLoadedNamespace('ns1')).to.equal(false);
});

describe('backendConnector - saveMissing', () => {
it('it should call saveMissing create on backend if loaded ns', () => {
describe('translator - calling t', () => {
it('it should not log anything if loaded ns', () => {
i18n.t('keyNotFound');
expect(Backend.created.length).to.equal(1);
expect(Backend.created[0]).to.equal('dev-translation-keyNotFound');
Backend.reset();
expect(Logger.entries.warn.length).to.equal(0);
Logger.reset();
});

it('it should not call saveMissing create on backend if not loaded ns', () => {
i18n.t('ns1:keyNotFound');

expect(Backend.created.length).to.equal(0);
Backend.reset();

expect(Logger.entries.warn.length).to.equal(2);
expect(Logger.entries.warn[1]).to.equal(
'i18next::backendConnector: did not save key "keyNotFound" for namespace "ns1" as the namespace was not yet loaded',
expect(Logger.entries.warn[0]).to.equal(
'i18next::translator: key "keyNotFound" for namespace "ns1" for languages "en-US, en, dev" won\'t get resolved as namespace was not yet loaded',
);
Logger.reset();
});
});

describe('translator - calling t', () => {
it('it should not log anything if loaded ns', () => {
describe('backendConnector - saveMissing', () => {
it('it should call saveMissing create on backend if loaded ns', () => {
i18n.t('keyNotFound');
expect(Logger.entries.warn.length).to.equal(0);
Logger.reset();
expect(Backend.created.length).to.equal(1);
expect(Backend.created[0]).to.equal('dev-translation-keyNotFound');
Backend.reset();
});

it('it should not call saveMissing create on backend if not loaded ns', () => {
i18n.t('ns1:keyNotFound');

expect(Logger.entries.warn.length).to.equal(2);
expect(Backend.created.length).to.equal(0);
Backend.reset();

expect(Logger.entries.warn.length).to.equal(1);
expect(Logger.entries.warn[0]).to.equal(
'i18next::translator: key "keyNotFound" for namespace "ns1" won\'t get resolved as namespace was not yet loaded',
'i18next::backendConnector: did not save key "keyNotFound" for namespace "ns1" as the namespace was not yet loaded',
);
Logger.reset();
});
Expand Down

0 comments on commit 8b16945

Please sign in to comment.