Skip to content

Commit

Permalink
Get locale from query params
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-asriyan committed Oct 19, 2024
1 parent c19bd2d commit fb2acfd
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ export const locales = {
const fallbackLocale = 'en';
const localStorageKey = 'locale';

const getLocaleFromQuery = function() {
const params = new URLSearchParams(location.search);
const lang = params.get('lang');
if (lang) {
params.delete('lang');
const newUrl = `${location.pathname}?${params.toString()}`;
history.replaceState(null, '', newUrl);
}
return lang;
};

const getLocale = function() {
const lang = localStorage.getItem(localStorageKey) || getLocaleFromNavigator();
const lang = getLocaleFromQuery() || localStorage.getItem(localStorageKey) || getLocaleFromNavigator();
if (locales[lang]) {
return lang;
}
Expand Down

0 comments on commit fb2acfd

Please sign in to comment.