File size: 5,984 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { DomainLocale, I18NConfig } from '../config-shared'
import { getRequestMeta, type NextIncomingMessage } from '../request-meta'

/**
 * The result of matching a locale aware route.
 */
export interface LocaleAnalysisResult {
  /**
   * The pathname without the locale prefix (if any).
   */
  pathname: string

  /**
   * The detected locale. If no locale was detected, this will be `undefined`.
   */
  detectedLocale?: string

  /**
   * True if the locale was inferred from the default locale.
   */
  inferredFromDefault: boolean
}

type LocaleAnalysisOptions = {
  /**
   * When provided, it will be used as the default locale if the locale
   * cannot be inferred from the pathname.
   */
  defaultLocale?: string
}

/**
 * The I18NProvider is used to match locale aware routes, detect the locale from
 * the pathname and hostname and normalize the pathname by removing the locale
 * prefix.
 */
export class I18NProvider {
  private readonly lowerCaseLocales: ReadonlyArray<string>
  private readonly lowerCaseDomains?: ReadonlyArray<
    DomainLocale & {
      // The configuration references a domain with an optional port, but the
      // hostname is always the domain without the port and is used for
      // matching.
      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,
      }
    })
  }

  /**
   * Detects the domain locale from the hostname and the detected locale if
   * provided.
   *
   * @param hostname The hostname to detect the domain locale from, this must be lowercased.
   * @param detectedLocale The detected locale to use if the hostname does not match.
   * @returns The domain locale if found, `undefined` otherwise.
   */
  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 (
        // We assume that the hostname is already lowercased.
        domainLocale.hostname === hostname ||
        // Configuration validation ensures that the locale is not repeated in
        // other domains locales.
        domainLocale.locales?.some((locale) => locale === detectedLocale)
      ) {
        return this.config.domains[i]
      }
    }

    return
  }

  /**
   * Pulls the pre-computed locale and inference results from the query
   * object.
   *
   * @param req the request object
   * @param pathname the pathname that could contain a locale prefix
   * @returns the locale analysis result
   */
  public fromRequest(
    req: NextIncomingMessage,
    pathname: string
  ): LocaleAnalysisResult {
    const detectedLocale = getRequestMeta(req, 'locale')

    // If a locale was detected on the query, analyze the pathname to ensure
    // that the locale matches.
    if (detectedLocale) {
      const analysis = this.analyze(pathname)

      // If the analysis contained a locale we should validate it against the
      // query and strip it from the 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,
    }
  }

  /**
   * Analyzes the pathname for a locale and returns the pathname without it.
   *
   * @param pathname The pathname that could contain a locale prefix.
   * @param options The options to use when matching the locale.
   * @returns The matched locale and the pathname without the locale prefix
   *          (if any).
   */
  public analyze(
    pathname: string,
    options: LocaleAnalysisOptions = {}
  ): LocaleAnalysisResult {
    let detectedLocale: string | undefined = options.defaultLocale

    // By default, we assume that the default locale was inferred if there was
    // no detected locale.
    let inferredFromDefault = typeof detectedLocale === 'string'

    // The first segment will be empty, because it has a leading `/`. If
    // there is no further segment, there is no locale (or it's the default).
    const segments = pathname.split('/', 2)
    if (!segments[1])
      return {
        detectedLocale,
        pathname,
        inferredFromDefault,
      }

    // The second segment will contain the locale part if any.
    const segment = segments[1].toLowerCase()

    // See if the segment matches one of the locales. If it doesn't, there is
    // no locale (or it's the default).
    const index = this.lowerCaseLocales.indexOf(segment)
    if (index < 0)
      return {
        detectedLocale,
        pathname,
        inferredFromDefault,
      }

    // Return the case-sensitive locale.
    detectedLocale = this.config.locales[index]
    inferredFromDefault = false

    // Remove the `/${locale}` part of the pathname.
    pathname = pathname.slice(detectedLocale.length + 1) || '/'

    return {
      detectedLocale,
      pathname,
      inferredFromDefault,
    }
  }
}