import { pick as acceptLanguagePick } from 'accept-language-parser' ; import Cookies from 'universal-cookie' ; import { LOCALE_COOKIE_KEY } from '../constants' ; import { I18nConfig } from '../types' ; import getDetectedLocaleFromPathname from './getDetectedLocaleFromPathname' ; import replaceBasename from './replaceBasename' ; interface UrlObject { pathname : string ; search : string ; } export default function getLocaleData ( { url , i18nConfig , headers , basename , } ) { const pathname = url . pathname || '/' ; const detectedLocale = getDetectedLocale ( { pathname , headers , i18nConfig , basename } ) ; const redirectUrl = getRedirectUrl ( pathname , { ... i18nConfig , detectedLocale } , basename ) ; return { detectedLocale , redirectUrl , } ; } function getDetectedLocale ( { pathname , i18nConfig , headers = { } , basename , } ) { let cookies ; if ( typeof window === 'undefined' ) { cookies = ( new Cookies ( headers . cookie ) ) . getAll ( ) ; } else { cookies = ( new Cookies ( ) ) . getAll ( ) ; } const { defaultLocale , locales , i18nRouting } = i18nConfig ; const detectedLocale = getLocaleFromCookie ( locales , cookies ) || getPreferredLocale ( locales , headers ) || ( i18nRouting === false ? undefined : getDetectedLocaleFromPathname ( { pathname , locales , basename , defaultLocale } ) ) || defaultLocale ; return detectedLocale ; } /**
 * 开启自动重定向选项时(redirect: true)时，获取下一个要跳转的 url
 * 仅在 pathname 为 `/` 或者 `/${basename}` 时重定向
 */ function getRedirectUrl ( pathname , i18nConfig , basename ? ) { const { redirect , defaultLocale , detectedLocale , i18nRouting } = i18nConfig ; const normalizedPathname = replaceBasename ( pathname , basename ) ; const isRootPath = normalizedPathname === '/' ; if ( redirect === true && i18nRouting !== false && isRootPath && defaultLocale !== detectedLocale ) { return `/${ detectedLocale }` ; } } function getLocaleFromCookie ( locales , cookies ) { const iceLocale = cookies [ LOCALE_COOKIE_KEY ] ; return locales . find ( locale => iceLocale === locale ) ; } function getPreferredLocale ( locales , headers ? ) { if ( typeof window === 'undefined' ) { const acceptLanguageValue = headers ?. [ 'accept-language' ] ; return acceptLanguagePick ( locales , acceptLanguageValue ) ; } else { const acceptLanguages = window . navigator . languages ; return acceptLanguages . find ( acceptLanguage => locales . includes ( acceptLanguage ) ) ; } }