MCPcopy
hub / github.com/github/docs / rememberPreferredLanguage

Function rememberPreferredLanguage

components/page-header/LanguagePicker.tsx:11–33  ·  view source on GitHub ↗
(value: string)

Source from the content-addressed store, hash-verified

9import { USER_LANGUAGE_COOKIE_NAME } from '../../lib/constants.js'
10
11function rememberPreferredLanguage(value: string) {
12 try {
13 // The reason we use a cookie and not local storage is because
14 // this cookie value is used and needed by the server. For
15 // example, when doing `GET /some/page` we need the cookie
16 // to redirect to `Location: /ja/some/page`.
17 // It's important it's *not* an HttpOnly cookie because we
18 // need this in the client-side which is used to determine
19 // the UI about displaying notifications about preferred
20 // language if your cookie doesn't match the current URL.
21 Cookies.set(USER_LANGUAGE_COOKIE_NAME, value, {
22 expires: 365,
23 secure: document.location.protocol !== 'http:',
24 })
25 } catch (err) {
26 // You can never be too careful because setting a cookie
27 // can fail. For example, some browser
28 // extensions disallow all setting of cookies and attempts
29 // at the `document.cookie` setter could throw. Just swallow
30 // and move on.
31 console.warn('Unable to set preferred language cookie', err)
32 }
33}
34
35type Props = {
36 mediumOrLower?: boolean

Callers 1

LanguagePickerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected