rsnext/packages/next/client/get-domain-locale.ts
Javi Velasco daab64c27e
Extract router utils to common functions (#37313)
* Extract `detect-domain-locale` to a util file

* Remove `pathNoQueryHash` in favor of `parsePath`

* Remove `hasPathPrefix` in favor of `pathHasPrefix`

* Remove `addPathPrefix` in favor of an existing util

* Bugfix parsing pathname

* Refactor `addLocale`

* Extract `removeLocale`

* Extract `basePath` utils

* Dynamic imports for `getDomainLocale`
2022-05-30 20:19:37 +02:00

30 lines
1.1 KiB
TypeScript

import type { DomainLocale } from '../server/config'
import type { normalizeLocalePath as NormalizeFn } from './normalize-locale-path'
import type { detectDomainLocale as DetectFn } from './detect-domain-locale'
const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || ''
export function getDomainLocale(
path: string,
locale?: string | false,
locales?: string[],
domainLocales?: DomainLocale[]
) {
if (process.env.__NEXT_I18N_SUPPORT) {
const normalizeLocalePath: typeof NormalizeFn =
require('./normalize-locale-path').normalizeLocalePath
const detectDomainLocale: typeof DetectFn =
require('./detect-domain-locale').detectDomainLocale
const target = locale || normalizeLocalePath(path, locales).detectedLocale
const domain = detectDomainLocale(domainLocales, undefined, target)
if (domain) {
const proto = `http${domain.http ? '' : 's'}://`
const finalLocale = target === domain.defaultLocale ? '' : `/${target}`
return `${proto}${domain.domain}${basePath}${finalLocale}${path}`
}
return false
} else {
return false
}
}