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

Added lang param in url (MN-447) #660

Merged
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
18 changes: 14 additions & 4 deletions src/components/SilentLang.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { ASYNC_STORAGE_LANG_KEY } from 'screens/Landing/LanguageSwitch';
import { useMonitoring } from '@monkvision/corejs';
import { allowedLanguage } from '../screens/Landing';

export default function SilentLang() {
const [hasBeenDone, setHasBeenDone] = useState(false);
Expand All @@ -11,12 +12,21 @@ export default function SilentLang() {

useEffect(() => {
if (!hasBeenDone) {
AsyncStorage.getItem(ASYNC_STORAGE_LANG_KEY)
.then((value) => (value !== null ? i18n.changeLanguage(value) : Promise.resolve()))
.then(() => setHasBeenDone(true))
.catch((err) => {
const urlParams = new URLSearchParams(window.location.search);
const language = urlParams.get('lang');

if (allowedLanguage.includes(language)) {
i18n.changeLanguage(language).catch((err) => {
errorHandler(err);
});
} else {
AsyncStorage.getItem(ASYNC_STORAGE_LANG_KEY)
.then((value) => (value !== null ? i18n.changeLanguage(value) : Promise.resolve()))
.then(() => setHasBeenDone(true))
.catch((err) => {
errorHandler(err);
});
}
}
}, []);

Expand Down
26 changes: 19 additions & 7 deletions src/screens/Landing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const ICON_BY_STATUS = {
ERROR: 'alert-octagon',
};

export const allowedLanguage = ['en', 'fr', 'de'];

export const debugParams = {
client: Clients.ALPHA,
inspectionId: '492241e6-5563-0a4d-4948-e39952452127',
Expand Down Expand Up @@ -69,18 +71,19 @@ export default function Landing() {
const isLastTour = route.params?.isLastTour ?? workflow !== Workflows.DEFAULT;

useEffect(() => {
let language = 'en';
if (USE_DEBUG_PARAMS && debugParams.lang) {
i18n.changeLanguage(debugParams.lang).catch((err) => {
errorHandler(err);
});
language = debugParams.lang;
} else if (info.preferredLanguage) {
i18n.changeLanguage(info.preferredLanguage).catch((err) => {
errorHandler(err);
});
language = info.preferredLanguage;
}

i18n.changeLanguage(language).catch((err) => {
errorHandler(err);
});
}, [info]);

const { clientId, inspectionId, token, vehicleTypeParam } = useMemo(() => {
const { clientId, inspectionId, token, vehicleTypeParam, language } = useMemo(() => {
if (USE_DEBUG_PARAMS) {
return {
clientId: debugParams.client,
Expand All @@ -101,9 +104,18 @@ export default function Landing() {
vehicleTypeParam: (clientParam === 'pd8') ? vehicleParam : vehicleParam ?? 1,
clientId: clientParam ? ClientParamMap[clientParam] : undefined,
token: compressedToken ? decompress(compressedToken) : undefined,
language: urlParams.get('lang'),
};
}, []);

useEffect(() => {
if (allowedLanguage.includes(language)) {
i18n.changeLanguage(language).catch((err) => {
errorHandler(err);
});
}
}, [language]);

useEffect(() => {
setClient(clientId);
}, [clientId]);
Expand Down
Loading