Skip to content

Commit 3115c50

Browse files
author
Mikhail Bashkirov
committed
fix(localize): don't fire localeChanged event if set to the same locale
1 parent 5452ae6 commit 3115c50

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

packages/localize/src/LocalizeManager.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ export class LocalizeManager extends LionSingleton {
181181
}
182182

183183
_onLocaleChanged(newLocale, oldLocale) {
184+
if (newLocale === oldLocale) {
185+
return;
186+
}
184187
this.dispatchEvent(new CustomEvent('localeChanged', { detail: { newLocale, oldLocale } }));
185188
if (this._autoLoadOnLocaleChange) {
186189
this._loadAllMissing(newLocale, oldLocale);

packages/localize/test/LocalizeManager.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, oneEvent } from '@open-wc/testing';
2+
import sinon from 'sinon';
23
import { fetchMock } from '@bundled-es-modules/fetch-mock';
34
import { setupFakeImport, resetFakeImport, fakeImport } from './test-utils.js';
45

@@ -48,6 +49,15 @@ describe('LocalizeManager', () => {
4849
expect(event.detail.oldLocale).to.equal('en-GB');
4950
});
5051

52+
it('does not fire "localeChanged" event if it was set to the same locale', () => {
53+
const manager = new LocalizeManager();
54+
const eventSpy = sinon.spy();
55+
manager.addEventListener('localeChanged', eventSpy);
56+
manager.locale = 'en-US';
57+
manager.locale = 'en-US';
58+
expect(eventSpy.callCount).to.equal(1);
59+
});
60+
5161
describe('addData()', () => {
5262
it('allows to provide inline data', () => {
5363
const manager = new LocalizeManager();

0 commit comments

Comments
 (0)