@modern-js/plugin-i18n 3.3.0 → 3.5.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/dist/cjs/cli/index.js +7 -59
- package/dist/cjs/cli/locales.js +132 -0
- package/dist/cjs/runtime/i18n/backend/defaults.js +6 -2
- package/dist/cjs/runtime/i18n/backend/defaults.node.js +47 -7
- package/dist/esm/cli/index.mjs +7 -48
- package/dist/esm/cli/locales.mjs +80 -0
- package/dist/esm/runtime/i18n/backend/defaults.mjs +6 -2
- package/dist/esm/runtime/i18n/backend/defaults.node.mjs +33 -2
- package/dist/esm/runtime/utils.mjs +2 -2
- package/dist/esm-node/cli/index.mjs +7 -48
- package/dist/esm-node/cli/locales.mjs +81 -0
- package/dist/esm-node/runtime/i18n/backend/defaults.mjs +6 -2
- package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +33 -2
- package/dist/types/cli/locales.d.ts +17 -0
- package/dist/types/runtime/i18n/backend/defaults.d.ts +10 -7
- package/dist/types/runtime/i18n/backend/defaults.node.d.ts +10 -2
- package/dist/types/shared/type.d.ts +20 -0
- package/package.json +11 -11
- package/src/cli/index.ts +24 -100
- package/src/cli/locales.ts +186 -0
- package/src/runtime/i18n/backend/defaults.node.ts +74 -5
- package/src/runtime/i18n/backend/defaults.ts +20 -16
- package/src/shared/type.ts +20 -0
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
|
|
1
3
|
export const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
|
|
2
4
|
loadPath: './locales/{{lng}}/{{ns}}.json',
|
|
3
5
|
addPath: './locales/{{lng}}/{{ns}}.json',
|
|
@@ -14,18 +16,85 @@ function convertPath(path: string | undefined): string | undefined {
|
|
|
14
16
|
return path;
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
interface InternalBackendPathOptions {
|
|
20
|
+
loadPath?: string;
|
|
21
|
+
addPath?: string;
|
|
22
|
+
serverLoadPath?: string;
|
|
23
|
+
serverAddPath?: string;
|
|
24
|
+
serverLoadPaths?: string[];
|
|
25
|
+
serverAddPaths?: string[];
|
|
26
|
+
_detectedLoadPath?: string;
|
|
27
|
+
_detectedAddPath?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function shouldUseServerPath(
|
|
31
|
+
currentPath: string | undefined,
|
|
32
|
+
detectedPath: string | undefined,
|
|
33
|
+
): boolean {
|
|
34
|
+
return !detectedPath || currentPath === detectedPath;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function getResourceBasePath(resourcePath: string): string {
|
|
38
|
+
const markerIndex = resourcePath.indexOf('{{lng}}');
|
|
39
|
+
if (markerIndex < 0) {
|
|
40
|
+
return resourcePath;
|
|
41
|
+
}
|
|
42
|
+
return resourcePath.slice(0, markerIndex).replace(/[\\/]+$/, '');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function pathExists(resourcePath: string): boolean {
|
|
46
|
+
try {
|
|
47
|
+
return fs.existsSync(getResourceBasePath(resourcePath));
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getServerPath(
|
|
54
|
+
pathCandidates: string[] | undefined,
|
|
55
|
+
fallbackPath: string | undefined,
|
|
56
|
+
): string | undefined {
|
|
57
|
+
const candidates = Array.from(
|
|
58
|
+
new Set([...(pathCandidates || []), fallbackPath].filter(Boolean)),
|
|
59
|
+
) as string[];
|
|
60
|
+
|
|
61
|
+
return candidates.find(pathExists) || candidates[0];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function convertBackendOptions<T extends InternalBackendPathOptions>(
|
|
65
|
+
options: T,
|
|
66
|
+
): T {
|
|
20
67
|
if (!options) {
|
|
21
68
|
return options;
|
|
22
69
|
}
|
|
23
70
|
const converted = { ...options };
|
|
24
|
-
if (
|
|
71
|
+
if (
|
|
72
|
+
(converted.serverLoadPath || converted.serverLoadPaths) &&
|
|
73
|
+
shouldUseServerPath(converted.loadPath, converted._detectedLoadPath)
|
|
74
|
+
) {
|
|
75
|
+
converted.loadPath = getServerPath(
|
|
76
|
+
converted.serverLoadPaths,
|
|
77
|
+
converted.serverLoadPath,
|
|
78
|
+
);
|
|
79
|
+
} else if (converted.loadPath) {
|
|
25
80
|
converted.loadPath = convertPath(converted.loadPath);
|
|
26
81
|
}
|
|
27
|
-
if (
|
|
82
|
+
if (
|
|
83
|
+
(converted.serverAddPath || converted.serverAddPaths) &&
|
|
84
|
+
shouldUseServerPath(converted.addPath, converted._detectedAddPath)
|
|
85
|
+
) {
|
|
86
|
+
converted.addPath = getServerPath(
|
|
87
|
+
converted.serverAddPaths,
|
|
88
|
+
converted.serverAddPath,
|
|
89
|
+
);
|
|
90
|
+
} else if (converted.addPath) {
|
|
28
91
|
converted.addPath = convertPath(converted.addPath);
|
|
29
92
|
}
|
|
93
|
+
delete converted.serverLoadPath;
|
|
94
|
+
delete converted.serverAddPath;
|
|
95
|
+
delete converted.serverLoadPaths;
|
|
96
|
+
delete converted.serverAddPaths;
|
|
97
|
+
delete converted._detectedLoadPath;
|
|
98
|
+
delete converted._detectedAddPath;
|
|
30
99
|
return converted;
|
|
31
100
|
}
|
|
@@ -3,30 +3,34 @@ export const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
|
|
|
3
3
|
addPath: '/locales/{{lng}}/{{ns}}.json',
|
|
4
4
|
};
|
|
5
5
|
|
|
6
|
-
declare global {
|
|
7
|
-
interface Window {
|
|
8
|
-
__assetPrefix__?: string;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
6
|
function convertPath(path: string | undefined): string | undefined {
|
|
13
|
-
if (!path) {
|
|
14
|
-
return path;
|
|
15
|
-
}
|
|
16
|
-
// If it's an absolute path (starts with /), convert to relative path
|
|
17
|
-
if (path.startsWith('/')) {
|
|
18
|
-
return `${window.__assetPrefix__ || ''}${path}`;
|
|
19
|
-
}
|
|
20
7
|
return path;
|
|
21
8
|
}
|
|
22
9
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
10
|
+
interface InternalBackendPathOptions {
|
|
11
|
+
loadPath?: string;
|
|
12
|
+
addPath?: string;
|
|
13
|
+
serverLoadPath?: string;
|
|
14
|
+
serverAddPath?: string;
|
|
15
|
+
serverLoadPaths?: string[];
|
|
16
|
+
serverAddPaths?: string[];
|
|
17
|
+
_detectedLoadPath?: string;
|
|
18
|
+
_detectedAddPath?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function convertBackendOptions<T extends InternalBackendPathOptions>(
|
|
22
|
+
options: T,
|
|
23
|
+
): T {
|
|
26
24
|
if (!options) {
|
|
27
25
|
return options;
|
|
28
26
|
}
|
|
29
27
|
const converted = { ...options };
|
|
28
|
+
delete converted.serverLoadPath;
|
|
29
|
+
delete converted.serverAddPath;
|
|
30
|
+
delete converted.serverLoadPaths;
|
|
31
|
+
delete converted.serverAddPaths;
|
|
32
|
+
delete converted._detectedLoadPath;
|
|
33
|
+
delete converted._detectedAddPath;
|
|
30
34
|
if (converted.loadPath) {
|
|
31
35
|
converted.loadPath = convertPath(converted.loadPath);
|
|
32
36
|
}
|
package/src/shared/type.ts
CHANGED
|
@@ -71,6 +71,26 @@ export interface BaseBackendOptions {
|
|
|
71
71
|
enabled?: boolean;
|
|
72
72
|
loadPath?: string;
|
|
73
73
|
addPath?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Internal file-system path used by the Node.js FS backend.
|
|
76
|
+
* Browser HTTP backend keeps using `loadPath`.
|
|
77
|
+
*/
|
|
78
|
+
serverLoadPath?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Internal file-system path candidates used by the Node.js FS backend.
|
|
81
|
+
* The first existing path under current cwd will be used.
|
|
82
|
+
*/
|
|
83
|
+
serverLoadPaths?: string[];
|
|
84
|
+
/**
|
|
85
|
+
* Internal file-system path used by the Node.js FS backend.
|
|
86
|
+
* Browser HTTP backend keeps using `addPath`.
|
|
87
|
+
*/
|
|
88
|
+
serverAddPath?: string;
|
|
89
|
+
/**
|
|
90
|
+
* Internal file-system path candidates used by the Node.js FS backend.
|
|
91
|
+
* The first existing path under current cwd will be used.
|
|
92
|
+
*/
|
|
93
|
+
serverAddPaths?: string[];
|
|
74
94
|
/**
|
|
75
95
|
* Cache hit mode for chained backend (only effective when both `loadPath` and `sdk` are provided)
|
|
76
96
|
*
|