Skip to content

Commit

Permalink
fix: Make language stable
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Mar 11, 2022
1 parent 7ccbdb3 commit 1085d75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 13 additions & 0 deletions packages/core/src/Properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ describe('Properties', () => {
expect(properties.currentLanguage).toEqual('cs');
});

test('language is stored locally and detected only once', () => {
const languageGetter = jest.spyOn(window.navigator, 'language', 'get');
languageGetter.mockReturnValue('cs');
Storage.prototype.getItem = jest.fn();
properties.config = new TolgeeConfig({
availableLanguages: ['cs', 'en'],
});
expect(properties.currentLanguage).toEqual('cs');
expect(properties.currentLanguage).toEqual('cs');

expect(languageGetter).toBeCalledTimes(1);
});

test('available languages derrived from staticData', () => {
const languageGetter = jest.spyOn(window.navigator, 'language', 'get');
languageGetter.mockReturnValue('cs');
Expand Down
15 changes: 11 additions & 4 deletions packages/core/src/Properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@ export class Properties {
return this._currentLanguage;
}

let result: string | undefined;

if (this.config.languageStore) {
const storedLanguage = this.getStoredLanguage();
if (storedLanguage) {
return storedLanguage;
result = storedLanguage;
}
}

if (this.config.languageDetect) {
if (this.config.languageDetect && !result) {
const detectedLanguage = this.getLanguageByNavigator();
if (detectedLanguage) {
return detectedLanguage;
result = detectedLanguage;
}
}

return this.config.defaultLanguage;
if (!result) {
result = this.config.defaultLanguage;
}

this._currentLanguage = result;
return result;
}

set currentLanguage(language: string) {
Expand Down

0 comments on commit 1085d75

Please sign in to comment.