File size: 1,354 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { DomainLocale } from '../server/config'
import type { normalizeLocalePath as NormalizeFn } from './normalize-locale-path'
import type { detectDomainLocale as DetectFn } from './detect-domain-locale'
import { normalizePathTrailingSlash } from './normalize-trailing-slash'

const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || ''

export function getDomainLocale(
  path: string,
  locale?: string | false,
  locales?: readonly string[],
  domainLocales?: readonly DomainLocale[]
) {
  if (process.env.__NEXT_I18N_SUPPORT) {
    const normalizeLocalePath: typeof NormalizeFn = (
      require('./normalize-locale-path') as typeof import('./normalize-locale-path')
    ).normalizeLocalePath
    const detectDomainLocale: typeof DetectFn = (
      require('./detect-domain-locale') as typeof import('./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}${normalizePathTrailingSlash(
        `${basePath}${finalLocale}${path}`
      )}`
    }
    return false
  } else {
    return false
  }
}