|
| 1 | +/** |
| 2 | + * Utility functions for language detection and handling |
| 3 | + */ |
| 4 | + |
| 5 | +function detectLanguageFromUserAgent(userAgent) { |
| 6 | + const langRegexes = [ |
| 7 | + /\b([a-z]{2}(-[A-Z]{2})?);/i, // matches patterns like "en;" or "en-US;" |
| 8 | + /\[([a-z]{2}(-[A-Z]{2})?)\]/i // matches patterns like "[en]" or "[en-US]" |
| 9 | + ]; |
| 10 | + |
| 11 | + const match = langRegexes.reduce((result, regex) => { |
| 12 | + if (result) return result; |
| 13 | + const matches = userAgent.match(regex); |
| 14 | + return matches && matches[1] ? matches[1] : null; |
| 15 | + }, null); |
| 16 | + |
| 17 | + return match; |
| 18 | +} |
| 19 | + |
| 20 | +function getPreferredLanguage(supportedLanguages = [], defaultLanguage = 'en') { |
| 21 | + if (typeof navigator === 'undefined') { |
| 22 | + return defaultLanguage; |
| 23 | + } |
| 24 | + |
| 25 | + const normalizeLanguage = (langCode) => langCode.toLowerCase().trim(); |
| 26 | + |
| 27 | + const normalizedSupported = supportedLanguages.map(normalizeLanguage); |
| 28 | + |
| 29 | + if (navigator.languages && navigator.languages.length) { |
| 30 | + const matchedLang = navigator.languages.find((browserLang) => { |
| 31 | + const normalizedBrowserLang = normalizeLanguage(browserLang); |
| 32 | + |
| 33 | + const hasExactMatch = |
| 34 | + normalizedSupported.findIndex( |
| 35 | + (lang) => lang === normalizedBrowserLang |
| 36 | + ) !== -1; |
| 37 | + |
| 38 | + if (hasExactMatch) { |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + const languageOnly = normalizedBrowserLang.split('-')[0]; |
| 43 | + const hasLanguageOnlyMatch = |
| 44 | + normalizedSupported.findIndex( |
| 45 | + (lang) => lang === languageOnly || lang.startsWith(`${languageOnly}-`) |
| 46 | + ) !== -1; |
| 47 | + |
| 48 | + return hasLanguageOnlyMatch; |
| 49 | + }); |
| 50 | + |
| 51 | + if (matchedLang) { |
| 52 | + const normalizedMatchedLang = normalizeLanguage(matchedLang); |
| 53 | + const exactMatchIndex = normalizedSupported.findIndex( |
| 54 | + (lang) => lang === normalizedMatchedLang |
| 55 | + ); |
| 56 | + |
| 57 | + if (exactMatchIndex !== -1) { |
| 58 | + return supportedLanguages[exactMatchIndex]; |
| 59 | + } |
| 60 | + |
| 61 | + const languageOnly = normalizedMatchedLang.split('-')[0]; |
| 62 | + const languageOnlyMatchIndex = normalizedSupported.findIndex( |
| 63 | + (lang) => lang === languageOnly || lang.startsWith(`${languageOnly}-`) |
| 64 | + ); |
| 65 | + |
| 66 | + if (languageOnlyMatchIndex !== -1) { |
| 67 | + return supportedLanguages[languageOnlyMatchIndex]; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (navigator.language) { |
| 73 | + const normalizedNavLang = normalizeLanguage(navigator.language); |
| 74 | + const exactMatchIndex = normalizedSupported.findIndex( |
| 75 | + (lang) => lang === normalizedNavLang |
| 76 | + ); |
| 77 | + |
| 78 | + if (exactMatchIndex !== -1) { |
| 79 | + return supportedLanguages[exactMatchIndex]; |
| 80 | + } |
| 81 | + |
| 82 | + const languageOnly = normalizedNavLang.split('-')[0]; |
| 83 | + const languageOnlyMatchIndex = normalizedSupported.findIndex( |
| 84 | + (lang) => lang === languageOnly || lang.startsWith(`${languageOnly}-`) |
| 85 | + ); |
| 86 | + |
| 87 | + if (languageOnlyMatchIndex !== -1) { |
| 88 | + return supportedLanguages[languageOnlyMatchIndex]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (navigator.userAgent) { |
| 93 | + const userAgentLang = detectLanguageFromUserAgent(navigator.userAgent); |
| 94 | + if ( |
| 95 | + userAgentLang && |
| 96 | + normalizedSupported.includes(normalizeLanguage(userAgentLang)) |
| 97 | + ) { |
| 98 | + const index = normalizedSupported.indexOf( |
| 99 | + normalizeLanguage(userAgentLang) |
| 100 | + ); |
| 101 | + return supportedLanguages[index]; |
| 102 | + } |
| 103 | + } |
| 104 | + return defaultLanguage; |
| 105 | +} |
| 106 | + |
| 107 | +export default getPreferredLanguage; |
0 commit comments