2020-10-14 11:56:58 +02:00
|
|
|
export function detectDomainLocale(
|
|
|
|
domainItems:
|
|
|
|
| Array<{
|
|
|
|
http?: boolean
|
|
|
|
domain: string
|
2020-10-27 09:45:39 +01:00
|
|
|
locales?: string[]
|
2020-10-14 11:56:58 +02:00
|
|
|
defaultLocale: string
|
|
|
|
}>
|
|
|
|
| undefined,
|
2020-10-20 12:23:11 +02:00
|
|
|
hostname?: string,
|
2020-10-14 11:56:58 +02:00
|
|
|
detectedLocale?: string
|
|
|
|
) {
|
|
|
|
let domainItem:
|
|
|
|
| {
|
|
|
|
http?: boolean
|
|
|
|
domain: string
|
2020-10-27 09:45:39 +01:00
|
|
|
locales?: string[]
|
2020-10-14 11:56:58 +02:00
|
|
|
defaultLocale: string
|
|
|
|
}
|
|
|
|
| undefined
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|