( domains: I18nDomain[] | undefined, hostname: string | undefined, detectedLocale?: string )
| 20 | * Detects the domain locale based on hostname or detected locale |
| 21 | */ |
| 22 | export function detectDomainLocale( |
| 23 | domains: I18nDomain[] | undefined, |
| 24 | hostname: string | undefined, |
| 25 | detectedLocale?: string |
| 26 | ): I18nDomain | undefined { |
| 27 | if (!domains) return undefined |
| 28 | |
| 29 | const normalizedHostname = hostname?.toLowerCase() |
| 30 | const normalizedLocale = detectedLocale?.toLowerCase() |
| 31 | |
| 32 | for (const domain of domains) { |
| 33 | // Remove port if present |
| 34 | const domainHostname = domain.domain.split(':', 1)[0].toLowerCase() |
| 35 | |
| 36 | if ( |
| 37 | normalizedHostname === domainHostname || |
| 38 | normalizedLocale === domain.defaultLocale.toLowerCase() || |
| 39 | domain.locales?.some( |
| 40 | (locale) => locale.toLowerCase() === normalizedLocale |
| 41 | ) |
| 42 | ) { |
| 43 | return domain |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return undefined |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Normalizes a pathname by removing the locale prefix if present |
no test coverage detected