Skip to content

Commit

Permalink
add isInitializing property #2141
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Feb 22, 2024
1 parent f66289e commit a19c141
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 23.10.0

- Add 'isInitializing' property so we're able to detect init() was already called [2141](https://github.com/i18next/i18next/issues/2141)

## 23.9.0

- types: support nested keys in `InterpolationMap` [2140](https://github.com/i18next/i18next/pull/2140) fixes [2014](https://github.com/i18next/i18next/issues/2014)
Expand Down
2 changes: 2 additions & 0 deletions i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,7 @@
var _this = this;
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
let callback = arguments.length > 1 ? arguments[1] : undefined;
this.isInitializing = true;
if (typeof options === 'function') {
callback = options;
options = {};
Expand Down Expand Up @@ -2033,6 +2034,7 @@
const deferred = defer();
const load = () => {
const finish = (err, t) => {
this.isInitializing = false;
if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn('init: i18next is already initialized. You should call init just once!');
this.isInitialized = true;
if (!this.options.isClone) this.logger.log('initialized', this.options);
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ export interface i18n extends CustomInstanceExtensions {
*/
isInitialized: boolean;

/**
* Is initializing
*/
isInitializing: boolean;

/**
* Store was initialized
*/
Expand Down
2 changes: 2 additions & 0 deletions src/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class I18n extends EventEmitter {
}

init(options = {}, callback) {
this.isInitializing = true;
if (typeof options === 'function') {
callback = options;
options = {};
Expand Down Expand Up @@ -189,6 +190,7 @@ class I18n extends EventEmitter {

const load = () => {
const finish = (err, t) => {
this.isInitializing = false;
if (this.isInitialized && !this.initializedStoreOnce) this.logger.warn('init: i18next is already initialized. You should call init just once!');
this.isInitialized = true;
if (!this.options.isClone) this.logger.log('initialized', this.options);
Expand Down
8 changes: 7 additions & 1 deletion test/runtime/i18next.hasLoadedNamespace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ describe('i18next', () => {

describe('called init() not detecting lng', () => {
it('it should ok - but warn about issue', async () => {
await i18n.init({ debug: true, saveMissing: true });
expect(i18n.isInitialized).toBeFalsy();
expect(i18n.isInitializing).toBeFalsy();
const prom = i18n.init({ debug: true, saveMissing: true });
expect(i18n.isInitializing).toBeTruthy();
expect(i18n.isInitialized).toBeFalsy();
await prom;
expect(i18n.isInitializing).toBeFalsy();
expect(i18n.isInitialized).toBeTruthy();

expect(i18n.hasLoadedNamespace('translation')).to.equal(false);
Expand Down

0 comments on commit a19c141

Please sign in to comment.