| import type { DomainLocale, I18NConfig } from '../config-shared' |
| import { getRequestMeta, type NextIncomingMessage } from '../request-meta' |
|
|
| |
| |
| |
| export interface LocaleAnalysisResult { |
| |
| |
| |
| pathname: string |
|
|
| |
| |
| |
| detectedLocale?: string |
|
|
| |
| |
| |
| inferredFromDefault: boolean |
| } |
|
|
| type LocaleAnalysisOptions = { |
| |
| |
| |
| |
| defaultLocale?: string |
| } |
|
|
| |
| |
| |
| |
| |
| export class I18NProvider { |
| private readonly lowerCaseLocales: ReadonlyArray<string> |
| private readonly lowerCaseDomains?: ReadonlyArray< |
| DomainLocale & { |
| |
| |
| |
| hostname: string |
| } |
| > |
|
|
| constructor(public readonly config: Readonly<I18NConfig>) { |
| if (!config.locales.length) { |
| throw new Error('Invariant: No locales provided') |
| } |
|
|
| this.lowerCaseLocales = config.locales.map((locale) => locale.toLowerCase()) |
| this.lowerCaseDomains = config.domains?.map((domainLocale) => { |
| const domain = domainLocale.domain.toLowerCase() |
| return { |
| defaultLocale: domainLocale.defaultLocale.toLowerCase(), |
| hostname: domain.split(':', 1)[0], |
| domain, |
| locales: domainLocale.locales?.map((locale) => locale.toLowerCase()), |
| http: domainLocale.http, |
| } |
| }) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public detectDomainLocale( |
| hostname?: string, |
| detectedLocale?: string |
| ): DomainLocale | undefined { |
| if (!hostname || !this.lowerCaseDomains || !this.config.domains) return |
|
|
| if (detectedLocale) detectedLocale = detectedLocale.toLowerCase() |
|
|
| for (let i = 0; i < this.lowerCaseDomains.length; i++) { |
| const domainLocale = this.lowerCaseDomains[i] |
| if ( |
| |
| domainLocale.hostname === hostname || |
| |
| |
| domainLocale.locales?.some((locale) => locale === detectedLocale) |
| ) { |
| return this.config.domains[i] |
| } |
| } |
|
|
| return |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public fromRequest( |
| req: NextIncomingMessage, |
| pathname: string |
| ): LocaleAnalysisResult { |
| const detectedLocale = getRequestMeta(req, 'locale') |
|
|
| |
| |
| if (detectedLocale) { |
| const analysis = this.analyze(pathname) |
|
|
| |
| |
| if (analysis.detectedLocale) { |
| if (analysis.detectedLocale !== detectedLocale) { |
| throw new Error( |
| `Invariant: The detected locale does not match the locale in the query. Expected to find '${detectedLocale}' in '${pathname}' but found '${analysis.detectedLocale}'}` |
| ) |
| } |
|
|
| pathname = analysis.pathname |
| } |
| } |
|
|
| return { |
| pathname, |
| detectedLocale, |
| inferredFromDefault: |
| getRequestMeta(req, 'localeInferredFromDefault') ?? false, |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public analyze( |
| pathname: string, |
| options: LocaleAnalysisOptions = {} |
| ): LocaleAnalysisResult { |
| let detectedLocale: string | undefined = options.defaultLocale |
|
|
| |
| |
| let inferredFromDefault = typeof detectedLocale === 'string' |
|
|
| |
| |
| const segments = pathname.split('/', 2) |
| if (!segments[1]) |
| return { |
| detectedLocale, |
| pathname, |
| inferredFromDefault, |
| } |
|
|
| |
| const segment = segments[1].toLowerCase() |
|
|
| |
| |
| const index = this.lowerCaseLocales.indexOf(segment) |
| if (index < 0) |
| return { |
| detectedLocale, |
| pathname, |
| inferredFromDefault, |
| } |
|
|
| |
| detectedLocale = this.config.locales[index] |
| inferredFromDefault = false |
|
|
| |
| pathname = pathname.slice(detectedLocale.length + 1) || '/' |
|
|
| return { |
| detectedLocale, |
| pathname, |
| inferredFromDefault, |
| } |
| } |
| } |
|
|