@i18n-micro/types 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,19 @@
1
+ /** BCP-47 locale code string (e.g. `'en'`, `'de-DE'`, `'zh-Hans'`). */
1
2
  export type LocaleCode = string;
3
+ /**
4
+ * Key registry for typed translation keys.
5
+ * Users (via `@i18n-micro/types-generator`) extend this interface through
6
+ * TypeScript module augmentation to get autocompletion for `$t()` calls.
7
+ */
2
8
  export interface DefineLocaleMessage {
3
9
  readonly __augmentation?: never;
4
10
  }
11
+ /**
12
+ * Resolved translation key type.
13
+ * - When `DefineLocaleMessage` is empty (generator not connected) — resolves to `string`.
14
+ * - When `DefineLocaleMessage` has keys — resolves to a union of those keys **and** `string`,
15
+ * providing autocompletion while still allowing dynamic keys.
16
+ */
5
17
  export type TranslationKey = keyof DefineLocaleMessage extends never ? string : keyof DefineLocaleMessage | string;
6
18
  /**
7
19
  * Helper for creating typed prefixes.
@@ -17,97 +29,386 @@ export type TranslationKey = keyof DefineLocaleMessage extends never ? string :
17
29
  */
18
30
  export type ScopedKey<Scope extends string> = Extract<TranslationKey, `${Scope}.${string}`>;
19
31
  export interface Locale {
32
+ /** BCP-47 locale code (e.g. `'en'`, `'de-DE'`, `'zh-Hans'`). Used in URLs and as file/key identifier. */
20
33
  code: LocaleCode;
34
+ /** When `true`, this locale is excluded from route generation and language switching. */
21
35
  disabled?: boolean;
36
+ /** ISO 639-1 / BCP-47 tag for `<html lang>` and `hreflang` SEO attributes (e.g. `'en-US'`). */
22
37
  iso?: string;
38
+ /** Text direction. Reflected in `<html dir>` when the locale is active. */
23
39
  dir?: 'ltr' | 'rtl' | 'auto';
40
+ /** Human-readable name shown in language switchers (e.g. `'English'`, `'Deutsch'`). */
24
41
  displayName?: string;
42
+ /**
43
+ * Per-locale base URL for SEO meta tags.
44
+ * Useful for multi-domain setups where each locale lives on its own domain
45
+ * (e.g. `'https://en.example.com'`).
46
+ */
25
47
  baseUrl?: string;
48
+ /**
49
+ * When `true`, this locale is treated as the "base default" for the domain
50
+ * specified in `baseUrl`. Affects canonical URL generation in multi-domain setups.
51
+ */
26
52
  baseDefault?: boolean;
53
+ /**
54
+ * Locale code to fall back to when a translation key is missing in this locale.
55
+ * Overrides the global `fallbackLocale` for this specific locale.
56
+ */
27
57
  fallbackLocale?: string;
58
+ /** Allows arbitrary extra properties for user-defined locale metadata. */
28
59
  [key: string]: unknown;
29
60
  }
61
+ /**
62
+ * Per-page i18n configuration declared via `defineI18nRoute()` in page components.
63
+ */
30
64
  export interface DefineI18nRouteConfig {
65
+ /**
66
+ * Restrict which locales this page is available in.
67
+ * - `string[]` — list of allowed locale codes.
68
+ * - `Record<LocaleCode, Translations>` — allowed locales with inline translation overrides.
69
+ */
31
70
  locales?: string[] | Record<LocaleCode, Translations>;
71
+ /** Per-locale custom route paths (e.g. `{ en: '/about', de: '/ueber-uns' }`). */
32
72
  localeRoutes?: Record<LocaleCode, string>;
73
+ /**
74
+ * Disable SEO meta tag generation for this page.
75
+ * - `true` — disable all meta tags.
76
+ * - `string[]` — disable only for the listed locale codes.
77
+ */
33
78
  disableMeta?: boolean | string[];
34
79
  }
80
+ /** Locale-specific route params for `$switchLocalePath` and `<i18n-link>`. */
35
81
  export type I18nRouteParams = Record<LocaleCode, Record<string, string>> | null;
82
+ /** Interpolation parameters passed to the `t()` function. */
36
83
  export type Params = Record<string, string | number | boolean>;
84
+ /** Getter function signature used by pluralization to resolve nested keys. */
37
85
  export type Getter = (key: TranslationKey, params?: Record<string, string | number | boolean>, defaultValue?: string) => unknown;
86
+ /**
87
+ * Custom pluralization function.
88
+ * Called when `tc()` / `$tc()` is used. Receives the key, count, params, locale,
89
+ * and a getter to fetch the singular/plural forms from translations.
90
+ */
38
91
  export type PluralFunc = (key: TranslationKey, count: number, params: Params, locale: string, getter: Getter) => string | null;
92
+ /**
93
+ * Global per-route locale configuration.
94
+ * - `Record<string, string>` — custom paths per locale for the route.
95
+ * - `false` — exclude the route from localization entirely.
96
+ * - `true` — include with default behavior.
97
+ */
39
98
  export type GlobalLocaleRoutes = Record<string, Record<LocaleCode, string> | false | boolean> | null | undefined;
99
+ /** URL routing strategy for locale handling. */
40
100
  export type Strategies = 'no_prefix' | 'prefix_except_default' | 'prefix' | 'prefix_and_default';
101
+ /**
102
+ * Callback invoked when a translation key is missing.
103
+ * Useful for logging, reporting, or providing dynamic fallbacks.
104
+ */
41
105
  export type MissingHandler = (locale: string, key: TranslationKey, routeName: string, instance?: unknown, type?: string) => void;
42
106
  export interface ModuleOptions {
107
+ /**
108
+ * List of supported locales.
109
+ * Each entry defines a locale code plus optional metadata (ISO, direction, display name, etc.).
110
+ * @default []
111
+ */
43
112
  locales?: Locale[];
113
+ /**
114
+ * Generate SEO meta tags (`hreflang`, `canonical`, `og:url`, `og:locale`) automatically.
115
+ * @default true
116
+ */
44
117
  meta?: boolean;
118
+ /**
119
+ * URL routing strategy for locale prefixes.
120
+ * - `'no_prefix'` — no locale in URL; locale stored in cookie.
121
+ * - `'prefix_except_default'` — prefix all locales except the default.
122
+ * - `'prefix'` — always prefix, including the default locale.
123
+ * - `'prefix_and_default'` — like `prefix`, but the default locale is also accessible without prefix.
124
+ * @default 'prefix_except_default'
125
+ */
45
126
  strategy?: Strategies;
127
+ /**
128
+ * Base URL for SEO meta tags (canonical, og:url, hreflang).
129
+ * - `undefined` — dynamically resolved from the current request URL
130
+ * (`useRequestURL().origin` on server, `window.location.origin` on client).
131
+ * Best for multi-domain deployments.
132
+ * - A concrete URL string (e.g. `'https://example.com'`) — used as-is.
133
+ * @default undefined
134
+ */
46
135
  metaBaseUrl?: string;
136
+ /**
137
+ * Trust the `X-Forwarded-Host` header when resolving the base URL for meta tags.
138
+ * Enable when the app runs behind a reverse proxy (nginx, Cloudflare, AWS ALB, etc.)
139
+ * that sets this header to the real client-facing hostname.
140
+ * @default true
141
+ */
142
+ metaTrustForwardedHost?: boolean;
143
+ /**
144
+ * Trust the `X-Forwarded-Proto` header when resolving the protocol for meta tags.
145
+ * Enable when the app runs behind a TLS-terminating proxy so that
146
+ * canonical URLs use `https://` even though the app itself listens on HTTP.
147
+ * @default true
148
+ */
149
+ metaTrustForwardedProto?: boolean;
150
+ /**
151
+ * Register the `defineI18nRoute()` macro plugin, enabling per-page `defineI18nRoute()` calls.
152
+ * @default true
153
+ */
47
154
  define?: boolean;
155
+ /**
156
+ * Enable automatic locale-based redirects.
157
+ * When `true`, visitors are redirected to their preferred locale (detected from cookie,
158
+ * `Accept-Language` header, or the default) on the first visit.
159
+ * @default true
160
+ */
48
161
  redirects?: boolean;
162
+ /**
163
+ * Register the core i18n plugin that provides `$t()`, `$tc()`, `$getLocale()`,
164
+ * `$switchLocale()`, and other runtime helpers.
165
+ * @default true
166
+ */
49
167
  plugin?: boolean;
168
+ /**
169
+ * Register the i18n hooks plugin that provides `i18n:register` and `i18n:beforeLocaleSwitch`
170
+ * / `i18n:afterLocaleSwitch` app-level hooks.
171
+ * @default true
172
+ */
50
173
  hooks?: boolean;
174
+ /**
175
+ * The locale to use when no locale can be determined from URL or user preferences.
176
+ * Also used as the fallback locale for missing translations when `fallbackLocale` is not set.
177
+ * @default 'en'
178
+ */
51
179
  defaultLocale?: string;
180
+ /**
181
+ * Base URL path segment for the translations API route (used in SSR/SSG data fetching).
182
+ * Can also be set via `NUXT_I18N_APP_BASE_URL` environment variable.
183
+ * @default '_locales'
184
+ */
52
185
  apiBaseUrl?: string;
186
+ /**
187
+ * Override the host used for client-side translation fetch requests.
188
+ * Useful when the client reaches the server via a different hostname than the one Nuxt sees.
189
+ * Can also be set via `NUXT_I18N_APP_BASE_CLIENT_HOST` environment variable.
190
+ * @default undefined
191
+ */
53
192
  apiBaseClientHost?: string;
193
+ /**
194
+ * Override the host used for server-side translation fetch requests.
195
+ * Useful in container/microservice setups where the server reaches itself via an internal hostname.
196
+ * Can also be set via `NUXT_I18N_APP_BASE_SERVER_HOST` environment variable.
197
+ * @default undefined
198
+ */
54
199
  apiBaseServerHost?: string;
200
+ /**
201
+ * Path to the directory containing translation JSON files, relative to the project root.
202
+ * @default 'locales'
203
+ */
55
204
  translationDir?: string;
205
+ /**
206
+ * Automatically detect the user's preferred language from the `Accept-Language` HTTP header.
207
+ * Used in combination with `autoDetectPath` to decide when detection occurs.
208
+ * @default true
209
+ */
56
210
  autoDetectLanguage?: boolean;
211
+ /**
212
+ * URL path on which automatic language detection and redirect occur.
213
+ * - `'/'` — detect only on the root path.
214
+ * - `'*'` — detect and redirect on every path (including locale-prefixed ones).
215
+ * @default '/'
216
+ */
57
217
  autoDetectPath?: string;
218
+ /**
219
+ * Disable the file watcher that auto-creates missing translation files in development mode.
220
+ * @default false
221
+ */
58
222
  disableWatcher?: boolean;
223
+ /**
224
+ * Generate TypeScript type declarations for `useI18n`, `$t`, and related helpers
225
+ * based on the translation keys in your default locale files.
226
+ * @default true
227
+ */
59
228
  types?: boolean;
229
+ /**
230
+ * Map route names to other route names to share the same translation files.
231
+ * For example, `{ 'about-us': 'about' }` means the `about-us` page will use
232
+ * translations from the `about` page instead of its own.
233
+ * @default {}
234
+ */
60
235
  routesLocaleLinks?: {
61
236
  [key: string]: string;
62
237
  };
238
+ /**
239
+ * Custom pluralization function or a path to a file exporting one.
240
+ * When a string path is provided, the file is imported at build time.
241
+ * The function receives `(key, count, params, locale, getter)` and should return
242
+ * the correct plural form as a string, or `null` to fall back to the built-in logic.
243
+ * @default built-in pluralization (singular/plural by count)
244
+ */
63
245
  plural?: string | PluralFunc;
246
+ /**
247
+ * Disable per-page translation files.
248
+ * When `true`, only global translations (`{locale}.json`) are loaded;
249
+ * page-specific files (`pages/{page}/{locale}.json`) are not generated or loaded.
250
+ * @default false
251
+ */
64
252
  disablePageLocales?: boolean;
253
+ /**
254
+ * Global fallback locale code.
255
+ * When a translation key is missing in the active locale, the module looks it up
256
+ * in this locale before returning the key itself.
257
+ * @default undefined (no fallback; returns the raw key)
258
+ */
65
259
  fallbackLocale?: string;
260
+ /**
261
+ * Cookie name for persisting the user's locale preference across sessions.
262
+ * Set to `null` to disable cookie-based persistence.
263
+ * Automatically set to `'user-locale'` for the `no_prefix` strategy if not provided.
264
+ * @default null
265
+ */
66
266
  localeCookie?: string | null;
267
+ /**
268
+ * Enable verbose debug logging for locale detection, route generation, and translation loading.
269
+ * @default false
270
+ */
67
271
  debug?: boolean;
272
+ /**
273
+ * Global route-level locale configuration.
274
+ * Allows restricting or customizing locale routes for specific pages without
275
+ * modifying their components.
276
+ * - `false` — exclude the route from localization.
277
+ * - `Record<LocaleCode, string>` — custom per-locale paths.
278
+ * @default {}
279
+ */
68
280
  globalLocaleRoutes?: GlobalLocaleRoutes;
281
+ /**
282
+ * Custom regular expression (or its string source) for matching locale codes in URL segments.
283
+ * All locale codes defined in `locales` must match this pattern, or a warning is emitted.
284
+ * @default undefined (uses built-in pattern based on locale codes)
285
+ */
69
286
  customRegexMatcher?: string | RegExp;
287
+ /**
288
+ * For `no_prefix` strategy: enable redirect from a locale-prefixed URL
289
+ * (e.g. `/en/about`) to the unprefixed version (`/about`).
290
+ * @default false
291
+ */
70
292
  noPrefixRedirect?: boolean;
293
+ /**
294
+ * List of query parameter names preserved in canonical and `og:url` meta tags.
295
+ * Parameters not in this list are stripped from the canonical URL.
296
+ * @default ['page', 'sort', 'filter', 'search', 'q', 'query', 'tag']
297
+ */
71
298
  canonicalQueryWhitelist?: string[];
299
+ /**
300
+ * URL patterns (strings or RegExp) to exclude from i18n processing entirely.
301
+ * Matching routes won't get locale prefixes, redirects, or translation loading.
302
+ * Internal Nuxt paths (`/__nuxt_error`, etc.) are always excluded automatically.
303
+ * @default undefined
304
+ */
72
305
  excludePatterns?: (string | RegExp)[];
306
+ /**
307
+ * Prefix prepended to localized route names (e.g. `'localized-index'`).
308
+ * Used internally to distinguish original routes from generated locale variants.
309
+ * @default 'localized-'
310
+ */
73
311
  localizedRouteNamePrefix?: string;
312
+ /**
313
+ * Per-route locale restrictions, extracted from `defineI18nRoute()` calls.
314
+ * Maps a route path (e.g. `'/about'`) to an array of allowed locale codes.
315
+ * Routes not listed have no restrictions (all locales allowed).
316
+ */
74
317
  routeLocales?: Record<string, string[]>;
318
+ /**
319
+ * Per-route meta tag disabling, extracted from `defineI18nRoute()` calls.
320
+ * Maps a route path to `true` (disable all meta) or an array of locale codes
321
+ * for which meta should be disabled.
322
+ */
75
323
  routeDisableMeta?: Record<string, boolean | string[]>;
324
+ /**
325
+ * Show console warnings when a translation key is missing.
326
+ * @default true
327
+ */
76
328
  missingWarn?: boolean;
77
- previousPageFallback?: boolean;
329
+ /**
330
+ * Enable Hot Module Replacement for translation files in development.
331
+ * When `true`, changes to JSON translation files trigger an automatic reload
332
+ * without a full page refresh.
333
+ * @default true
334
+ */
78
335
  hmr?: boolean;
336
+ /**
337
+ * Maximum number of entries in the in-memory translation cache.
338
+ * `0` means no limit.
339
+ * @default 0
340
+ */
79
341
  cacheMaxSize?: number;
342
+ /**
343
+ * Time-to-live (in milliseconds) for cached translation entries.
344
+ * `0` means entries never expire.
345
+ * @default 0
346
+ */
80
347
  cacheTtl?: number;
348
+ /**
349
+ * Bucket for experimental/unstable options.
350
+ * Contents may change or be removed without notice between minor versions.
351
+ */
81
352
  experimental?: Record<string, unknown>;
82
353
  }
354
+ /**
355
+ * Extended module options injected into the Nuxt runtime config.
356
+ * Includes resolved values and build-time metadata.
357
+ */
83
358
  export interface ModuleOptionsExtend extends ModuleOptions {
359
+ /** Unix timestamp (ms) of the last build. Used for cache-busting translation requests. */
84
360
  dateBuild: number;
361
+ /** Whether the Nuxt app uses hash-based routing (`router.options.hashMode`). */
85
362
  hashMode: boolean;
363
+ /** Whether the current build is a static site generation (SSG / `nuxi generate`). */
86
364
  isSSG: boolean;
365
+ /** Resolved translations API base URL (after env variable / config merging). */
87
366
  apiBaseUrl: string;
367
+ /** Resolved flag: whether per-page translations are disabled. */
88
368
  disablePageLocales: boolean;
369
+ /** Resolved flag: whether locale-based redirects are enabled. */
89
370
  redirects?: boolean;
90
371
  }
372
+ /**
373
+ * Private (server-only) module options passed to Nitro plugins and server middleware.
374
+ * Extends `ModuleOptions` with resolved root paths and directory info.
375
+ */
91
376
  export interface ModulePrivateOptionsExtend extends ModuleOptions {
377
+ /** Absolute path to the Nuxt project root directory. */
92
378
  rootDir: string;
379
+ /** List of all root directories (includes layer roots in a multi-layer setup). */
93
380
  rootDirs: string[];
381
+ /** Resolved debug flag. */
94
382
  debug: boolean;
383
+ /** Resolved path to the translations directory. */
95
384
  translationDir: string;
385
+ /** Resolved fallback locale code. */
96
386
  fallbackLocale: string;
387
+ /** Resolved translations API base URL. */
97
388
  apiBaseUrl: string;
389
+ /** Resolved client-side host override. */
98
390
  apiBaseClientHost?: string;
391
+ /** Resolved server-side host override. */
99
392
  apiBaseServerHost?: string;
393
+ /** Resolved custom regex matcher (RegExp is serialized to its `.source` string). */
100
394
  customRegexMatcher?: string | RegExp;
395
+ /** Resolved route-to-route translation sharing map. */
101
396
  routesLocaleLinks?: {
102
397
  [key: string]: string;
103
398
  };
104
399
  }
400
+ /** Object shape for a translation value that contains singular/plural forms. */
105
401
  export interface PluralTranslations {
402
+ /** Singular form of the translation (count === 1). */
106
403
  singular: string;
404
+ /** Plural form of the translation (count !== 1). */
107
405
  plural: string;
108
406
  }
407
+ /** A translation value without the `unknown` escape hatch. */
109
408
  export type CleanTranslation = string | number | boolean | Translations | PluralTranslations | null;
409
+ /** A single translation value — can be a primitive, nested object, plural pair, or `unknown`. */
110
410
  export type Translation = CleanTranslation | unknown;
411
+ /** A flat or nested dictionary of translation key-value pairs. */
111
412
  export interface Translations {
112
413
  [key: string]: Translation;
113
414
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@i18n-micro/types",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "",
5
5
  "repository": "s00d/nuxt-i18n-micro",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -1,15 +1,22 @@
1
+ /** BCP-47 locale code string (e.g. `'en'`, `'de-DE'`, `'zh-Hans'`). */
1
2
  export type LocaleCode = string
2
3
 
3
- // Key registry. Users (via generator) will do module augmentation of this interface.
4
+ /**
5
+ * Key registry for typed translation keys.
6
+ * Users (via `@i18n-micro/types-generator`) extend this interface through
7
+ * TypeScript module augmentation to get autocompletion for `$t()` calls.
8
+ */
4
9
  export interface DefineLocaleMessage {
5
10
  // Module augmentation point - will be extended by @i18n-micro/types-generator
6
11
  readonly __augmentation?: never
7
12
  }
8
13
 
9
- // Key type resolution logic.
10
- // If the interface is empty (generator not connected), key type is string.
11
- // If the interface has keys, key type is a union of those keys and string.
12
- // This allows both strict types (autocomplete) and regular strings (dynamic keys).
14
+ /**
15
+ * Resolved translation key type.
16
+ * - When `DefineLocaleMessage` is empty (generator not connected) — resolves to `string`.
17
+ * - When `DefineLocaleMessage` has keys — resolves to a union of those keys **and** `string`,
18
+ * providing autocompletion while still allowing dynamic keys.
19
+ */
13
20
  export type TranslationKey = keyof DefineLocaleMessage extends never ? string : keyof DefineLocaleMessage | string
14
21
 
15
22
  /**
@@ -27,106 +34,434 @@ export type TranslationKey = keyof DefineLocaleMessage extends never ? string :
27
34
  export type ScopedKey<Scope extends string> = Extract<TranslationKey, `${Scope}.${string}`>
28
35
 
29
36
  export interface Locale {
37
+ /** BCP-47 locale code (e.g. `'en'`, `'de-DE'`, `'zh-Hans'`). Used in URLs and as file/key identifier. */
30
38
  code: LocaleCode
39
+ /** When `true`, this locale is excluded from route generation and language switching. */
31
40
  disabled?: boolean
41
+ /** ISO 639-1 / BCP-47 tag for `<html lang>` and `hreflang` SEO attributes (e.g. `'en-US'`). */
32
42
  iso?: string
43
+ /** Text direction. Reflected in `<html dir>` when the locale is active. */
33
44
  dir?: 'ltr' | 'rtl' | 'auto'
45
+ /** Human-readable name shown in language switchers (e.g. `'English'`, `'Deutsch'`). */
34
46
  displayName?: string
47
+ /**
48
+ * Per-locale base URL for SEO meta tags.
49
+ * Useful for multi-domain setups where each locale lives on its own domain
50
+ * (e.g. `'https://en.example.com'`).
51
+ */
35
52
  baseUrl?: string
53
+ /**
54
+ * When `true`, this locale is treated as the "base default" for the domain
55
+ * specified in `baseUrl`. Affects canonical URL generation in multi-domain setups.
56
+ */
36
57
  baseDefault?: boolean
58
+ /**
59
+ * Locale code to fall back to when a translation key is missing in this locale.
60
+ * Overrides the global `fallbackLocale` for this specific locale.
61
+ */
37
62
  fallbackLocale?: string
63
+ /** Allows arbitrary extra properties for user-defined locale metadata. */
38
64
  [key: string]: unknown
39
65
  }
40
66
 
67
+ /**
68
+ * Per-page i18n configuration declared via `defineI18nRoute()` in page components.
69
+ */
41
70
  export interface DefineI18nRouteConfig {
71
+ /**
72
+ * Restrict which locales this page is available in.
73
+ * - `string[]` — list of allowed locale codes.
74
+ * - `Record<LocaleCode, Translations>` — allowed locales with inline translation overrides.
75
+ */
42
76
  locales?: string[] | Record<LocaleCode, Translations>
77
+ /** Per-locale custom route paths (e.g. `{ en: '/about', de: '/ueber-uns' }`). */
43
78
  localeRoutes?: Record<LocaleCode, string>
79
+ /**
80
+ * Disable SEO meta tag generation for this page.
81
+ * - `true` — disable all meta tags.
82
+ * - `string[]` — disable only for the listed locale codes.
83
+ */
44
84
  disableMeta?: boolean | string[]
45
85
  }
86
+
87
+ /** Locale-specific route params for `$switchLocalePath` and `<i18n-link>`. */
46
88
  export type I18nRouteParams = Record<LocaleCode, Record<string, string>> | null
47
89
 
90
+ /** Interpolation parameters passed to the `t()` function. */
48
91
  export type Params = Record<string, string | number | boolean>
49
92
 
93
+ /** Getter function signature used by pluralization to resolve nested keys. */
50
94
  export type Getter = (key: TranslationKey, params?: Record<string, string | number | boolean>, defaultValue?: string) => unknown
95
+
96
+ /**
97
+ * Custom pluralization function.
98
+ * Called when `tc()` / `$tc()` is used. Receives the key, count, params, locale,
99
+ * and a getter to fetch the singular/plural forms from translations.
100
+ */
51
101
  export type PluralFunc = (key: TranslationKey, count: number, params: Params, locale: string, getter: Getter) => string | null
52
102
 
103
+ /**
104
+ * Global per-route locale configuration.
105
+ * - `Record<string, string>` — custom paths per locale for the route.
106
+ * - `false` — exclude the route from localization entirely.
107
+ * - `true` — include with default behavior.
108
+ */
53
109
  export type GlobalLocaleRoutes = Record<string, Record<LocaleCode, string> | false | boolean> | null | undefined
54
110
 
111
+ /** URL routing strategy for locale handling. */
55
112
  export type Strategies = 'no_prefix' | 'prefix_except_default' | 'prefix' | 'prefix_and_default'
56
113
 
114
+ /**
115
+ * Callback invoked when a translation key is missing.
116
+ * Useful for logging, reporting, or providing dynamic fallbacks.
117
+ */
57
118
  export type MissingHandler = (locale: string, key: TranslationKey, routeName: string, instance?: unknown, type?: string) => void
58
119
 
59
120
  export interface ModuleOptions {
121
+ /**
122
+ * List of supported locales.
123
+ * Each entry defines a locale code plus optional metadata (ISO, direction, display name, etc.).
124
+ * @default []
125
+ */
60
126
  locales?: Locale[]
127
+
128
+ /**
129
+ * Generate SEO meta tags (`hreflang`, `canonical`, `og:url`, `og:locale`) automatically.
130
+ * @default true
131
+ */
61
132
  meta?: boolean
133
+
134
+ /**
135
+ * URL routing strategy for locale prefixes.
136
+ * - `'no_prefix'` — no locale in URL; locale stored in cookie.
137
+ * - `'prefix_except_default'` — prefix all locales except the default.
138
+ * - `'prefix'` — always prefix, including the default locale.
139
+ * - `'prefix_and_default'` — like `prefix`, but the default locale is also accessible without prefix.
140
+ * @default 'prefix_except_default'
141
+ */
62
142
  strategy?: Strategies
143
+
144
+ /**
145
+ * Base URL for SEO meta tags (canonical, og:url, hreflang).
146
+ * - `undefined` — dynamically resolved from the current request URL
147
+ * (`useRequestURL().origin` on server, `window.location.origin` on client).
148
+ * Best for multi-domain deployments.
149
+ * - A concrete URL string (e.g. `'https://example.com'`) — used as-is.
150
+ * @default undefined
151
+ */
63
152
  metaBaseUrl?: string
153
+
154
+ /**
155
+ * Trust the `X-Forwarded-Host` header when resolving the base URL for meta tags.
156
+ * Enable when the app runs behind a reverse proxy (nginx, Cloudflare, AWS ALB, etc.)
157
+ * that sets this header to the real client-facing hostname.
158
+ * @default true
159
+ */
160
+ metaTrustForwardedHost?: boolean
161
+
162
+ /**
163
+ * Trust the `X-Forwarded-Proto` header when resolving the protocol for meta tags.
164
+ * Enable when the app runs behind a TLS-terminating proxy so that
165
+ * canonical URLs use `https://` even though the app itself listens on HTTP.
166
+ * @default true
167
+ */
168
+ metaTrustForwardedProto?: boolean
169
+
170
+ /**
171
+ * Register the `defineI18nRoute()` macro plugin, enabling per-page `defineI18nRoute()` calls.
172
+ * @default true
173
+ */
64
174
  define?: boolean
175
+
176
+ /**
177
+ * Enable automatic locale-based redirects.
178
+ * When `true`, visitors are redirected to their preferred locale (detected from cookie,
179
+ * `Accept-Language` header, or the default) on the first visit.
180
+ * @default true
181
+ */
65
182
  redirects?: boolean
183
+
184
+ /**
185
+ * Register the core i18n plugin that provides `$t()`, `$tc()`, `$getLocale()`,
186
+ * `$switchLocale()`, and other runtime helpers.
187
+ * @default true
188
+ */
66
189
  plugin?: boolean
190
+
191
+ /**
192
+ * Register the i18n hooks plugin that provides `i18n:register` and `i18n:beforeLocaleSwitch`
193
+ * / `i18n:afterLocaleSwitch` app-level hooks.
194
+ * @default true
195
+ */
67
196
  hooks?: boolean
197
+
198
+ /**
199
+ * The locale to use when no locale can be determined from URL or user preferences.
200
+ * Also used as the fallback locale for missing translations when `fallbackLocale` is not set.
201
+ * @default 'en'
202
+ */
68
203
  defaultLocale?: string
204
+
205
+ /**
206
+ * Base URL path segment for the translations API route (used in SSR/SSG data fetching).
207
+ * Can also be set via `NUXT_I18N_APP_BASE_URL` environment variable.
208
+ * @default '_locales'
209
+ */
69
210
  apiBaseUrl?: string
211
+
212
+ /**
213
+ * Override the host used for client-side translation fetch requests.
214
+ * Useful when the client reaches the server via a different hostname than the one Nuxt sees.
215
+ * Can also be set via `NUXT_I18N_APP_BASE_CLIENT_HOST` environment variable.
216
+ * @default undefined
217
+ */
70
218
  apiBaseClientHost?: string
219
+
220
+ /**
221
+ * Override the host used for server-side translation fetch requests.
222
+ * Useful in container/microservice setups where the server reaches itself via an internal hostname.
223
+ * Can also be set via `NUXT_I18N_APP_BASE_SERVER_HOST` environment variable.
224
+ * @default undefined
225
+ */
71
226
  apiBaseServerHost?: string
227
+
228
+ /**
229
+ * Path to the directory containing translation JSON files, relative to the project root.
230
+ * @default 'locales'
231
+ */
72
232
  translationDir?: string
233
+
234
+ /**
235
+ * Automatically detect the user's preferred language from the `Accept-Language` HTTP header.
236
+ * Used in combination with `autoDetectPath` to decide when detection occurs.
237
+ * @default true
238
+ */
73
239
  autoDetectLanguage?: boolean
240
+
241
+ /**
242
+ * URL path on which automatic language detection and redirect occur.
243
+ * - `'/'` — detect only on the root path.
244
+ * - `'*'` — detect and redirect on every path (including locale-prefixed ones).
245
+ * @default '/'
246
+ */
74
247
  autoDetectPath?: string
248
+
249
+ /**
250
+ * Disable the file watcher that auto-creates missing translation files in development mode.
251
+ * @default false
252
+ */
75
253
  disableWatcher?: boolean
254
+
255
+ /**
256
+ * Generate TypeScript type declarations for `useI18n`, `$t`, and related helpers
257
+ * based on the translation keys in your default locale files.
258
+ * @default true
259
+ */
76
260
  types?: boolean
77
261
 
262
+ /**
263
+ * Map route names to other route names to share the same translation files.
264
+ * For example, `{ 'about-us': 'about' }` means the `about-us` page will use
265
+ * translations from the `about` page instead of its own.
266
+ * @default {}
267
+ */
78
268
  routesLocaleLinks?: { [key: string]: string }
269
+
270
+ /**
271
+ * Custom pluralization function or a path to a file exporting one.
272
+ * When a string path is provided, the file is imported at build time.
273
+ * The function receives `(key, count, params, locale, getter)` and should return
274
+ * the correct plural form as a string, or `null` to fall back to the built-in logic.
275
+ * @default built-in pluralization (singular/plural by count)
276
+ */
79
277
  plural?: string | PluralFunc
278
+
279
+ /**
280
+ * Disable per-page translation files.
281
+ * When `true`, only global translations (`{locale}.json`) are loaded;
282
+ * page-specific files (`pages/{page}/{locale}.json`) are not generated or loaded.
283
+ * @default false
284
+ */
80
285
  disablePageLocales?: boolean
286
+
287
+ /**
288
+ * Global fallback locale code.
289
+ * When a translation key is missing in the active locale, the module looks it up
290
+ * in this locale before returning the key itself.
291
+ * @default undefined (no fallback; returns the raw key)
292
+ */
81
293
  fallbackLocale?: string
294
+
295
+ /**
296
+ * Cookie name for persisting the user's locale preference across sessions.
297
+ * Set to `null` to disable cookie-based persistence.
298
+ * Automatically set to `'user-locale'` for the `no_prefix` strategy if not provided.
299
+ * @default null
300
+ */
82
301
  localeCookie?: string | null
302
+
303
+ /**
304
+ * Enable verbose debug logging for locale detection, route generation, and translation loading.
305
+ * @default false
306
+ */
83
307
  debug?: boolean
308
+
309
+ /**
310
+ * Global route-level locale configuration.
311
+ * Allows restricting or customizing locale routes for specific pages without
312
+ * modifying their components.
313
+ * - `false` — exclude the route from localization.
314
+ * - `Record<LocaleCode, string>` — custom per-locale paths.
315
+ * @default {}
316
+ */
84
317
  globalLocaleRoutes?: GlobalLocaleRoutes
318
+
319
+ /**
320
+ * Custom regular expression (or its string source) for matching locale codes in URL segments.
321
+ * All locale codes defined in `locales` must match this pattern, or a warning is emitted.
322
+ * @default undefined (uses built-in pattern based on locale codes)
323
+ */
85
324
  customRegexMatcher?: string | RegExp
325
+
326
+ /**
327
+ * For `no_prefix` strategy: enable redirect from a locale-prefixed URL
328
+ * (e.g. `/en/about`) to the unprefixed version (`/about`).
329
+ * @default false
330
+ */
86
331
  noPrefixRedirect?: boolean
332
+
333
+ /**
334
+ * List of query parameter names preserved in canonical and `og:url` meta tags.
335
+ * Parameters not in this list are stripped from the canonical URL.
336
+ * @default ['page', 'sort', 'filter', 'search', 'q', 'query', 'tag']
337
+ */
87
338
  canonicalQueryWhitelist?: string[]
339
+
340
+ /**
341
+ * URL patterns (strings or RegExp) to exclude from i18n processing entirely.
342
+ * Matching routes won't get locale prefixes, redirects, or translation loading.
343
+ * Internal Nuxt paths (`/__nuxt_error`, etc.) are always excluded automatically.
344
+ * @default undefined
345
+ */
88
346
  excludePatterns?: (string | RegExp)[]
347
+
348
+ /**
349
+ * Prefix prepended to localized route names (e.g. `'localized-index'`).
350
+ * Used internally to distinguish original routes from generated locale variants.
351
+ * @default 'localized-'
352
+ */
89
353
  localizedRouteNamePrefix?: string
354
+
355
+ /**
356
+ * Per-route locale restrictions, extracted from `defineI18nRoute()` calls.
357
+ * Maps a route path (e.g. `'/about'`) to an array of allowed locale codes.
358
+ * Routes not listed have no restrictions (all locales allowed).
359
+ */
90
360
  routeLocales?: Record<string, string[]>
361
+
362
+ /**
363
+ * Per-route meta tag disabling, extracted from `defineI18nRoute()` calls.
364
+ * Maps a route path to `true` (disable all meta) or an array of locale codes
365
+ * for which meta should be disabled.
366
+ */
91
367
  routeDisableMeta?: Record<string, boolean | string[]>
368
+
369
+ /**
370
+ * Show console warnings when a translation key is missing.
371
+ * @default true
372
+ */
92
373
  missingWarn?: boolean
93
- previousPageFallback?: boolean
374
+
375
+ /**
376
+ * Enable Hot Module Replacement for translation files in development.
377
+ * When `true`, changes to JSON translation files trigger an automatic reload
378
+ * without a full page refresh.
379
+ * @default true
380
+ */
94
381
  hmr?: boolean
382
+
383
+ /**
384
+ * Maximum number of entries in the in-memory translation cache.
385
+ * `0` means no limit.
386
+ * @default 0
387
+ */
95
388
  cacheMaxSize?: number
389
+
390
+ /**
391
+ * Time-to-live (in milliseconds) for cached translation entries.
392
+ * `0` means entries never expire.
393
+ * @default 0
394
+ */
96
395
  cacheTtl?: number
396
+
397
+ /**
398
+ * Bucket for experimental/unstable options.
399
+ * Contents may change or be removed without notice between minor versions.
400
+ */
97
401
  experimental?: Record<string, unknown>
98
402
  }
99
403
 
404
+ /**
405
+ * Extended module options injected into the Nuxt runtime config.
406
+ * Includes resolved values and build-time metadata.
407
+ */
100
408
  export interface ModuleOptionsExtend extends ModuleOptions {
409
+ /** Unix timestamp (ms) of the last build. Used for cache-busting translation requests. */
101
410
  dateBuild: number
411
+ /** Whether the Nuxt app uses hash-based routing (`router.options.hashMode`). */
102
412
  hashMode: boolean
413
+ /** Whether the current build is a static site generation (SSG / `nuxi generate`). */
103
414
  isSSG: boolean
415
+ /** Resolved translations API base URL (after env variable / config merging). */
104
416
  apiBaseUrl: string
417
+ /** Resolved flag: whether per-page translations are disabled. */
105
418
  disablePageLocales: boolean
419
+ /** Resolved flag: whether locale-based redirects are enabled. */
106
420
  redirects?: boolean
107
421
  }
108
422
 
423
+ /**
424
+ * Private (server-only) module options passed to Nitro plugins and server middleware.
425
+ * Extends `ModuleOptions` with resolved root paths and directory info.
426
+ */
109
427
  export interface ModulePrivateOptionsExtend extends ModuleOptions {
428
+ /** Absolute path to the Nuxt project root directory. */
110
429
  rootDir: string
430
+ /** List of all root directories (includes layer roots in a multi-layer setup). */
111
431
  rootDirs: string[]
432
+ /** Resolved debug flag. */
112
433
  debug: boolean
434
+ /** Resolved path to the translations directory. */
113
435
  translationDir: string
436
+ /** Resolved fallback locale code. */
114
437
  fallbackLocale: string
438
+ /** Resolved translations API base URL. */
115
439
  apiBaseUrl: string
440
+ /** Resolved client-side host override. */
116
441
  apiBaseClientHost?: string
442
+ /** Resolved server-side host override. */
117
443
  apiBaseServerHost?: string
444
+ /** Resolved custom regex matcher (RegExp is serialized to its `.source` string). */
118
445
  customRegexMatcher?: string | RegExp
446
+ /** Resolved route-to-route translation sharing map. */
119
447
  routesLocaleLinks?: { [key: string]: string }
120
448
  }
121
449
 
450
+ /** Object shape for a translation value that contains singular/plural forms. */
122
451
  export interface PluralTranslations {
452
+ /** Singular form of the translation (count === 1). */
123
453
  singular: string
454
+ /** Plural form of the translation (count !== 1). */
124
455
  plural: string
125
456
  }
126
457
 
458
+ /** A translation value without the `unknown` escape hatch. */
127
459
  export type CleanTranslation = string | number | boolean | Translations | PluralTranslations | null
460
+
461
+ /** A single translation value — can be a primitive, nested object, plural pair, or `unknown`. */
128
462
  export type Translation = CleanTranslation | unknown
129
463
 
464
+ /** A flat or nested dictionary of translation key-value pairs. */
130
465
  export interface Translations {
131
466
  [key: string]: Translation
132
467
  }