MCPcopy
hub / github.com/github/docs / LanguagePicker

Function LanguagePicker

components/page-header/LanguagePicker.tsx:39–89  ·  view source on GitHub ↗
({ mediumOrLower }: Props)

Source from the content-addressed store, hash-verified

37}
38
39export const LanguagePicker = ({ mediumOrLower }: Props) => {
40 const router = useRouter()
41 const { languages } = useLanguages()
42
43 const locale = router.locale || 'en'
44
45 const { t } = useTranslation('picker')
46 // Remember, in this context `languages` is only the active ones
47 // that are available. I.e. no wip ones.
48 // Also, if the current context has a page and that page has own ideas
49 // about which languages it's available in (e.g. early-access)
50 // it would already have been paired down.
51 const langs = Object.values(languages)
52
53 if (langs.length < 2) {
54 return null
55 }
56
57 const selectedLang = languages[locale]
58
59 // The `router.asPath` will always be without a hash in SSR
60 // So to avoid a hydraration failure on the client, we have to
61 // normalize it to be without the hash. That way the path is treated
62 // in a "denormalized" way.
63 const routerPath = router.asPath.split('#')[0]
64
65 return (
66 <div data-testid="language-picker">
67 <Picker
68 defaultText={t('language_picker_default_text')}
69 items={langs.map((lang) => ({
70 text: lang.nativeName || lang.name,
71 selected: lang === selectedLang,
72 href: `/${lang.code}${routerPath}`,
73 extra: {
74 locale: lang.code,
75 },
76 }))}
77 pickerLabel={mediumOrLower ? 'Language' : ''}
78 iconButton={mediumOrLower ? undefined : GlobeIcon}
79 onSelect={(item) => {
80 if (item.extra?.locale) rememberPreferredLanguage(item.extra.locale)
81 }}
82 buttonBorder={mediumOrLower}
83 dataTestId="default-language"
84 ariaLabel={`Select language: current language is ${selectedLang.name}`}
85 alignment={mediumOrLower ? 'start' : 'end'}
86 />
87 </div>
88 )
89}

Callers

nothing calls this directly

Calls 3

useLanguagesFunction · 0.90
useTranslationFunction · 0.90

Tested by

no test coverage detected