Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(map): preserve language on refresh or app change #345

Merged
merged 2 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/earth-map/src/auth/auth0.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ export const Auth0Provider = ({
*/
const login = (options = {}) => {
SessionStorage.remove('ephemeral');
SessionStorage.remove('lang');

return client.loginWithRedirect(options);
};

const logout = (options = {}) => {
SessionStorage.remove('ephemeral');
SessionStorage.remove('lang');

// force the user to log out of their identity provider;
return client.logout({ ...options, federated: true });
};
Expand Down
9 changes: 7 additions & 2 deletions packages/earth-shared/src/components/user-menu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import compose from 'lodash/fp/compose';
import noop from 'lodash/noop';
import { getInitials } from '../../utils';

import { useDomWatcher, Elang } from '@marapp/earth-shared';
import { useDomWatcher, Elang, TranslationService } from '@marapp/earth-shared';

import './styles.scss';

Expand Down Expand Up @@ -77,6 +77,11 @@ export const UserMenu = (props: IProps) => {
i18n.changeLanguage(lang);
};

const handleLogout = (e) => {
const defaultLanguage = TranslationService.getDefaultLanguage();
changeLanguage(e, defaultLanguage);
};

const selectedLanguage = i18n.language;

return (
Expand Down Expand Up @@ -188,7 +193,7 @@ export const UserMenu = (props: IProps) => {
</li>

<li className="marapp-qa-signout">
<a onClick={compose(onLogout, toggleDrop)}>{t('Sign Out')}</a>
<a onClick={compose(onLogout, handleLogout, toggleDrop)}>{t('Sign Out')}</a>
</li>
</>
) : (
Expand Down
9 changes: 9 additions & 0 deletions packages/earth-shared/src/translations/TranslationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ import * as weglot from './weglot';

interface ITranslationService {
init(): void;
getDefaultLanguage(): string;
}

class TranslationService implements ITranslationService {
constructor() {}

getDefaultLanguage() {
const {
fallbackLng: [defaultLanguage],
} = i18n.options;

return defaultLanguage || Elang.EN;
}

init(weglotApiKey) {
const lang = SessionStorage.get('lang') || Elang.EN;

Expand Down