| 9 | import { USER_LANGUAGE_COOKIE_NAME } from '../../lib/constants.js' |
| 10 | |
| 11 | function 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 | |
| 35 | type Props = { |
| 36 | mediumOrLower?: boolean |