@@ -17,53 +17,48 @@ export const useLanguagePicker = (handleClose?: () => void) => {
17
17
const { t } = useTranslation ( "common" )
18
18
const locale = useLocale ( )
19
19
20
- const languages = useMemo < LocaleDisplayInfo [ ] > ( ( ) => {
21
- const locales = filterRealLocales ( LOCALES_CODES )
22
-
23
- // Get the preferred languages for the users browser
24
- const navLangs = typeof navigator !== "undefined" ? navigator . languages : [ ]
25
-
26
- // For each browser preference, reduce to the most specific match found in `locales` array
27
- const allBrowserLocales : Lang [ ] = navLangs
28
- . map (
29
- ( navLang ) =>
30
- locales ?. reduce ( ( acc , cur ) => {
31
- if ( cur . toLowerCase ( ) === navLang . toLowerCase ( ) ) return cur
32
- if (
33
- navLang . toLowerCase ( ) . startsWith ( cur . toLowerCase ( ) ) &&
34
- acc !== navLang
35
- )
36
- return cur
37
- return acc
38
- } , "" ) as Lang
39
- )
40
- . filter ( ( i ) => ! ! i ) // Remove those without matches
41
-
42
- // Remove duplicate matches
43
- const browserLocales = Array . from ( new Set ( allBrowserLocales ) )
44
-
45
- return (
20
+ // Get the preferred language for the users browser
21
+ const [ navLang ] = typeof navigator !== "undefined" ? navigator . languages : [ ]
22
+ const locales = useMemo ( ( ) => filterRealLocales ( LOCALES_CODES ) , [ ] )
23
+ const intlLocalePreference = useMemo (
24
+ ( ) =>
25
+ locales ?. reduce ( ( acc , cur ) => {
26
+ if ( cur . toLowerCase ( ) === navLang . toLowerCase ( ) ) return cur
27
+ if (
28
+ navLang . toLowerCase ( ) . startsWith ( cur . toLowerCase ( ) ) &&
29
+ acc !== navLang
30
+ )
31
+ return cur
32
+ return acc
33
+ } , "" ) as Lang ,
34
+ [ navLang , locales ]
35
+ )
36
+
37
+ const languages = useMemo < LocaleDisplayInfo [ ] > (
38
+ ( ) =>
46
39
( locales as Lang [ ] )
47
40
?. map ( ( localeOption ) => {
48
41
const displayInfo = localeToDisplayInfo (
49
42
localeOption ,
50
43
locale as Lang ,
51
44
t
52
45
)
53
- const isBrowserDefault = browserLocales . includes ( localeOption )
46
+ const isBrowserDefault = intlLocalePreference === localeOption
54
47
return { ...displayInfo , isBrowserDefault }
55
48
} )
56
49
. sort ( ( a , b ) => {
57
- const indexA = browserLocales . indexOf ( a . localeOption as Lang )
58
- const indexB = browserLocales . indexOf ( b . localeOption as Lang )
59
- if ( indexA >= 0 && indexB >= 0 ) return indexA - indexB
60
- if ( indexA >= 0 ) return - 1
61
- if ( indexB >= 0 ) return 1
62
-
63
- return a . sourceName . localeCompare ( b . sourceName )
64
- } ) || [ ]
65
- )
66
- } , [ locale , t ] )
50
+ // Always put the browser's preferred language first
51
+ if ( a . localeOption === intlLocalePreference ) return - 1
52
+ if ( b . localeOption === intlLocalePreference ) return 1
53
+ // Otherwise, sort by wordsApproved descending
54
+ return ( b . wordsApproved ?? 0 ) - ( a . wordsApproved ?? 0 )
55
+ } ) || [ ] ,
56
+ [ intlLocalePreference , locale , locales , t ]
57
+ )
58
+
59
+ const intlLanguagePreference = languages . find (
60
+ ( lang ) => lang . localeOption === intlLocalePreference
61
+ )
67
62
68
63
const { isOpen, setValue, ...menu } = useDisclosure ( )
69
64
@@ -100,5 +95,6 @@ export const useLanguagePicker = (handleClose?: () => void) => {
100
95
return {
101
96
disclosure : { isOpen, setValue, onOpen, onClose } ,
102
97
languages,
98
+ intlLanguagePreference,
103
99
}
104
100
}
0 commit comments