-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Detect browser language to set default language for new users #3492
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
Merged
raclim
merged 6 commits into
processing:develop
from
deveshidwivedi:feat/auto-detect-language
Jun 4, 2025
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
04da737
feat: detect and use browser language as default for first-time users
deveshidwivedi 24279ee
Merge branch 'develop' into feat/auto-detect-language
deveshidwivedi e5dc04d
refactor: remove userAgent language detection
deveshidwivedi 7ad92ca
Merge branch 'feat/auto-detect-language' of https://github.com/devesh…
deveshidwivedi c92d2b8
Merge branch 'develop' into feat/auto-detect-language
raclim 11cb236
Merge branch 'develop' into feat/auto-detect-language
raclim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Utility functions for language detection and handling | ||
*/ | ||
|
||
function detectLanguageFromUserAgent(userAgent) { | ||
const langRegexes = [ | ||
/\b([a-z]{2}(-[A-Z]{2})?);/i, // matches patterns like "en;" or "en-US;" | ||
/\[([a-z]{2}(-[A-Z]{2})?)\]/i // matches patterns like "[en]" or "[en-US]" | ||
]; | ||
|
||
const match = langRegexes.reduce((result, regex) => { | ||
if (result) return result; | ||
const matches = userAgent.match(regex); | ||
return matches && matches[1] ? matches[1] : null; | ||
}, null); | ||
|
||
return match; | ||
} | ||
|
||
function getPreferredLanguage(supportedLanguages = [], defaultLanguage = 'en') { | ||
if (typeof navigator === 'undefined') { | ||
return defaultLanguage; | ||
} | ||
|
||
const normalizeLanguage = (langCode) => langCode.toLowerCase().trim(); | ||
|
||
const normalizedSupported = supportedLanguages.map(normalizeLanguage); | ||
|
||
if (navigator.languages && navigator.languages.length) { | ||
const matchedLang = navigator.languages.find((browserLang) => { | ||
const normalizedBrowserLang = normalizeLanguage(browserLang); | ||
|
||
const hasExactMatch = | ||
normalizedSupported.findIndex( | ||
(lang) => lang === normalizedBrowserLang | ||
) !== -1; | ||
|
||
if (hasExactMatch) { | ||
return true; | ||
} | ||
|
||
const languageOnly = normalizedBrowserLang.split('-')[0]; | ||
const hasLanguageOnlyMatch = | ||
normalizedSupported.findIndex( | ||
(lang) => lang === languageOnly || lang.startsWith(`${languageOnly}-`) | ||
) !== -1; | ||
|
||
return hasLanguageOnlyMatch; | ||
}); | ||
|
||
if (matchedLang) { | ||
const normalizedMatchedLang = normalizeLanguage(matchedLang); | ||
const exactMatchIndex = normalizedSupported.findIndex( | ||
(lang) => lang === normalizedMatchedLang | ||
); | ||
|
||
if (exactMatchIndex !== -1) { | ||
return supportedLanguages[exactMatchIndex]; | ||
} | ||
|
||
const languageOnly = normalizedMatchedLang.split('-')[0]; | ||
const languageOnlyMatchIndex = normalizedSupported.findIndex( | ||
(lang) => lang === languageOnly || lang.startsWith(`${languageOnly}-`) | ||
); | ||
|
||
if (languageOnlyMatchIndex !== -1) { | ||
return supportedLanguages[languageOnlyMatchIndex]; | ||
} | ||
} | ||
} | ||
|
||
if (navigator.language) { | ||
const normalizedNavLang = normalizeLanguage(navigator.language); | ||
const exactMatchIndex = normalizedSupported.findIndex( | ||
(lang) => lang === normalizedNavLang | ||
); | ||
|
||
if (exactMatchIndex !== -1) { | ||
return supportedLanguages[exactMatchIndex]; | ||
} | ||
|
||
const languageOnly = normalizedNavLang.split('-')[0]; | ||
const languageOnlyMatchIndex = normalizedSupported.findIndex( | ||
(lang) => lang === languageOnly || lang.startsWith(`${languageOnly}-`) | ||
); | ||
|
||
if (languageOnlyMatchIndex !== -1) { | ||
return supportedLanguages[languageOnlyMatchIndex]; | ||
} | ||
} | ||
|
||
if (navigator.userAgent) { | ||
const userAgentLang = detectLanguageFromUserAgent(navigator.userAgent); | ||
if ( | ||
userAgentLang && | ||
normalizedSupported.includes(normalizeLanguage(userAgentLang)) | ||
) { | ||
const index = normalizedSupported.indexOf( | ||
normalizeLanguage(userAgentLang) | ||
); | ||
return supportedLanguages[index]; | ||
} | ||
} | ||
return defaultLanguage; | ||
} | ||
|
||
export default getPreferredLanguage; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.