@i18n-micro/route-strategy 1.1.0
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/LICENSE +22 -0
- package/README.md +228 -0
- package/dist/core/alias.d.ts +6 -0
- package/dist/core/builder.d.ts +20 -0
- package/dist/core/context.d.ts +53 -0
- package/dist/core/localized-paths.d.ts +20 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.mjs +875 -0
- package/dist/route-generator.d.ts +56 -0
- package/dist/strategies/abstract.d.ts +30 -0
- package/dist/strategies/factory.d.ts +3 -0
- package/dist/strategies/no-prefix.d.ts +13 -0
- package/dist/strategies/prefix-and-default.d.ts +8 -0
- package/dist/strategies/prefix-except-default.d.ts +12 -0
- package/dist/strategies/prefix.d.ts +7 -0
- package/dist/strategies/types.d.ts +17 -0
- package/dist/utils/common.d.ts +9 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/locales.d.ts +9 -0
- package/dist/utils/path.d.ts +6 -0
- package/package.json +75 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
+
import { GlobalLocaleRoutes, Locale, Strategies } from '@i18n-micro/types';
|
|
3
|
+
import { LocaleRoutesConfig } from './strategies/types';
|
|
4
|
+
import { LocalizedPathsMap } from './core/localized-paths';
|
|
5
|
+
export interface RouteGeneratorOptions {
|
|
6
|
+
locales: Locale[];
|
|
7
|
+
defaultLocaleCode: string;
|
|
8
|
+
strategy: Strategies;
|
|
9
|
+
globalLocaleRoutes: GlobalLocaleRoutes;
|
|
10
|
+
filesLocaleRoutes?: GlobalLocaleRoutes;
|
|
11
|
+
routeLocales?: Record<string, string[]>;
|
|
12
|
+
noPrefixRedirect: boolean;
|
|
13
|
+
excludePatterns?: (string | RegExp)[];
|
|
14
|
+
localizedRouteNamePrefix?: string;
|
|
15
|
+
customRegexMatcher?: string | RegExp;
|
|
16
|
+
fallbackRedirectComponentPath?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare class RouteGenerator {
|
|
19
|
+
locales: Locale[];
|
|
20
|
+
defaultLocale: Locale;
|
|
21
|
+
strategy: Strategies;
|
|
22
|
+
localizedPaths: LocalizedPathsMap;
|
|
23
|
+
activeLocaleCodes: string[];
|
|
24
|
+
globalLocaleRoutes: LocaleRoutesConfig;
|
|
25
|
+
rawGlobalLocaleRoutes: GlobalLocaleRoutes;
|
|
26
|
+
filesLocaleRoutes: LocaleRoutesConfig;
|
|
27
|
+
routeLocales: Record<string, string[]>;
|
|
28
|
+
noPrefixRedirect: boolean;
|
|
29
|
+
excludePatterns: (string | RegExp)[] | undefined;
|
|
30
|
+
localizedRouteNamePrefix: string;
|
|
31
|
+
customRegex: string | RegExp | undefined;
|
|
32
|
+
fallbackRedirectComponentPath: string | undefined;
|
|
33
|
+
constructor(options: RouteGeneratorOptions);
|
|
34
|
+
private computeActiveLocaleCodes;
|
|
35
|
+
extendPages(pages: NuxtPage[]): void;
|
|
36
|
+
extractLocalizedPaths(pages: NuxtPage[], parentPath?: string): {
|
|
37
|
+
[key: string]: {
|
|
38
|
+
[locale: string]: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Creates global and per-page translation JSON files for the current resolved locales.
|
|
43
|
+
*/
|
|
44
|
+
ensureTranslationFilesExist(pagesNames: string[], translationDir: string, rootDir: string, disablePageLocales?: boolean): void;
|
|
45
|
+
private ensureFileExists;
|
|
46
|
+
/**
|
|
47
|
+
* Returns the localized path for a given URL and locale,
|
|
48
|
+
* respecting strategy and globalLocaleRoutes (custom paths).
|
|
49
|
+
* Used by nitro:config and prerender so route rules work with custom paths (e.g. /de/ueber-uns).
|
|
50
|
+
*/
|
|
51
|
+
resolveLocalizedPath(originalPath: string, localeCode: string): string;
|
|
52
|
+
/**
|
|
53
|
+
* Generates the list of data.json routes for prerender (general + per-page per locale).
|
|
54
|
+
*/
|
|
55
|
+
generateDataRoutes(pages: NuxtPage[], apiBaseUrl: string, disablePageLocales: boolean): string[];
|
|
56
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
+
import { GeneratorContext } from '../core/context';
|
|
3
|
+
import { RouteStrategy } from './types';
|
|
4
|
+
export declare abstract class BaseStrategy implements RouteStrategy {
|
|
5
|
+
processPage(page: NuxtPage, context: GeneratorContext): NuxtPage[];
|
|
6
|
+
postProcess(pages: NuxtPage[], _context: GeneratorContext): NuxtPage[];
|
|
7
|
+
protected abstract generateVariants(page: NuxtPage, context: GeneratorContext): NuxtPage[];
|
|
8
|
+
/**
|
|
9
|
+
* Recursive localization for children: for a given locale, builds:
|
|
10
|
+
* - parentOriginalPath used for Context lookups,
|
|
11
|
+
* - parentLocalizedPath used for the final path,
|
|
12
|
+
* and returns an array of localized child routes.
|
|
13
|
+
*/
|
|
14
|
+
protected localizeChildren(children: NuxtPage[], parentLocalizedPath: string, parentOriginalPath: string, locale: string, context: GeneratorContext, addPrefix: boolean): NuxtPage[];
|
|
15
|
+
/**
|
|
16
|
+
* Variant for a single route with :locale(locale1|locale2) — without custom paths.
|
|
17
|
+
* Returns a single child route with a relative path and recursively localized children.
|
|
18
|
+
*/
|
|
19
|
+
protected localizeChildrenAllLocales(children: NuxtPage[], parentPath: string, parentOriginalPath: string, localeCodes: string[], context: GeneratorContext): NuxtPage[];
|
|
20
|
+
private localizeChildAllLocales;
|
|
21
|
+
private localizeChild;
|
|
22
|
+
/**
|
|
23
|
+
* Shared helper for strategies that build prefix-based paths (:locale(...)):
|
|
24
|
+
* - for custom paths, decides whether a prefix is required,
|
|
25
|
+
* - for regular paths, always uses buildFullPath.
|
|
26
|
+
*
|
|
27
|
+
* The logic matches what used to be duplicated in each strategy's buildRoutePath.
|
|
28
|
+
*/
|
|
29
|
+
protected buildRoutePathForLocales(localeCodes: string[], originalPath: string, customPath: string, isCustom: boolean, customRegex: string | RegExp | undefined, force: boolean, defaultLocaleCode: string): string;
|
|
30
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
+
import { GeneratorContext } from '../core/context';
|
|
3
|
+
import { BaseStrategy } from './abstract';
|
|
4
|
+
export declare class NoPrefixStrategy extends BaseStrategy {
|
|
5
|
+
protected generateVariants(page: NuxtPage, context: GeneratorContext): NuxtPage[];
|
|
6
|
+
/**
|
|
7
|
+
* Localizes the children tree for the no_prefix strategy.
|
|
8
|
+
* Mirrors the behavior of the legacy RouteGenerator:
|
|
9
|
+
* - on the first level, custom paths (globalLocaleRoutes/filesLocaleRoutes) are applied;
|
|
10
|
+
* - on deeper levels, path stays as a segment (item, detail, etc.).
|
|
11
|
+
*/
|
|
12
|
+
private localizeChildrenForNoPrefix;
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
+
import { GeneratorContext } from '../core/context';
|
|
3
|
+
import { BaseStrategy } from './abstract';
|
|
4
|
+
export declare class PrefixAndDefaultStrategy extends BaseStrategy {
|
|
5
|
+
protected generateVariants(page: NuxtPage, context: GeneratorContext): NuxtPage[];
|
|
6
|
+
postProcess(pages: NuxtPage[], context: GeneratorContext): NuxtPage[];
|
|
7
|
+
private createLocalizedRoute;
|
|
8
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
+
import { GeneratorContext } from '../core/context';
|
|
3
|
+
import { BaseStrategy } from './abstract';
|
|
4
|
+
export declare class PrefixExceptDefaultStrategy extends BaseStrategy {
|
|
5
|
+
protected generateVariants(page: NuxtPage, context: GeneratorContext): NuxtPage[];
|
|
6
|
+
postProcess(pages: NuxtPage[], context: GeneratorContext): NuxtPage[];
|
|
7
|
+
private createLocalizedChildren;
|
|
8
|
+
private createLocalizedVariants;
|
|
9
|
+
private buildChildRouteName;
|
|
10
|
+
private createLocalizedRoute;
|
|
11
|
+
private buildLocalizedRouteName;
|
|
12
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
+
import { GeneratorContext } from '../core/context';
|
|
3
|
+
import { BaseStrategy } from './abstract';
|
|
4
|
+
export declare class PrefixStrategy extends BaseStrategy {
|
|
5
|
+
protected generateVariants(page: NuxtPage, context: GeneratorContext): NuxtPage[];
|
|
6
|
+
postProcess(pages: NuxtPage[], context: GeneratorContext): NuxtPage[];
|
|
7
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
+
import { GeneratorContext } from '../core/context';
|
|
3
|
+
export interface RouteStrategy {
|
|
4
|
+
/**
|
|
5
|
+
* Processes a page and returns an array of routes (base + aliases, etc.).
|
|
6
|
+
*/
|
|
7
|
+
processPage(page: NuxtPage, context: GeneratorContext): NuxtPage[];
|
|
8
|
+
/**
|
|
9
|
+
* Post-processes the full pages array (e.g. removing unprefixed routes).
|
|
10
|
+
*/
|
|
11
|
+
postProcess(pages: NuxtPage[], context: GeneratorContext): NuxtPage[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Shared type for global/files-level locale routes configuration.
|
|
15
|
+
* Used in Context and RouteGenerator to avoid repeating the same signature.
|
|
16
|
+
*/
|
|
17
|
+
export type LocaleRoutesConfig = Record<string, Record<string, string> | false | boolean>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
+
import { Locale } from '@i18n-micro/types';
|
|
3
|
+
export declare const cloneArray: <T extends object>(array: T[]) => T[];
|
|
4
|
+
export declare const isPageRedirectOnly: (page: NuxtPage) => boolean;
|
|
5
|
+
export declare const buildRouteName: (baseName: string, localeCode: string, isCustom: boolean, prefix?: string) => string;
|
|
6
|
+
export declare const buildRouteNameFromRoute: (name: string | null | undefined, routePath: string | null | undefined) => string;
|
|
7
|
+
export declare const shouldAddLocalePrefix: (locale: string, defaultLocale: Locale, addLocalePrefix: boolean, includeDefaultLocaleRoute: boolean) => boolean;
|
|
8
|
+
export declare const isLocaleDefault: (locale: string | Locale, defaultLocale: Locale, includeDefaultLocaleRoute: boolean) => boolean;
|
|
9
|
+
export declare function isInternalPath(path: string, excludePatterns?: (string | RegExp | object)[]): boolean;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { normalizePath, normalizeRouteKey, removeLeadingSlash, joinPath, buildFullPath, buildFullPathNoPrefix, } from './path';
|
|
2
|
+
export { cloneArray, isPageRedirectOnly, buildRouteName, buildRouteNameFromRoute, shouldAddLocalePrefix, isLocaleDefault, isInternalPath, } from './common';
|
|
3
|
+
export { resolveLocales, type ResolvedLocales } from './locales';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Locale } from '@i18n-micro/types';
|
|
2
|
+
export interface ResolvedLocales {
|
|
3
|
+
locales: Locale[];
|
|
4
|
+
defaultLocale: Locale;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Merges locale list (dedupe by code, merge props) and filters out disabled locales.
|
|
8
|
+
*/
|
|
9
|
+
export declare function resolveLocales(locales: Locale[], defaultLocaleCode: string): ResolvedLocales;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function normalizeRouteKey(key: string): string;
|
|
2
|
+
export declare const normalizePath: (routePath: string) => string;
|
|
3
|
+
export declare const removeLeadingSlash: (routePath: string) => string;
|
|
4
|
+
export declare function joinPath(...segments: string[]): string;
|
|
5
|
+
export declare function buildFullPath(locale: string | string[], basePath: string, customRegex?: string | RegExp): string;
|
|
6
|
+
export declare function buildFullPathNoPrefix(basePath: string): string;
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@i18n-micro/route-strategy",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Locale-aware route generator for Nuxt i18n micro",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.mjs",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.cjs",
|
|
14
|
+
"default": "./dist/index.mjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"typesVersions": {
|
|
18
|
+
"*": {
|
|
19
|
+
".": [
|
|
20
|
+
"./dist/index.d.ts"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"LICENSE",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"i18n",
|
|
31
|
+
"nuxt",
|
|
32
|
+
"routes",
|
|
33
|
+
"locale",
|
|
34
|
+
"route-generator",
|
|
35
|
+
"internationalization"
|
|
36
|
+
],
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "https://github.com/s00d/nuxt-i18n-micro.git",
|
|
40
|
+
"directory": "packages/route-generator"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/s00d/nuxt-i18n-micro/tree/main/packages/route-generator#readme",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/s00d/nuxt-i18n-micro/issues"
|
|
45
|
+
},
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"author": {
|
|
48
|
+
"name": "s00d",
|
|
49
|
+
"email": "Virus191288@gmail.com",
|
|
50
|
+
"url": "https://s00d.github.io/"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
},
|
|
55
|
+
"sideEffects": false,
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@nuxt/schema": "^3.18.1 || ^4.0.0",
|
|
58
|
+
"@i18n-micro/types": "1.1.0",
|
|
59
|
+
"@i18n-micro/core": "1.1.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@types/jest": "^29.5.14",
|
|
63
|
+
"jest": "^29.7.0",
|
|
64
|
+
"ts-jest": "^29.4.6",
|
|
65
|
+
"vite": "^7.3.1",
|
|
66
|
+
"vite-plugin-dts": "^4.5.4"
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=18"
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "vite build",
|
|
73
|
+
"test": "jest"
|
|
74
|
+
}
|
|
75
|
+
}
|