Skip to content

Multiple changeLanguage call may result in wrong order #1605

@waivital

Description

@waivital

🐛 Bug Report

When call changeLanguage multiple times, if loadResources inside the changeLanguage resolved in different order, then the final language will be the last resolved changeLanguage call, instead of the last changeLanguage call.

To Reproduce

    describe('changeLanguage', () => {
      it('it should keep changeLanguage call in order', () => {
        const instance = i18next.createInstance();
        const makeLang = (lan, delay) => ({
          delay,
          value: {
            foo: `${lan}`,
          },
        });
        const langMap = {
          en: makeLang('en', 100),
          zh: makeLang('zh', 500),
          es: makeLang('es', 300),
          jp: makeLang('jp', 200),
        };

        return new Promise(resolve => {
          instance
            .use({
              type: 'backend',
              read: function(language, namespace, callback) {
                const lang = langMap[language];

                setTimeout(() => {
                  callback(null, lang.value);
                }, lang.delay);
              },
            })
            .init(
              {
                fallbackLng: 'en',
              },
              (err, t) => {
                resolve(
                  Promise.all([
                    instance.changeLanguage('en'),
                    instance.changeLanguage('zh'),
                    instance.changeLanguage('jp'),
                    instance.changeLanguage('es'),
                  ]).then(() => {
                    expect(t('foo')).to.equal('es');
                  }),
                );
              },
            );
        });
      });
    });

Expected behavior

Test not fail. The value of t('foo') is es

Your Environment

  • runtime version: node v14.16.0
  • i18next version: 20.2.1
  • os: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions