* upgrade primer/react * upgrade * using deprecated * remove lib" * Upgrade primer/react: Upgrade Label (#28502) update Label to primer/react 35.2.2 * fix merge conflicts * primer/react v35: update ActionList (#28467) * Update to v35 ActionList for LearningTrack * Update to v35 ActionList for ArticleList * Update to v35 ActionList for ProductArticleList * Update to v35 ActionList for TableOfContents * Update to v35 ActionList for ProductCollapsibleSection * Update to v35 ActionList for RestCollapsibleSection * Update to v35 ActionList for SidebarHomepage * Update to v35 ActionList for MiniTocs * Update to v35 ActionList for Search * Extra div for rendering test * One less div for rendering test * All the style updates for v35 ActionList * Works without setting as an li which is already the default (didn't before for some reason) * Use deprecated ItemInput for now * Picker update for primer/react (#28485) * update picker * inline picker for mobile * set width to auto * Update components/ui/Picker/Picker.tsx Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com> * update * Update Picker.tsx * update onselect * checking language code * move language cookie setting to language picker Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com> * Resolve package merge conflicts * fresh npm install * Primer update UnderlineNav (#28582) * update underlinenav for primer/react update * update tests * update switches test * update one last label * update header test" * remove href in underlinenav * update rendering tests * update cursor * primer/react v35: update DropDownMenu to ActionMenu (#28576) * Update to v35 ActionMenu for ArticleCards * Update to v35 ActionMenu for Search * Set button to inline-block * Put the props on the overlay * Update test for ActionMenu markup * update package * update package lock * primer/react v35: CodeLanguagePicker update from SelectMenu to ActionMenu (#28625) * Use octicon for menu down arrow * Update to v35 ActionMenu for CodeLanguagePicker * update to SubNav Co-authored-by: Grace Park <gracepark@github.com> * update package-lock Co-authored-by: Robert Sese <734194+rsese@users.noreply.github.com> Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import { useRouter } from 'next/router'
|
|
import Cookies from 'js-cookie'
|
|
|
|
import { useLanguages } from 'components/context/LanguagesContext'
|
|
import { Picker } from 'components/ui/Picker'
|
|
import { useTranslation } from 'components/hooks/useTranslation'
|
|
|
|
// This value is replicated in two places! See middleware/detect-language.js
|
|
const PREFERRED_LOCALE_COOKIE_NAME = 'preferredlang'
|
|
|
|
type Props = {
|
|
variant?: 'inline'
|
|
}
|
|
|
|
export const LanguagePicker = ({ variant }: Props) => {
|
|
const router = useRouter()
|
|
const { languages } = useLanguages()
|
|
const locale = router.locale || 'en'
|
|
const langs = Object.values(languages)
|
|
const selectedLang = languages[locale]
|
|
const { t } = useTranslation('picker')
|
|
|
|
// The `router.asPath` will always be without a hash in SSR
|
|
// So to avoid a hydraration failure on the client, we have to
|
|
// normalize it to be without the hash. That way the path is treated
|
|
// in a "denormalized" way.
|
|
const routerPath = router.asPath.split('#')[0]
|
|
|
|
function rememberPreferredLanguage(option: { locale: string }) {
|
|
try {
|
|
Cookies.set(PREFERRED_LOCALE_COOKIE_NAME, option.locale, {
|
|
expires: 365,
|
|
secure: document.location.protocol !== 'http:',
|
|
})
|
|
} catch (err) {
|
|
// You can never be too careful because setting a cookie
|
|
// can fail. For example, some browser
|
|
// extensions disallow all setting of cookies and attempts
|
|
// at the `document.cookie` setter could throw. Just swallow
|
|
// and move on.
|
|
console.warn('Unable to set preferred language cookie', err)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div data-testid="language-picker">
|
|
<Picker
|
|
variant={variant}
|
|
defaultText={t('language_picker_default_text')}
|
|
options={langs
|
|
.filter((lang) => !lang.wip)
|
|
.map((lang) => ({
|
|
text: lang.nativeName || lang.name,
|
|
selected: lang === selectedLang,
|
|
locale: lang.code,
|
|
href: `${routerPath}`,
|
|
onselect: rememberPreferredLanguage,
|
|
}))}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|