-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathuseLanguageFromURL.ts
More file actions
79 lines (72 loc) · 2.89 KB
/
Copy pathuseLanguageFromURL.ts
File metadata and controls
79 lines (72 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { useEffect } from 'react';
import { FILTERED_LANGUAGES } from '@/utils/languages';
import { useTranslations } from '@deriv-com/translations';
/**
* Custom hook to handle language switching from URL parameter
*
* This hook:
* 1. Reads 'lang' parameter from URL
* 2. Falls back to localStorage if no URL parameter
* 3. Validates against supported languages
* 4. Switches to the language and removes the parameter from URL
* 5. Defaults to 'EN' for unsupported languages
*
* @example
* ```tsx
* // In your component
* useLanguageFromURL();
*
* // URL: ?lang=es - switches to Spanish
* // URL: ?lang=invalid - defaults to English
* ```
*/
export const useLanguageFromURL = () => {
const { switchLanguage } = useTranslations();
useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
let langParam = urlParams.get('lang');
// If no URL param, check localStorage
if (!langParam) {
const storedLang = localStorage.getItem('i18n_language');
if (storedLang) {
try {
// Try to parse as JSON first (in case it's stored as JSON string)
langParam = JSON.parse(storedLang);
} catch {
// If parsing fails, use the raw value
langParam = storedLang;
}
}
}
if (langParam) {
// Convert to uppercase to match our language codes
const langCodeCandidate = langParam.toUpperCase();
// Use FILTERED_LANGUAGES to check supported languages
const supportedLangCodes = FILTERED_LANGUAGES.map(lang => lang.code);
// Redirect any unsupported language to EN (English)
if (!supportedLangCodes.includes(langCodeCandidate)) {
try {
switchLanguage('EN');
// Remove lang parameter after processing to avoid URL pollution
const url = new URL(window.location.href);
url.searchParams.delete('lang');
window.history.replaceState({}, '', url.toString());
} catch (error) {
console.error('Failed to switch language:', error);
}
return;
}
// If language is supported, switch to it
const langCode = langCodeCandidate as (typeof FILTERED_LANGUAGES)[number]['code'];
try {
switchLanguage(langCode);
// Remove lang parameter after processing to avoid URL pollution
const url = new URL(window.location.href);
url.searchParams.delete('lang');
window.history.replaceState({}, '', url.toString());
} catch (error) {
console.error('Failed to switch language:', error);
}
}
}, [switchLanguage]);
};