2021-07-21 18:12:33 +02:00
|
|
|
import type { DomainLocale } from '../../../server/config-shared'
|
|
|
|
|
2020-10-14 11:56:58 +02:00
|
|
|
export function detectDomainLocale(
|
2021-07-21 18:12:33 +02:00
|
|
|
domainItems?: DomainLocale[],
|
2020-10-20 12:23:11 +02:00
|
|
|
hostname?: string,
|
2020-10-14 11:56:58 +02:00
|
|
|
detectedLocale?: string
|
|
|
|
) {
|
2021-07-21 18:12:33 +02:00
|
|
|
let domainItem: DomainLocale | undefined
|
2020-10-14 11:56:58 +02:00
|
|
|
|
|
|
|
if (domainItems) {
|
2020-10-27 09:45:39 +01:00
|
|
|
if (detectedLocale) {
|
|
|
|
detectedLocale = detectedLocale.toLowerCase()
|
|
|
|
}
|
|
|
|
|
2020-10-14 11:56:58 +02:00
|
|
|
for (const item of domainItems) {
|
|
|
|
// remove port if present
|
|
|
|
const domainHostname = item.domain?.split(':')[0].toLowerCase()
|
|
|
|
if (
|
|
|
|
hostname === domainHostname ||
|
2020-10-27 09:45:39 +01:00
|
|
|
detectedLocale === item.defaultLocale.toLowerCase() ||
|
|
|
|
item.locales?.some((locale) => locale.toLowerCase() === detectedLocale)
|
2020-10-14 11:56:58 +02:00
|
|
|
) {
|
|
|
|
domainItem = item
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return domainItem
|
|
|
|
}
|