Skip to content

Commit

Permalink
chore: fix language not correct when redirect from deriv-com (binary-…
Browse files Browse the repository at this point in the history
…com#9632)

* chore: fix language not correct when redirect from deriv-com

* chore: fix language redirection from deriv-com

* chore: add optional chaining

* chore: update code base on comments
  • Loading branch information
suisin-deriv committed Sep 7, 2023
1 parent efbc2f5 commit ac29aba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 7 additions & 6 deletions packages/core/src/Stores/client-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { RudderStack } from '@deriv/analytics';
import { WS, requestLogout } from 'Services';
import { action, computed, makeObservable, observable, reaction, runInAction, toJS, when } from 'mobx';
import { getAccountTitle, getClientAccountType, getAvailableAccount } from './Helpers/client';
import { getLanguage, localize } from '@deriv/translations';
import { getLanguage, localize, getRedirectionLanguage } from '@deriv/translations';
import { getRegion, isEuCountry, isMultipliersOnly, isOptionsBlocked } from '_common/utility';

import BaseStore from './base-store';
Expand All @@ -40,7 +40,6 @@ import moment from 'moment';
import { setDeviceDataCookie } from './Helpers/device';

const LANGUAGE_KEY = 'i18n_language';
const DEFAULT_LANGUAGE = 'EN';
const storage_key = 'client.accounts';
const store_name = 'client_store';
const eu_shortcode_regex = new RegExp('^(maltainvest|malta|iom)$');
Expand Down Expand Up @@ -430,9 +429,11 @@ export default class ClientStore extends BaseStore {
reaction(
() => [this.account_settings],
() => {
const lang_from_url = new URLSearchParams(window.location.search).get('lang') || DEFAULT_LANGUAGE;
this.setPreferredLanguage(lang_from_url);
LocalStore.set(LANGUAGE_KEY, lang_from_url);
const language = getRedirectionLanguage(this.account_settings?.preferred_language);
window.history.replaceState({}, document.title, urlForLanguage(language));

this.setPreferredLanguage(language);
LocalStore.set(LANGUAGE_KEY, language);
}
);

Expand Down Expand Up @@ -1688,7 +1689,7 @@ export default class ClientStore extends BaseStore {
runInAction(() => {
this.is_populating_account_list = false;
});
const language = authorize_response.authorize.preferred_language;
const language = getRedirectionLanguage(authorize_response.authorize.preferred_language);
const stored_language = LocalStore.get(LANGUAGE_KEY);
if (language !== 'EN' && stored_language && language !== stored_language) {
window.history.replaceState({}, document.title, urlForLanguage(language));
Expand Down
6 changes: 6 additions & 0 deletions packages/translations/src/i18next/i18next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ const isLanguageAvailable = (lang: string) => {
return Object.keys(getAllowedLanguages()).includes(selected_language);
};

export const getRedirectionLanguage = (preferred_language: string) => {
const language_query = new URLSearchParams(window.location.search).get('lang');
const is_language_query_valid = language_query && isLanguageAvailable(language_query);
return is_language_query_valid ? language_query : preferred_language ?? DEFAULT_LANGUAGE;
};

export const getAllLanguages = () => ALL_LANGUAGES;

export const getInitialLanguage = () => {
Expand Down

0 comments on commit ac29aba

Please sign in to comment.