Skip to content

Commit

Permalink
User preferred selected language is not retained during redirection o…
Browse files Browse the repository at this point in the history
…r when user navigates back post perform action suggested on the popup

Signed-off-by: Sreang Rathanak <[email protected]>
  • Loading branch information
Sreang Rathanak committed Jul 10, 2024
1 parent c2be525 commit 7d2eb68
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
7 changes: 5 additions & 2 deletions signup-ui/public/lang-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ window._env_.FALLBACK_LANG = window._env_.DEFAULT_LANG ?? "km";

// Check if the URLSearchParams has the key 'ui_locales'
// If it has, set the value of 'ui_locales' to the DEFAULT_LANG
if (urlSearchParams.has('ui_locales')) {
window._env_.DEFAULT_LANG = urlSearchParams.get('ui_locales').split('-')[0];
if (urlSearchParams.has("ui_locales")) {
window._env_.DEFAULT_LANG = urlSearchParams.get("ui_locales").split("-")[0];
} else if (!!localStorage.getItem("esignet-signup-language")) {
console.log("I am in");
window._env_.DEFAULT_LANG = localStorage.getItem("esignet-signup-language");
}
25 changes: 2 additions & 23 deletions signup-ui/src/components/language.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useTranslation } from "react-i18next";

import { ReactComponent as TranslationIcon } from "~assets/svg/translation-icon.svg";
import { cn } from "~utils/cn";
import { replaceUILocales } from "~utils/link";
import {
langFontMappingSelector,
languages2LettersSelector,
Expand All @@ -30,29 +31,7 @@ export const Language = () => {
const handleLanguageChange = (language: string) => {
i18n.changeLanguage(language);

// Get the encoded string from the URL
const hashCode = window.location.hash.substring(1);

// Decode the string
const decodedBase64 = atob(hashCode);

var urlSearchParams = new URLSearchParams(decodedBase64);

// Convert the decoded string to JSON
var jsonObject: Record<string, string> = {};
urlSearchParams.forEach(function (value, key) {
jsonObject[key] = value;
// Assign the current i18n language to the ui_locales
if (key === ui_locales) {
jsonObject[key] = language;
}
});

// Convert the JSON back to decoded string
Object.entries(jsonObject).forEach(([key, value]) => {
urlSearchParams.set(key, value);
});

const urlSearchParams = replaceUILocales(window.location.hash, language);
// Encode the string
const encodedBase64 = btoa(urlSearchParams.toString());
const url =
Expand Down
41 changes: 35 additions & 6 deletions signup-ui/src/utils/link.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { Buffer } from "buffer";

export const getSignInRedirectURL = (redirectUrl: string | undefined, hash: string, defaultPath: string): string => {
export const getSignInRedirectURL = (
redirectUrl: string | undefined,
hash: string,
defaultPath: string
): string => {
const locale = localStorage.getItem("esignet-signup-language");
if (!!hash) {
const signInQueryParams = Buffer.from(hash ?? "", "base64")?.toString();
return redirectUrl + "?" + signInQueryParams;
const signInQueryParams = replaceUILocales(hash, locale);
return redirectUrl + "?" + signInQueryParams.toString();
}
return defaultPath + "?ui_locales=" + locale;
};

export const replaceUILocales = (
hash: string,
locale: string | null
): URLSearchParams => {
// Convert the decoded string to JSON
const decodedBase64 = atob(hash.substring(1));

var urlSearchParams = new URLSearchParams(decodedBase64);

// Convert the decoded string to JSON
var jsonObject: Record<string, string> = {};
urlSearchParams.forEach(function (value, key) {
jsonObject[key] = value;
// Assign the current i18n language to the ui_locales
if (key === "ui_locales") {
jsonObject[key] = locale || value;
}
});

// Convert the JSON back to decoded string
Object.entries(jsonObject).forEach(([key, value]) => {
urlSearchParams.set(key, value);
});

return defaultPath;
return urlSearchParams;
};

0 comments on commit 7d2eb68

Please sign in to comment.