@intlayer/sync-json-plugin 7.0.5 → 7.0.6
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/index.cjs +5 -155
- package/dist/cjs/loadJSON.cjs +90 -0
- package/dist/cjs/loadJSON.cjs.map +1 -0
- package/dist/cjs/syncJSON.cjs +160 -0
- package/dist/cjs/syncJSON.cjs.map +1 -0
- package/dist/esm/index.mjs +3 -150
- package/dist/esm/loadJSON.mjs +86 -0
- package/dist/esm/loadJSON.mjs.map +1 -0
- package/dist/esm/syncJSON.mjs +154 -0
- package/dist/esm/syncJSON.mjs.map +1 -0
- package/dist/types/index.d.ts +3 -58
- package/dist/types/loadJSON.d.ts +74 -0
- package/dist/types/loadJSON.d.ts.map +1 -0
- package/dist/types/syncJSON.d.ts +62 -0
- package/dist/types/syncJSON.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,156 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
node_fs_promises = require_rolldown_runtime.__toESM(node_fs_promises);
|
|
4
|
-
let node_path = require("node:path");
|
|
5
|
-
node_path = require_rolldown_runtime.__toESM(node_path);
|
|
6
|
-
let __intlayer_config = require("@intlayer/config");
|
|
7
|
-
__intlayer_config = require_rolldown_runtime.__toESM(__intlayer_config);
|
|
8
|
-
let fast_glob = require("fast-glob");
|
|
9
|
-
fast_glob = require_rolldown_runtime.__toESM(fast_glob);
|
|
1
|
+
const require_syncJSON = require('./syncJSON.cjs');
|
|
2
|
+
const require_loadJSON = require('./loadJSON.cjs');
|
|
10
3
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const keyPlaceholder = "{{__KEY__}}";
|
|
15
|
-
const localePlaceholder = "{{__LOCALE__}}";
|
|
16
|
-
const escapedMask = escapeRegex(maskPattern);
|
|
17
|
-
const localesAlternation = locales.join("|");
|
|
18
|
-
let regexStr = `^${escapedMask}$`;
|
|
19
|
-
regexStr = regexStr.replace(escapeRegex(localePlaceholder), `(?<locale>${localesAlternation})`);
|
|
20
|
-
if (maskPattern.includes(keyPlaceholder)) regexStr = regexStr.replace(escapeRegex(keyPlaceholder), "(?<key>[^/]+)");
|
|
21
|
-
const match = new RegExp(regexStr).exec(filePath);
|
|
22
|
-
let locale;
|
|
23
|
-
let key;
|
|
24
|
-
if (match?.groups) {
|
|
25
|
-
locale = match.groups.locale;
|
|
26
|
-
key = match.groups.key ?? "index";
|
|
27
|
-
}
|
|
28
|
-
return {
|
|
29
|
-
key,
|
|
30
|
-
locale
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
const listMessages = (builder, configuration) => {
|
|
34
|
-
const { content, internationalization } = configuration;
|
|
35
|
-
const baseDir = content.baseDir;
|
|
36
|
-
const locales = internationalization.locales;
|
|
37
|
-
const globPattern = builder({
|
|
38
|
-
key: "*",
|
|
39
|
-
locale: `{${locales.map((locale) => locale).join(",")}}`
|
|
40
|
-
});
|
|
41
|
-
const maskPattern = builder({
|
|
42
|
-
key: "{{__KEY__}}",
|
|
43
|
-
locale: "{{__LOCALE__}}"
|
|
44
|
-
});
|
|
45
|
-
const files = fast_glob.default.sync(globPattern, { cwd: baseDir });
|
|
46
|
-
const result = {};
|
|
47
|
-
for (const file of files) {
|
|
48
|
-
const { key, locale } = extractKeyAndLocaleFromPath(file, maskPattern, locales);
|
|
49
|
-
const absolutePath = (0, node_path.isAbsolute)(file) ? file : (0, node_path.resolve)(baseDir, file);
|
|
50
|
-
if (!result[locale]) result[locale] = {};
|
|
51
|
-
result[locale][key] = absolutePath;
|
|
52
|
-
}
|
|
53
|
-
const hasKeyInMask = maskPattern.includes("{{__KEY__}}");
|
|
54
|
-
const discoveredKeys = /* @__PURE__ */ new Set();
|
|
55
|
-
for (const locale of Object.keys(result)) for (const key of Object.keys(result[locale] ?? {})) discoveredKeys.add(key);
|
|
56
|
-
if (!hasKeyInMask) discoveredKeys.add("index");
|
|
57
|
-
const keysToEnsure = discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];
|
|
58
|
-
for (const locale of locales) {
|
|
59
|
-
if (!result[locale]) result[locale] = {};
|
|
60
|
-
for (const key of keysToEnsure) if (!result[locale][key]) {
|
|
61
|
-
const builtPath = builder({
|
|
62
|
-
key,
|
|
63
|
-
locale
|
|
64
|
-
});
|
|
65
|
-
const absoluteBuiltPath = (0, node_path.isAbsolute)(builtPath) ? builtPath : (0, node_path.resolve)(baseDir, builtPath);
|
|
66
|
-
result[locale][key] = absoluteBuiltPath;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return result;
|
|
70
|
-
};
|
|
71
|
-
const loadMessagePathMap = (source, configuration) => {
|
|
72
|
-
const messages = listMessages(source, configuration);
|
|
73
|
-
return Object.entries(messages).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
74
|
-
return {
|
|
75
|
-
path: (0, node_path.isAbsolute)(path) ? path : (0, node_path.resolve)(configuration.content.baseDir, path),
|
|
76
|
-
locale,
|
|
77
|
-
key
|
|
78
|
-
};
|
|
79
|
-
}));
|
|
80
|
-
};
|
|
81
|
-
const syncJSON = (options) => {
|
|
82
|
-
const { location, priority } = {
|
|
83
|
-
location: "plugin",
|
|
84
|
-
priority: 0,
|
|
85
|
-
...options
|
|
86
|
-
};
|
|
87
|
-
return {
|
|
88
|
-
name: "sync-json",
|
|
89
|
-
loadDictionaries: async ({ configuration }) => {
|
|
90
|
-
const dictionariesMap = loadMessagePathMap(options.source, configuration);
|
|
91
|
-
let fill = options.source({
|
|
92
|
-
key: "{{key}}",
|
|
93
|
-
locale: "{{locale}}"
|
|
94
|
-
});
|
|
95
|
-
if (fill && !(0, node_path.isAbsolute)(fill)) fill = (0, node_path.join)(configuration.content.baseDir, fill);
|
|
96
|
-
const dictionaries = [];
|
|
97
|
-
for (const { locale, path, key } of dictionariesMap) {
|
|
98
|
-
const requireFunction = configuration.build?.require ?? (0, __intlayer_config.getProjectRequire)();
|
|
99
|
-
let json = {};
|
|
100
|
-
try {
|
|
101
|
-
json = requireFunction(path);
|
|
102
|
-
} catch {
|
|
103
|
-
json = {};
|
|
104
|
-
}
|
|
105
|
-
const filePath = (0, node_path.relative)(configuration.content.baseDir, path);
|
|
106
|
-
const dictionary = {
|
|
107
|
-
key,
|
|
108
|
-
locale,
|
|
109
|
-
fill,
|
|
110
|
-
localId: `${key}::${location}::${filePath}`,
|
|
111
|
-
location,
|
|
112
|
-
filled: locale !== configuration.internationalization.defaultLocale ? true : void 0,
|
|
113
|
-
content: json,
|
|
114
|
-
filePath,
|
|
115
|
-
priority
|
|
116
|
-
};
|
|
117
|
-
dictionaries.push(dictionary);
|
|
118
|
-
}
|
|
119
|
-
return dictionaries;
|
|
120
|
-
},
|
|
121
|
-
formatOutput: ({ dictionary }) => {
|
|
122
|
-
if (!dictionary.filePath || !dictionary.locale) return dictionary;
|
|
123
|
-
if ((0, node_path.resolve)(options.source({
|
|
124
|
-
key: dictionary.key,
|
|
125
|
-
locale: dictionary.locale
|
|
126
|
-
})) !== (0, node_path.resolve)(dictionary.filePath)) return dictionary;
|
|
127
|
-
return dictionary.content;
|
|
128
|
-
},
|
|
129
|
-
afterBuild: async ({ dictionaries, configuration }) => {
|
|
130
|
-
const { getLocalizedContent } = await import("@intlayer/core");
|
|
131
|
-
const { parallelize } = await import("@intlayer/chokidar");
|
|
132
|
-
const locales = configuration.internationalization.locales;
|
|
133
|
-
await parallelize(Object.entries(dictionaries.mergedDictionaries).flatMap(([key, dictionary]) => locales.map((locale) => ({
|
|
134
|
-
key,
|
|
135
|
-
dictionary: dictionary.dictionary,
|
|
136
|
-
locale
|
|
137
|
-
}))), async ({ key, dictionary, locale }) => {
|
|
138
|
-
const builderPath = options.source({
|
|
139
|
-
key,
|
|
140
|
-
locale
|
|
141
|
-
});
|
|
142
|
-
const localizedContent = getLocalizedContent(JSON.parse(JSON.stringify(dictionary.content)), locale, {
|
|
143
|
-
dictionaryKey: key,
|
|
144
|
-
keyPath: []
|
|
145
|
-
});
|
|
146
|
-
if (typeof localizedContent === "undefined" || typeof localizedContent === "object" && Object.keys(localizedContent).length === 0) return;
|
|
147
|
-
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(builderPath), { recursive: true });
|
|
148
|
-
await (0, node_fs_promises.writeFile)(builderPath, `${JSON.stringify(localizedContent, null, 2)}\n`, "utf-8");
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
//#endregion
|
|
155
|
-
exports.syncJSON = syncJSON;
|
|
156
|
-
//# sourceMappingURL=index.cjs.map
|
|
4
|
+
exports.extractKeyAndLocaleFromPath = require_syncJSON.extractKeyAndLocaleFromPath;
|
|
5
|
+
exports.loadJSON = require_loadJSON.loadJSON;
|
|
6
|
+
exports.syncJSON = require_syncJSON.syncJSON;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_syncJSON = require('./syncJSON.cjs');
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
node_path = require_rolldown_runtime.__toESM(node_path);
|
|
5
|
+
let __intlayer_config = require("@intlayer/config");
|
|
6
|
+
__intlayer_config = require_rolldown_runtime.__toESM(__intlayer_config);
|
|
7
|
+
let fast_glob = require("fast-glob");
|
|
8
|
+
fast_glob = require_rolldown_runtime.__toESM(fast_glob);
|
|
9
|
+
|
|
10
|
+
//#region src/loadJSON.ts
|
|
11
|
+
const listMessages = (builder, configuration, selectedLocale) => {
|
|
12
|
+
const { content, internationalization } = configuration;
|
|
13
|
+
const baseDir = content.baseDir;
|
|
14
|
+
const locales = internationalization.locales;
|
|
15
|
+
const globPattern = builder({
|
|
16
|
+
key: "*",
|
|
17
|
+
locale: `{${locales.map((locale) => locale).join(",")}}`
|
|
18
|
+
});
|
|
19
|
+
const maskPattern = builder({
|
|
20
|
+
key: "{{__KEY__}}",
|
|
21
|
+
locale: "{{__LOCALE__}}"
|
|
22
|
+
});
|
|
23
|
+
const files = fast_glob.default.sync(globPattern, { cwd: baseDir });
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const file of files) {
|
|
26
|
+
const { key, locale } = require_syncJSON.extractKeyAndLocaleFromPath(file, maskPattern, locales, selectedLocale);
|
|
27
|
+
const absolutePath = (0, node_path.isAbsolute)(file) ? file : (0, node_path.resolve)(baseDir, file);
|
|
28
|
+
if (!result[locale]) result[locale] = {};
|
|
29
|
+
result[locale][key] = absolutePath;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
};
|
|
33
|
+
const loadMessagePathMap = (source, configuration, selectedLocale) => {
|
|
34
|
+
const builder = source;
|
|
35
|
+
const messages = listMessages(builder, configuration, selectedLocale);
|
|
36
|
+
return (builder({
|
|
37
|
+
key: "{{__KEY__}}",
|
|
38
|
+
locale: "{{__LOCALE__}}"
|
|
39
|
+
}).includes("{{__LOCALE__}}") && selectedLocale ? Object.entries(messages).filter(([locale]) => locale === selectedLocale) : Object.entries(messages)).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
40
|
+
return {
|
|
41
|
+
path: (0, node_path.isAbsolute)(path) ? path : (0, node_path.resolve)(configuration.content.baseDir, path),
|
|
42
|
+
locale,
|
|
43
|
+
key
|
|
44
|
+
};
|
|
45
|
+
}));
|
|
46
|
+
};
|
|
47
|
+
const loadJSON = (options) => {
|
|
48
|
+
const { location, priority, locale } = {
|
|
49
|
+
location: "plugin",
|
|
50
|
+
priority: 0,
|
|
51
|
+
...options
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
name: "load-json",
|
|
55
|
+
loadDictionaries: async ({ configuration }) => {
|
|
56
|
+
const usedLocale = locale ?? configuration.internationalization.defaultLocale;
|
|
57
|
+
const dictionariesMap = loadMessagePathMap(options.source, configuration, usedLocale);
|
|
58
|
+
let filePath = options.source({ key: "{{key}}" });
|
|
59
|
+
if (filePath && !(0, node_path.isAbsolute)(filePath)) filePath = (0, node_path.join)(configuration.content.baseDir, filePath);
|
|
60
|
+
const dictionaries = [];
|
|
61
|
+
for (const { path, key } of dictionariesMap) {
|
|
62
|
+
const requireFunction = configuration.build?.require ?? (0, __intlayer_config.getProjectRequire)();
|
|
63
|
+
let json = {};
|
|
64
|
+
try {
|
|
65
|
+
json = requireFunction(path);
|
|
66
|
+
} catch {
|
|
67
|
+
json = {};
|
|
68
|
+
}
|
|
69
|
+
const filePath$1 = (0, node_path.relative)(configuration.content.baseDir, path);
|
|
70
|
+
const dictionary = {
|
|
71
|
+
key,
|
|
72
|
+
locale: usedLocale,
|
|
73
|
+
fill: filePath$1,
|
|
74
|
+
localId: `${key}::${location}::${filePath$1}`,
|
|
75
|
+
location,
|
|
76
|
+
filled: usedLocale !== configuration.internationalization.defaultLocale ? true : void 0,
|
|
77
|
+
content: json,
|
|
78
|
+
filePath: filePath$1,
|
|
79
|
+
priority
|
|
80
|
+
};
|
|
81
|
+
dictionaries.push(dictionary);
|
|
82
|
+
}
|
|
83
|
+
return dictionaries;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
exports.loadJSON = loadJSON;
|
|
90
|
+
//# sourceMappingURL=loadJSON.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadJSON.cjs","names":["fg","result: MessagesRecord","extractKeyAndLocaleFromPath","messages: MessagesRecord","dictionariesMap: DictionariesMap","filePath: string","dictionaries: Dictionary[]","json: JSONContent","filePath","dictionary: Dictionary"],"sources":["../../src/loadJSON.ts"],"sourcesContent":["import { isAbsolute, join, relative, resolve } from 'node:path';\nimport { getProjectRequire } from '@intlayer/config';\nimport type {\n Dictionary,\n IntlayerConfig,\n LocalDictionaryId,\n Locale,\n Plugin,\n} from '@intlayer/types';\nimport fg from 'fast-glob';\nimport { extractKeyAndLocaleFromPath } from './syncJSON';\n\ntype JSONContent = Record<string, any>;\n\ntype Builder = ({\n key,\n locale,\n}: {\n key: string;\n locale?: Locale | (string & {});\n}) => string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst listMessages = (\n builder: Builder,\n configuration: IntlayerConfig,\n selectedLocale: Locale\n): MessagesRecord => {\n const { content, internationalization } = configuration;\n\n const baseDir = content.baseDir;\n const locales = internationalization.locales;\n\n const localePattern = `{${locales.map((locale) => locale).join(',')}}`;\n\n const globPattern = builder({ key: '*', locale: localePattern });\n const maskPattern = builder({ key: '{{__KEY__}}', locale: '{{__LOCALE__}}' });\n\n const files = fg.sync(globPattern, {\n cwd: baseDir,\n });\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const file of files) {\n const { key, locale } = extractKeyAndLocaleFromPath(\n file,\n maskPattern,\n locales,\n selectedLocale\n );\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n if (!result[locale as Locale]) {\n result[locale as Locale] = {};\n }\n\n result[locale as Locale][key as Dictionary['key']] = absolutePath;\n }\n\n // For the load plugin we only use actual discovered files; do not fabricate\n // missing locales or keys, since we don't write outputs.\n return result;\n};\n\ntype FilePath = string;\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = (\n source: MessagesRecord | Builder,\n configuration: IntlayerConfig,\n selectedLocale: Locale\n) => {\n const builder = source as Builder;\n const messages: MessagesRecord = listMessages(\n builder,\n configuration,\n selectedLocale\n );\n\n const maskPattern = builder({\n key: '{{__KEY__}}',\n locale: '{{__LOCALE__}}',\n });\n const hasLocaleInMask = maskPattern.includes('{{__LOCALE__}}');\n\n const entries = (\n hasLocaleInMask && selectedLocale\n ? Object.entries(messages).filter(([locale]) => locale === selectedLocale)\n : Object.entries(messages)\n ) as [Locale, Record<Dictionary['key'], FilePath>][];\n\n const dictionariesPathMap: DictionariesMap = entries.flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.content.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype LoadJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * })\n * ```\n */\n source: Builder;\n\n /**\n * Locale\n *\n * If not provided, the plugin will consider the default locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * locale: Locales.ENGLISH,\n * })\n * ```\n */\n locale?: Locale;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * @example\n * ```ts\n * const config = {\n * plugins: [\n * loadJSON({\n * source: ({ key }) => `./resources/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * loadJSON({\n * source: ({ key }) => `./messages/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n};\n\nexport const loadJSON = (options: LoadJSONPluginOptions): Plugin => {\n const { location, priority, locale } = {\n location: 'plugin',\n priority: 0,\n ...options,\n };\n\n return {\n name: 'load-json',\n\n loadDictionaries: async ({ configuration }) => {\n const usedLocale = (locale ??\n configuration.internationalization.defaultLocale) as Locale;\n\n const dictionariesMap: DictionariesMap = loadMessagePathMap(\n options.source,\n configuration,\n usedLocale\n );\n\n let filePath: string = options.source({\n key: '{{key}}',\n });\n\n if (filePath && !isAbsolute(filePath)) {\n filePath = join(configuration.content.baseDir, filePath);\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { path, key } of dictionariesMap) {\n const requireFunction =\n configuration.build?.require ?? getProjectRequire();\n let json: JSONContent = {};\n try {\n json = requireFunction(path as string);\n } catch {\n // File does not exist yet; default to empty content so it can be filled later\n json = {};\n }\n\n const filePath = relative(configuration.content.baseDir, path);\n\n const dictionary: Dictionary = {\n key,\n locale: usedLocale,\n fill: filePath,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n usedLocale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n };\n};\n"],"mappings":";;;;;;;;;;AAwBA,MAAM,gBACJ,SACA,eACA,mBACmB;CACnB,MAAM,EAAE,SAAS,yBAAyB;CAE1C,MAAM,UAAU,QAAQ;CACxB,MAAM,UAAU,qBAAqB;CAIrC,MAAM,cAAc,QAAQ;EAAE,KAAK;EAAK,QAFlB,IAAI,QAAQ,KAAK,WAAW,OAAO,CAAC,KAAK,IAAI,CAAC;EAEL,CAAC;CAChE,MAAM,cAAc,QAAQ;EAAE,KAAK;EAAe,QAAQ;EAAkB,CAAC;CAE7E,MAAM,QAAQA,kBAAG,KAAK,aAAa,EACjC,KAAK,SACN,CAAC;CAEF,MAAMC,SAAyB,EAAE;AAEjC,MAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,EAAE,KAAK,WAAWC,6CACtB,MACA,aACA,SACA,eACD;EAED,MAAM,yCAA0B,KAAK,GAAG,8BAAe,SAAS,KAAK;AAErE,MAAI,CAAC,OAAO,QACV,QAAO,UAAoB,EAAE;AAG/B,SAAO,QAAkB,OAA4B;;AAKvD,QAAO;;AAOT,MAAM,sBACJ,QACA,eACA,mBACG;CACH,MAAM,UAAU;CAChB,MAAMC,WAA2B,aAC/B,SACA,eACA,eACD;AA6BD,SA3BoB,QAAQ;EAC1B,KAAK;EACL,QAAQ;EACT,CAAC,C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_path = require("node:path");
|
|
3
|
+
node_path = require_rolldown_runtime.__toESM(node_path);
|
|
4
|
+
let __intlayer_config = require("@intlayer/config");
|
|
5
|
+
__intlayer_config = require_rolldown_runtime.__toESM(__intlayer_config);
|
|
6
|
+
let fast_glob = require("fast-glob");
|
|
7
|
+
fast_glob = require_rolldown_runtime.__toESM(fast_glob);
|
|
8
|
+
let node_fs_promises = require("node:fs/promises");
|
|
9
|
+
node_fs_promises = require_rolldown_runtime.__toESM(node_fs_promises);
|
|
10
|
+
|
|
11
|
+
//#region src/syncJSON.ts
|
|
12
|
+
const escapeRegex = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
13
|
+
const extractKeyAndLocaleFromPath = (filePath, maskPattern, locales, defaultLocale) => {
|
|
14
|
+
const keyPlaceholder = "{{__KEY__}}";
|
|
15
|
+
const localePlaceholder = "{{__LOCALE__}}";
|
|
16
|
+
const escapedMask = escapeRegex(maskPattern);
|
|
17
|
+
const localesAlternation = locales.join("|");
|
|
18
|
+
let regexStr = `^${escapedMask}$`;
|
|
19
|
+
regexStr = regexStr.replace(escapeRegex(localePlaceholder), `(?<locale>${localesAlternation})`);
|
|
20
|
+
if (maskPattern.includes(keyPlaceholder)) regexStr = regexStr.replace(escapeRegex(keyPlaceholder), "(?<key>[^/]+)");
|
|
21
|
+
const match = new RegExp(regexStr).exec(filePath);
|
|
22
|
+
let locale;
|
|
23
|
+
let key;
|
|
24
|
+
if (match?.groups) {
|
|
25
|
+
locale = match.groups.locale;
|
|
26
|
+
key = match.groups.key ?? "index";
|
|
27
|
+
}
|
|
28
|
+
if (typeof key === "undefined") key = "index";
|
|
29
|
+
if (typeof locale === "undefined") locale = defaultLocale;
|
|
30
|
+
return {
|
|
31
|
+
key,
|
|
32
|
+
locale
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
const listMessages = (builder, configuration) => {
|
|
36
|
+
const { content, internationalization } = configuration;
|
|
37
|
+
const baseDir = content.baseDir;
|
|
38
|
+
const locales = internationalization.locales;
|
|
39
|
+
const defaultLocale = internationalization.defaultLocale;
|
|
40
|
+
const globPattern = builder({
|
|
41
|
+
key: "*",
|
|
42
|
+
locale: `{${locales.map((locale) => locale).join(",")}}`
|
|
43
|
+
});
|
|
44
|
+
const maskPattern = builder({
|
|
45
|
+
key: "{{__KEY__}}",
|
|
46
|
+
locale: "{{__LOCALE__}}"
|
|
47
|
+
});
|
|
48
|
+
const files = fast_glob.default.sync(globPattern, { cwd: baseDir });
|
|
49
|
+
const result = {};
|
|
50
|
+
for (const file of files) {
|
|
51
|
+
const { key, locale } = extractKeyAndLocaleFromPath(file, maskPattern, locales, defaultLocale);
|
|
52
|
+
const absolutePath = (0, node_path.isAbsolute)(file) ? file : (0, node_path.resolve)(baseDir, file);
|
|
53
|
+
if (!result[locale]) result[locale] = {};
|
|
54
|
+
result[locale][key] = absolutePath;
|
|
55
|
+
}
|
|
56
|
+
const hasKeyInMask = maskPattern.includes("{{__KEY__}}");
|
|
57
|
+
const discoveredKeys = /* @__PURE__ */ new Set();
|
|
58
|
+
for (const locale of Object.keys(result)) for (const key of Object.keys(result[locale] ?? {})) discoveredKeys.add(key);
|
|
59
|
+
if (!hasKeyInMask) discoveredKeys.add("index");
|
|
60
|
+
const keysToEnsure = discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];
|
|
61
|
+
for (const locale of locales) {
|
|
62
|
+
if (!result[locale]) result[locale] = {};
|
|
63
|
+
for (const key of keysToEnsure) if (!result[locale][key]) {
|
|
64
|
+
const builtPath = builder({
|
|
65
|
+
key,
|
|
66
|
+
locale
|
|
67
|
+
});
|
|
68
|
+
const absoluteBuiltPath = (0, node_path.isAbsolute)(builtPath) ? builtPath : (0, node_path.resolve)(baseDir, builtPath);
|
|
69
|
+
result[locale][key] = absoluteBuiltPath;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
};
|
|
74
|
+
const loadMessagePathMap = (source, configuration) => {
|
|
75
|
+
const messages = listMessages(source, configuration);
|
|
76
|
+
return Object.entries(messages).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
77
|
+
return {
|
|
78
|
+
path: (0, node_path.isAbsolute)(path) ? path : (0, node_path.resolve)(configuration.content.baseDir, path),
|
|
79
|
+
locale,
|
|
80
|
+
key
|
|
81
|
+
};
|
|
82
|
+
}));
|
|
83
|
+
};
|
|
84
|
+
const syncJSON = (options) => {
|
|
85
|
+
const { location, priority } = {
|
|
86
|
+
location: "plugin",
|
|
87
|
+
priority: 0,
|
|
88
|
+
...options
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
name: "sync-json",
|
|
92
|
+
loadDictionaries: async ({ configuration }) => {
|
|
93
|
+
const dictionariesMap = loadMessagePathMap(options.source, configuration);
|
|
94
|
+
let fill = options.source({
|
|
95
|
+
key: "{{key}}",
|
|
96
|
+
locale: "{{locale}}"
|
|
97
|
+
});
|
|
98
|
+
if (fill && !(0, node_path.isAbsolute)(fill)) fill = (0, node_path.join)(configuration.content.baseDir, fill);
|
|
99
|
+
const dictionaries = [];
|
|
100
|
+
for (const { locale, path, key } of dictionariesMap) {
|
|
101
|
+
const requireFunction = configuration.build?.require ?? (0, __intlayer_config.getProjectRequire)();
|
|
102
|
+
let json = {};
|
|
103
|
+
try {
|
|
104
|
+
json = requireFunction(path);
|
|
105
|
+
} catch {
|
|
106
|
+
json = {};
|
|
107
|
+
}
|
|
108
|
+
const filePath = (0, node_path.relative)(configuration.content.baseDir, path);
|
|
109
|
+
const dictionary = {
|
|
110
|
+
key,
|
|
111
|
+
locale,
|
|
112
|
+
fill,
|
|
113
|
+
localId: `${key}::${location}::${filePath}`,
|
|
114
|
+
location,
|
|
115
|
+
filled: locale !== configuration.internationalization.defaultLocale ? true : void 0,
|
|
116
|
+
content: json,
|
|
117
|
+
filePath,
|
|
118
|
+
priority
|
|
119
|
+
};
|
|
120
|
+
dictionaries.push(dictionary);
|
|
121
|
+
}
|
|
122
|
+
return dictionaries;
|
|
123
|
+
},
|
|
124
|
+
formatOutput: ({ dictionary }) => {
|
|
125
|
+
if (!dictionary.filePath || !dictionary.locale) return dictionary;
|
|
126
|
+
if ((0, node_path.resolve)(options.source({
|
|
127
|
+
key: dictionary.key,
|
|
128
|
+
locale: dictionary.locale
|
|
129
|
+
})) !== (0, node_path.resolve)(dictionary.filePath)) return dictionary;
|
|
130
|
+
return dictionary.content;
|
|
131
|
+
},
|
|
132
|
+
afterBuild: async ({ dictionaries, configuration }) => {
|
|
133
|
+
const { getLocalizedContent } = await import("@intlayer/core");
|
|
134
|
+
const { parallelize } = await import("@intlayer/chokidar");
|
|
135
|
+
const locales = configuration.internationalization.locales;
|
|
136
|
+
await parallelize(Object.entries(dictionaries.mergedDictionaries).flatMap(([key, dictionary]) => locales.map((locale) => ({
|
|
137
|
+
key,
|
|
138
|
+
dictionary: dictionary.dictionary,
|
|
139
|
+
locale
|
|
140
|
+
}))), async ({ key, dictionary, locale }) => {
|
|
141
|
+
const builderPath = options.source({
|
|
142
|
+
key,
|
|
143
|
+
locale
|
|
144
|
+
});
|
|
145
|
+
const localizedContent = getLocalizedContent(JSON.parse(JSON.stringify(dictionary.content)), locale, {
|
|
146
|
+
dictionaryKey: key,
|
|
147
|
+
keyPath: []
|
|
148
|
+
});
|
|
149
|
+
if (typeof localizedContent === "undefined" || typeof localizedContent === "object" && Object.keys(localizedContent).length === 0) return;
|
|
150
|
+
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(builderPath), { recursive: true });
|
|
151
|
+
await (0, node_fs_promises.writeFile)(builderPath, `${JSON.stringify(localizedContent, null, 2)}\n`, "utf-8");
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
//#endregion
|
|
158
|
+
exports.extractKeyAndLocaleFromPath = extractKeyAndLocaleFromPath;
|
|
159
|
+
exports.syncJSON = syncJSON;
|
|
160
|
+
//# sourceMappingURL=syncJSON.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncJSON.cjs","names":["locale: Locale | undefined","key: string | undefined","fg","result: MessagesRecord","messages: MessagesRecord","dictionariesMap: DictionariesMap","fill: string","dictionaries: Dictionary[]","json: JSONContent","dictionary: Dictionary"],"sources":["../../src/syncJSON.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, join, relative, resolve } from 'node:path';\nimport { getProjectRequire } from '@intlayer/config';\nimport type {\n ContentNode,\n Dictionary,\n IntlayerConfig,\n LocalDictionaryId,\n Locale,\n LocalesValues,\n Plugin,\n} from '@intlayer/types';\nimport fg from 'fast-glob';\n\ntype JSONContent = Record<string, any>;\n\ntype Builder = ({\n key,\n locale,\n}: {\n key: string;\n locale: LocalesValues | (string & {});\n}) => string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport const extractKeyAndLocaleFromPath = (\n filePath: string,\n maskPattern: string,\n locales: Locale[],\n defaultLocale: Locale\n) => {\n const keyPlaceholder = '{{__KEY__}}';\n const localePlaceholder = '{{__LOCALE__}}';\n\n const escapedMask = escapeRegex(maskPattern);\n const localesAlternation = locales.join('|');\n\n // Build a regex from the mask to capture locale (and key if present)\n let regexStr = `^${escapedMask}$`;\n\n regexStr = regexStr.replace(\n escapeRegex(localePlaceholder),\n `(?<locale>${localesAlternation})`\n );\n\n if (maskPattern.includes(keyPlaceholder)) {\n regexStr = regexStr.replace(escapeRegex(keyPlaceholder), '(?<key>[^/]+)');\n }\n\n const maskRegex = new RegExp(regexStr);\n\n const match = maskRegex.exec(filePath);\n\n let locale: Locale | undefined;\n let key: string | undefined;\n\n if (match?.groups) {\n locale = match.groups.locale as Locale | undefined;\n key = (match.groups.key as string | undefined) ?? 'index';\n }\n\n if (typeof key === 'undefined') {\n key = 'index';\n }\n\n if (typeof locale === 'undefined') {\n locale = defaultLocale;\n }\n\n return {\n key,\n locale,\n };\n};\n\nconst listMessages = (\n builder: Builder,\n configuration: IntlayerConfig\n): MessagesRecord => {\n const { content, internationalization } = configuration;\n\n const baseDir = content.baseDir;\n const locales = internationalization.locales;\n const defaultLocale = internationalization.defaultLocale;\n\n const localePattern = `{${locales.map((locale) => locale).join(',')}}`;\n\n const globPattern = builder({ key: '*', locale: localePattern });\n const maskPattern = builder({ key: '{{__KEY__}}', locale: '{{__LOCALE__}}' });\n\n const files = fg.sync(globPattern, {\n cwd: baseDir,\n });\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const file of files) {\n const { key, locale } = extractKeyAndLocaleFromPath(\n file,\n maskPattern,\n locales,\n defaultLocale\n );\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n if (!result[locale as Locale]) {\n result[locale as Locale] = {};\n }\n\n result[locale as Locale][key as Dictionary['key']] = absolutePath;\n }\n\n // Ensure all declared locales are present even if the file doesn't exist yet\n // Derive the list of keys from discovered files; if no key placeholder in mask, default to 'index'\n const hasKeyInMask = maskPattern.includes('{{__KEY__}}');\n const discoveredKeys = new Set<string>();\n\n for (const locale of Object.keys(result)) {\n for (const key of Object.keys(result[locale as Locale] ?? {})) {\n discoveredKeys.add(key);\n }\n }\n\n if (!hasKeyInMask) {\n discoveredKeys.add('index');\n }\n\n // If no keys were discovered and mask expects a key, we cannot infer keys.\n // In that case, do not fabricate unknown keys.\n const keysToEnsure =\n discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];\n\n for (const locale of locales) {\n if (!result[locale]) {\n result[locale] = {} as Record<Dictionary['key'], FilePath>;\n }\n\n for (const key of keysToEnsure) {\n if (!result[locale][key as Dictionary['key']]) {\n const builtPath = builder({ key, locale });\n const absoluteBuiltPath = isAbsolute(builtPath)\n ? builtPath\n : resolve(baseDir, builtPath);\n\n result[locale][key as Dictionary['key']] = absoluteBuiltPath;\n }\n }\n }\n\n return result;\n};\n\ntype FilePath = string;\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = (\n source: MessagesRecord | Builder,\n configuration: IntlayerConfig\n) => {\n const messages: MessagesRecord = listMessages(\n source as Builder,\n configuration\n );\n\n const dictionariesPathMap: DictionariesMap = Object.entries(messages).flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.content.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype SyncJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * ```ts\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * })\n * ```\n */\n source: Builder;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * ```ts\n * const config ={\n * plugins: [\n * syncJSON({\n * source: ({ key, locale }) => `./resources/${locale}/${key}.json`\n * location: 'plugin-i18next',\n * }),\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n};\n\nexport const syncJSON = (options: SyncJSONPluginOptions): Plugin => {\n const { location, priority } = {\n location: 'plugin',\n priority: 0,\n ...options,\n };\n\n return {\n name: 'sync-json',\n\n loadDictionaries: async ({ configuration }) => {\n const dictionariesMap: DictionariesMap = loadMessagePathMap(\n options.source,\n configuration\n );\n\n let fill: string = options.source({\n key: '{{key}}',\n locale: '{{locale}}',\n });\n\n if (fill && !isAbsolute(fill)) {\n fill = join(configuration.content.baseDir, fill);\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { locale, path, key } of dictionariesMap) {\n const requireFunction =\n configuration.build?.require ?? getProjectRequire();\n let json: JSONContent = {};\n try {\n json = requireFunction(path as string);\n } catch {\n // File does not exist yet; default to empty content so it can be filled later\n json = {};\n }\n\n const filePath = relative(configuration.content.baseDir, path);\n\n const dictionary: Dictionary = {\n key,\n locale,\n fill,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n locale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n\n formatOutput: ({ dictionary }) => {\n if (!dictionary.filePath || !dictionary.locale) return dictionary;\n\n const builderPath = options.source({\n key: dictionary.key,\n locale: dictionary.locale,\n });\n\n // It's not one of the JSON that we synchronize, don't modify it\n if (resolve(builderPath) !== resolve(dictionary.filePath)) {\n return dictionary;\n }\n\n return dictionary.content;\n },\n afterBuild: async ({ dictionaries, configuration }) => {\n // Dynamic import to avoid circular dependency as core package import config, that load esbuild, that load the config file, that load the plugin\n const { getLocalizedContent } = await import('@intlayer/core');\n const { parallelize } = await import('@intlayer/chokidar');\n\n const locales = configuration.internationalization.locales;\n\n type RecordList = {\n key: string;\n dictionary: Dictionary;\n locale: Locale;\n };\n\n const recordList: RecordList[] = Object.entries(\n dictionaries.mergedDictionaries\n ).flatMap(([key, dictionary]) =>\n locales.map((locale) => ({\n key,\n dictionary: dictionary.dictionary as Dictionary,\n locale,\n }))\n );\n\n await parallelize(recordList, async ({ key, dictionary, locale }) => {\n const builderPath = options.source({\n key,\n locale,\n });\n\n // Remove function, Symbol, etc. as it can be written as JSON\n const flatContent = JSON.parse(JSON.stringify(dictionary.content));\n\n const localizedContent = getLocalizedContent(\n flatContent as unknown as ContentNode,\n locale,\n {\n dictionaryKey: key,\n keyPath: [],\n }\n );\n\n // The file is empty, don't write it\n if (\n typeof localizedContent === 'undefined' ||\n (typeof localizedContent === 'object' &&\n Object.keys(localizedContent as Record<string, unknown>).length ===\n 0)\n )\n return;\n\n // Ensure directory exists before writing the file\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(localizedContent, null, 2);\n\n await writeFile(\n builderPath,\n `${stringContent}\\n`, // Add a new line at the end of the file to avoid formatting issues with VSCode\n 'utf-8'\n );\n });\n },\n };\n};\n"],"mappings":";;;;;;;;;;;AA0BA,MAAM,eAAe,QAAgB,IAAI,QAAQ,uBAAuB,OAAO;AAE/E,MAAa,+BACX,UACA,aACA,SACA,kBACG;CACH,MAAM,iBAAiB;CACvB,MAAM,oBAAoB;CAE1B,MAAM,cAAc,YAAY,YAAY;CAC5C,MAAM,qBAAqB,QAAQ,KAAK,IAAI;CAG5C,IAAI,WAAW,IAAI,YAAY;AAE/B,YAAW,SAAS,QAClB,YAAY,kBAAkB,EAC9B,aAAa,mBAAmB,GACjC;AAED,KAAI,YAAY,SAAS,eAAe,CACtC,YAAW,SAAS,QAAQ,YAAY,eAAe,EAAE,gBAAgB;CAK3E,MAAM,QAFY,IAAI,OAAO,SAAS,CAEd,KAAK,SAAS;CAEtC,IAAIA;CACJ,IAAIC;AAEJ,KAAI,OAAO,QAAQ;AACjB,WAAS,MAAM,OAAO;AACtB,QAAO,MAAM,OAAO,OAA8B;;AAGpD,KAAI,OAAO,QAAQ,YACjB,OAAM;AAGR,KAAI,OAAO,WAAW,YACpB,UAAS;AAGX,QAAO;EACL;EACA;EACD;;AAGH,MAAM,gBACJ,SACA,kBACmB;CACnB,MAAM,EAAE,SAAS,yBAAyB;CAE1C,MAAM,UAAU,QAAQ;CACxB,MAAM,UAAU,qBAAqB;CACrC,MAAM,gBAAgB,qBAAqB;CAI3C,MAAM,cAAc,QAAQ;EAAE,KAAK;EAAK,QAFlB,IAAI,QAAQ,KAAK,WAAW,OAAO,CAAC,KAAK,IAAI,CAAC;EAEL,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C,SAAS,CAAC,KAAK,gBACf,QAAQ,KAAK,YAAY;IACvB;IACA,YAAY,WAAW;IACvB;IACD,EAAE,CACJ,EAE6B,OAAO,EAAE,KAAK,YAAY,aAAa;IACnE,MAAM,cAAc,QAAQ,OAAO;KACjC;KACA;KACD,CAAC;IAKF,MAAM,mBAAmB,oBAFL,KAAK,MAAM,KAAK,UAAU,WAAW,QAAQ,CAAC,EAIhE,QACA;KACE,eAAe;KACf,SAAS,EAAE;KACZ,CACF;AAGD,QACE,OAAO,qBAAqB,eAC3B,OAAO,qBAAqB,YAC3B,OAAO,KAAK,iBAA4C,CAAC,WACvD,EAEJ;AAGF,6DAAoB,YAAY,EAAE,EAAE,WAAW,MAAM,CAAC;AAItD,0CACE,aACA,GAJoB,KAAK,UAAU,kBAAkB,MAAM,EAAE,CAI5C,KACjB,QACD;KACD;;EAEL"}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,151 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { getProjectRequire } from "@intlayer/config";
|
|
4
|
-
import fg from "fast-glob";
|
|
1
|
+
import { extractKeyAndLocaleFromPath, syncJSON } from "./syncJSON.mjs";
|
|
2
|
+
import { loadJSON } from "./loadJSON.mjs";
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
const escapeRegex = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
8
|
-
const extractKeyAndLocaleFromPath = (filePath, maskPattern, locales) => {
|
|
9
|
-
const keyPlaceholder = "{{__KEY__}}";
|
|
10
|
-
const localePlaceholder = "{{__LOCALE__}}";
|
|
11
|
-
const escapedMask = escapeRegex(maskPattern);
|
|
12
|
-
const localesAlternation = locales.join("|");
|
|
13
|
-
let regexStr = `^${escapedMask}$`;
|
|
14
|
-
regexStr = regexStr.replace(escapeRegex(localePlaceholder), `(?<locale>${localesAlternation})`);
|
|
15
|
-
if (maskPattern.includes(keyPlaceholder)) regexStr = regexStr.replace(escapeRegex(keyPlaceholder), "(?<key>[^/]+)");
|
|
16
|
-
const match = new RegExp(regexStr).exec(filePath);
|
|
17
|
-
let locale;
|
|
18
|
-
let key;
|
|
19
|
-
if (match?.groups) {
|
|
20
|
-
locale = match.groups.locale;
|
|
21
|
-
key = match.groups.key ?? "index";
|
|
22
|
-
}
|
|
23
|
-
return {
|
|
24
|
-
key,
|
|
25
|
-
locale
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
const listMessages = (builder, configuration) => {
|
|
29
|
-
const { content, internationalization } = configuration;
|
|
30
|
-
const baseDir = content.baseDir;
|
|
31
|
-
const locales = internationalization.locales;
|
|
32
|
-
const globPattern = builder({
|
|
33
|
-
key: "*",
|
|
34
|
-
locale: `{${locales.map((locale) => locale).join(",")}}`
|
|
35
|
-
});
|
|
36
|
-
const maskPattern = builder({
|
|
37
|
-
key: "{{__KEY__}}",
|
|
38
|
-
locale: "{{__LOCALE__}}"
|
|
39
|
-
});
|
|
40
|
-
const files = fg.sync(globPattern, { cwd: baseDir });
|
|
41
|
-
const result = {};
|
|
42
|
-
for (const file of files) {
|
|
43
|
-
const { key, locale } = extractKeyAndLocaleFromPath(file, maskPattern, locales);
|
|
44
|
-
const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);
|
|
45
|
-
if (!result[locale]) result[locale] = {};
|
|
46
|
-
result[locale][key] = absolutePath;
|
|
47
|
-
}
|
|
48
|
-
const hasKeyInMask = maskPattern.includes("{{__KEY__}}");
|
|
49
|
-
const discoveredKeys = /* @__PURE__ */ new Set();
|
|
50
|
-
for (const locale of Object.keys(result)) for (const key of Object.keys(result[locale] ?? {})) discoveredKeys.add(key);
|
|
51
|
-
if (!hasKeyInMask) discoveredKeys.add("index");
|
|
52
|
-
const keysToEnsure = discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];
|
|
53
|
-
for (const locale of locales) {
|
|
54
|
-
if (!result[locale]) result[locale] = {};
|
|
55
|
-
for (const key of keysToEnsure) if (!result[locale][key]) {
|
|
56
|
-
const builtPath = builder({
|
|
57
|
-
key,
|
|
58
|
-
locale
|
|
59
|
-
});
|
|
60
|
-
const absoluteBuiltPath = isAbsolute(builtPath) ? builtPath : resolve(baseDir, builtPath);
|
|
61
|
-
result[locale][key] = absoluteBuiltPath;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return result;
|
|
65
|
-
};
|
|
66
|
-
const loadMessagePathMap = (source, configuration) => {
|
|
67
|
-
const messages = listMessages(source, configuration);
|
|
68
|
-
return Object.entries(messages).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
69
|
-
return {
|
|
70
|
-
path: isAbsolute(path) ? path : resolve(configuration.content.baseDir, path),
|
|
71
|
-
locale,
|
|
72
|
-
key
|
|
73
|
-
};
|
|
74
|
-
}));
|
|
75
|
-
};
|
|
76
|
-
const syncJSON = (options) => {
|
|
77
|
-
const { location, priority } = {
|
|
78
|
-
location: "plugin",
|
|
79
|
-
priority: 0,
|
|
80
|
-
...options
|
|
81
|
-
};
|
|
82
|
-
return {
|
|
83
|
-
name: "sync-json",
|
|
84
|
-
loadDictionaries: async ({ configuration }) => {
|
|
85
|
-
const dictionariesMap = loadMessagePathMap(options.source, configuration);
|
|
86
|
-
let fill = options.source({
|
|
87
|
-
key: "{{key}}",
|
|
88
|
-
locale: "{{locale}}"
|
|
89
|
-
});
|
|
90
|
-
if (fill && !isAbsolute(fill)) fill = join(configuration.content.baseDir, fill);
|
|
91
|
-
const dictionaries = [];
|
|
92
|
-
for (const { locale, path, key } of dictionariesMap) {
|
|
93
|
-
const requireFunction = configuration.build?.require ?? getProjectRequire();
|
|
94
|
-
let json = {};
|
|
95
|
-
try {
|
|
96
|
-
json = requireFunction(path);
|
|
97
|
-
} catch {
|
|
98
|
-
json = {};
|
|
99
|
-
}
|
|
100
|
-
const filePath = relative(configuration.content.baseDir, path);
|
|
101
|
-
const dictionary = {
|
|
102
|
-
key,
|
|
103
|
-
locale,
|
|
104
|
-
fill,
|
|
105
|
-
localId: `${key}::${location}::${filePath}`,
|
|
106
|
-
location,
|
|
107
|
-
filled: locale !== configuration.internationalization.defaultLocale ? true : void 0,
|
|
108
|
-
content: json,
|
|
109
|
-
filePath,
|
|
110
|
-
priority
|
|
111
|
-
};
|
|
112
|
-
dictionaries.push(dictionary);
|
|
113
|
-
}
|
|
114
|
-
return dictionaries;
|
|
115
|
-
},
|
|
116
|
-
formatOutput: ({ dictionary }) => {
|
|
117
|
-
if (!dictionary.filePath || !dictionary.locale) return dictionary;
|
|
118
|
-
if (resolve(options.source({
|
|
119
|
-
key: dictionary.key,
|
|
120
|
-
locale: dictionary.locale
|
|
121
|
-
})) !== resolve(dictionary.filePath)) return dictionary;
|
|
122
|
-
return dictionary.content;
|
|
123
|
-
},
|
|
124
|
-
afterBuild: async ({ dictionaries, configuration }) => {
|
|
125
|
-
const { getLocalizedContent } = await import("@intlayer/core");
|
|
126
|
-
const { parallelize } = await import("@intlayer/chokidar");
|
|
127
|
-
const locales = configuration.internationalization.locales;
|
|
128
|
-
await parallelize(Object.entries(dictionaries.mergedDictionaries).flatMap(([key, dictionary]) => locales.map((locale) => ({
|
|
129
|
-
key,
|
|
130
|
-
dictionary: dictionary.dictionary,
|
|
131
|
-
locale
|
|
132
|
-
}))), async ({ key, dictionary, locale }) => {
|
|
133
|
-
const builderPath = options.source({
|
|
134
|
-
key,
|
|
135
|
-
locale
|
|
136
|
-
});
|
|
137
|
-
const localizedContent = getLocalizedContent(JSON.parse(JSON.stringify(dictionary.content)), locale, {
|
|
138
|
-
dictionaryKey: key,
|
|
139
|
-
keyPath: []
|
|
140
|
-
});
|
|
141
|
-
if (typeof localizedContent === "undefined" || typeof localizedContent === "object" && Object.keys(localizedContent).length === 0) return;
|
|
142
|
-
await mkdir(dirname(builderPath), { recursive: true });
|
|
143
|
-
await writeFile(builderPath, `${JSON.stringify(localizedContent, null, 2)}\n`, "utf-8");
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
//#endregion
|
|
150
|
-
export { syncJSON };
|
|
151
|
-
//# sourceMappingURL=index.mjs.map
|
|
4
|
+
export { extractKeyAndLocaleFromPath, loadJSON, syncJSON };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { extractKeyAndLocaleFromPath } from "./syncJSON.mjs";
|
|
2
|
+
import { isAbsolute, join, relative, resolve } from "node:path";
|
|
3
|
+
import { getProjectRequire } from "@intlayer/config";
|
|
4
|
+
import fg from "fast-glob";
|
|
5
|
+
|
|
6
|
+
//#region src/loadJSON.ts
|
|
7
|
+
const listMessages = (builder, configuration, selectedLocale) => {
|
|
8
|
+
const { content, internationalization } = configuration;
|
|
9
|
+
const baseDir = content.baseDir;
|
|
10
|
+
const locales = internationalization.locales;
|
|
11
|
+
const globPattern = builder({
|
|
12
|
+
key: "*",
|
|
13
|
+
locale: `{${locales.map((locale) => locale).join(",")}}`
|
|
14
|
+
});
|
|
15
|
+
const maskPattern = builder({
|
|
16
|
+
key: "{{__KEY__}}",
|
|
17
|
+
locale: "{{__LOCALE__}}"
|
|
18
|
+
});
|
|
19
|
+
const files = fg.sync(globPattern, { cwd: baseDir });
|
|
20
|
+
const result = {};
|
|
21
|
+
for (const file of files) {
|
|
22
|
+
const { key, locale } = extractKeyAndLocaleFromPath(file, maskPattern, locales, selectedLocale);
|
|
23
|
+
const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);
|
|
24
|
+
if (!result[locale]) result[locale] = {};
|
|
25
|
+
result[locale][key] = absolutePath;
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
const loadMessagePathMap = (source, configuration, selectedLocale) => {
|
|
30
|
+
const builder = source;
|
|
31
|
+
const messages = listMessages(builder, configuration, selectedLocale);
|
|
32
|
+
return (builder({
|
|
33
|
+
key: "{{__KEY__}}",
|
|
34
|
+
locale: "{{__LOCALE__}}"
|
|
35
|
+
}).includes("{{__LOCALE__}}") && selectedLocale ? Object.entries(messages).filter(([locale]) => locale === selectedLocale) : Object.entries(messages)).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
36
|
+
return {
|
|
37
|
+
path: isAbsolute(path) ? path : resolve(configuration.content.baseDir, path),
|
|
38
|
+
locale,
|
|
39
|
+
key
|
|
40
|
+
};
|
|
41
|
+
}));
|
|
42
|
+
};
|
|
43
|
+
const loadJSON = (options) => {
|
|
44
|
+
const { location, priority, locale } = {
|
|
45
|
+
location: "plugin",
|
|
46
|
+
priority: 0,
|
|
47
|
+
...options
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
name: "load-json",
|
|
51
|
+
loadDictionaries: async ({ configuration }) => {
|
|
52
|
+
const usedLocale = locale ?? configuration.internationalization.defaultLocale;
|
|
53
|
+
const dictionariesMap = loadMessagePathMap(options.source, configuration, usedLocale);
|
|
54
|
+
let filePath = options.source({ key: "{{key}}" });
|
|
55
|
+
if (filePath && !isAbsolute(filePath)) filePath = join(configuration.content.baseDir, filePath);
|
|
56
|
+
const dictionaries = [];
|
|
57
|
+
for (const { path, key } of dictionariesMap) {
|
|
58
|
+
const requireFunction = configuration.build?.require ?? getProjectRequire();
|
|
59
|
+
let json = {};
|
|
60
|
+
try {
|
|
61
|
+
json = requireFunction(path);
|
|
62
|
+
} catch {
|
|
63
|
+
json = {};
|
|
64
|
+
}
|
|
65
|
+
const filePath$1 = relative(configuration.content.baseDir, path);
|
|
66
|
+
const dictionary = {
|
|
67
|
+
key,
|
|
68
|
+
locale: usedLocale,
|
|
69
|
+
fill: filePath$1,
|
|
70
|
+
localId: `${key}::${location}::${filePath$1}`,
|
|
71
|
+
location,
|
|
72
|
+
filled: usedLocale !== configuration.internationalization.defaultLocale ? true : void 0,
|
|
73
|
+
content: json,
|
|
74
|
+
filePath: filePath$1,
|
|
75
|
+
priority
|
|
76
|
+
};
|
|
77
|
+
dictionaries.push(dictionary);
|
|
78
|
+
}
|
|
79
|
+
return dictionaries;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
export { loadJSON };
|
|
86
|
+
//# sourceMappingURL=loadJSON.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadJSON.mjs","names":["result: MessagesRecord","messages: MessagesRecord","dictionariesMap: DictionariesMap","filePath: string","dictionaries: Dictionary[]","json: JSONContent","filePath","dictionary: Dictionary"],"sources":["../../src/loadJSON.ts"],"sourcesContent":["import { isAbsolute, join, relative, resolve } from 'node:path';\nimport { getProjectRequire } from '@intlayer/config';\nimport type {\n Dictionary,\n IntlayerConfig,\n LocalDictionaryId,\n Locale,\n Plugin,\n} from '@intlayer/types';\nimport fg from 'fast-glob';\nimport { extractKeyAndLocaleFromPath } from './syncJSON';\n\ntype JSONContent = Record<string, any>;\n\ntype Builder = ({\n key,\n locale,\n}: {\n key: string;\n locale?: Locale | (string & {});\n}) => string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst listMessages = (\n builder: Builder,\n configuration: IntlayerConfig,\n selectedLocale: Locale\n): MessagesRecord => {\n const { content, internationalization } = configuration;\n\n const baseDir = content.baseDir;\n const locales = internationalization.locales;\n\n const localePattern = `{${locales.map((locale) => locale).join(',')}}`;\n\n const globPattern = builder({ key: '*', locale: localePattern });\n const maskPattern = builder({ key: '{{__KEY__}}', locale: '{{__LOCALE__}}' });\n\n const files = fg.sync(globPattern, {\n cwd: baseDir,\n });\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const file of files) {\n const { key, locale } = extractKeyAndLocaleFromPath(\n file,\n maskPattern,\n locales,\n selectedLocale\n );\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n if (!result[locale as Locale]) {\n result[locale as Locale] = {};\n }\n\n result[locale as Locale][key as Dictionary['key']] = absolutePath;\n }\n\n // For the load plugin we only use actual discovered files; do not fabricate\n // missing locales or keys, since we don't write outputs.\n return result;\n};\n\ntype FilePath = string;\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = (\n source: MessagesRecord | Builder,\n configuration: IntlayerConfig,\n selectedLocale: Locale\n) => {\n const builder = source as Builder;\n const messages: MessagesRecord = listMessages(\n builder,\n configuration,\n selectedLocale\n );\n\n const maskPattern = builder({\n key: '{{__KEY__}}',\n locale: '{{__LOCALE__}}',\n });\n const hasLocaleInMask = maskPattern.includes('{{__LOCALE__}}');\n\n const entries = (\n hasLocaleInMask && selectedLocale\n ? Object.entries(messages).filter(([locale]) => locale === selectedLocale)\n : Object.entries(messages)\n ) as [Locale, Record<Dictionary['key'], FilePath>][];\n\n const dictionariesPathMap: DictionariesMap = entries.flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.content.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype LoadJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * })\n * ```\n */\n source: Builder;\n\n /**\n * Locale\n *\n * If not provided, the plugin will consider the default locale.\n *\n * @example\n * ```ts\n * loadJSON({\n * source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,\n * locale: Locales.ENGLISH,\n * })\n * ```\n */\n locale?: Locale;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * @example\n * ```ts\n * const config = {\n * plugins: [\n * loadJSON({\n * source: ({ key }) => `./resources/${key}.json`,\n * location: 'plugin-i18next',\n * }),\n * loadJSON({\n * source: ({ key }) => `./messages/${key}.json`,\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n};\n\nexport const loadJSON = (options: LoadJSONPluginOptions): Plugin => {\n const { location, priority, locale } = {\n location: 'plugin',\n priority: 0,\n ...options,\n };\n\n return {\n name: 'load-json',\n\n loadDictionaries: async ({ configuration }) => {\n const usedLocale = (locale ??\n configuration.internationalization.defaultLocale) as Locale;\n\n const dictionariesMap: DictionariesMap = loadMessagePathMap(\n options.source,\n configuration,\n usedLocale\n );\n\n let filePath: string = options.source({\n key: '{{key}}',\n });\n\n if (filePath && !isAbsolute(filePath)) {\n filePath = join(configuration.content.baseDir, filePath);\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { path, key } of dictionariesMap) {\n const requireFunction =\n configuration.build?.require ?? getProjectRequire();\n let json: JSONContent = {};\n try {\n json = requireFunction(path as string);\n } catch {\n // File does not exist yet; default to empty content so it can be filled later\n json = {};\n }\n\n const filePath = relative(configuration.content.baseDir, path);\n\n const dictionary: Dictionary = {\n key,\n locale: usedLocale,\n fill: filePath,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n usedLocale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n };\n};\n"],"mappings":";;;;;;AAwBA,MAAM,gBACJ,SACA,eACA,mBACmB;CACnB,MAAM,EAAE,SAAS,yBAAyB;CAE1C,MAAM,UAAU,QAAQ;CACxB,MAAM,UAAU,qBAAqB;CAIrC,MAAM,cAAc,QAAQ;EAAE,KAAK;EAAK,QAFlB,IAAI,QAAQ,KAAK,WAAW,OAAO,CAAC,KAAK,IAAI,CAAC;EAEL,CAAC;CAChE,MAAM,cAAc,QAA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,EAAE;AAErC,QAAK,MAAM,EAAE,MAAM,SAAS,iBAAiB;IAC3C,MAAM,kBACJ,cAAc,OAAO,WAAW,mBAAmB;IACrD,IAAIC,OAAoB,EAAE;AAC1B,QAAI;AACF,YAAO,gBAAgB,KAAe;YAChC;AAEN,YAAO,EAAE;;IAGX,MAAMC,aAAW,SAAS,cAAc,QAAQ,SAAS,KAAK;IAE9D,MAAMC,aAAyB;KAC7B;KACA,QAAQ;KACR,MAAMD;KACN,SAAS,GAAG,IAAI,IAAI,SAAS,IAAIA;KACvB;KACV,QACE,eAAe,cAAc,qBAAqB,gBAC9C,OACA;KACN,SAAS;KACT;KACA;KACD;AAED,iBAAa,KAAK,WAAW;;AAG/B,UAAO;;EAEV"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
2
|
+
import { getProjectRequire } from "@intlayer/config";
|
|
3
|
+
import fg from "fast-glob";
|
|
4
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
5
|
+
|
|
6
|
+
//#region src/syncJSON.ts
|
|
7
|
+
const escapeRegex = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
8
|
+
const extractKeyAndLocaleFromPath = (filePath, maskPattern, locales, defaultLocale) => {
|
|
9
|
+
const keyPlaceholder = "{{__KEY__}}";
|
|
10
|
+
const localePlaceholder = "{{__LOCALE__}}";
|
|
11
|
+
const escapedMask = escapeRegex(maskPattern);
|
|
12
|
+
const localesAlternation = locales.join("|");
|
|
13
|
+
let regexStr = `^${escapedMask}$`;
|
|
14
|
+
regexStr = regexStr.replace(escapeRegex(localePlaceholder), `(?<locale>${localesAlternation})`);
|
|
15
|
+
if (maskPattern.includes(keyPlaceholder)) regexStr = regexStr.replace(escapeRegex(keyPlaceholder), "(?<key>[^/]+)");
|
|
16
|
+
const match = new RegExp(regexStr).exec(filePath);
|
|
17
|
+
let locale;
|
|
18
|
+
let key;
|
|
19
|
+
if (match?.groups) {
|
|
20
|
+
locale = match.groups.locale;
|
|
21
|
+
key = match.groups.key ?? "index";
|
|
22
|
+
}
|
|
23
|
+
if (typeof key === "undefined") key = "index";
|
|
24
|
+
if (typeof locale === "undefined") locale = defaultLocale;
|
|
25
|
+
return {
|
|
26
|
+
key,
|
|
27
|
+
locale
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const listMessages = (builder, configuration) => {
|
|
31
|
+
const { content, internationalization } = configuration;
|
|
32
|
+
const baseDir = content.baseDir;
|
|
33
|
+
const locales = internationalization.locales;
|
|
34
|
+
const defaultLocale = internationalization.defaultLocale;
|
|
35
|
+
const globPattern = builder({
|
|
36
|
+
key: "*",
|
|
37
|
+
locale: `{${locales.map((locale) => locale).join(",")}}`
|
|
38
|
+
});
|
|
39
|
+
const maskPattern = builder({
|
|
40
|
+
key: "{{__KEY__}}",
|
|
41
|
+
locale: "{{__LOCALE__}}"
|
|
42
|
+
});
|
|
43
|
+
const files = fg.sync(globPattern, { cwd: baseDir });
|
|
44
|
+
const result = {};
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
const { key, locale } = extractKeyAndLocaleFromPath(file, maskPattern, locales, defaultLocale);
|
|
47
|
+
const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);
|
|
48
|
+
if (!result[locale]) result[locale] = {};
|
|
49
|
+
result[locale][key] = absolutePath;
|
|
50
|
+
}
|
|
51
|
+
const hasKeyInMask = maskPattern.includes("{{__KEY__}}");
|
|
52
|
+
const discoveredKeys = /* @__PURE__ */ new Set();
|
|
53
|
+
for (const locale of Object.keys(result)) for (const key of Object.keys(result[locale] ?? {})) discoveredKeys.add(key);
|
|
54
|
+
if (!hasKeyInMask) discoveredKeys.add("index");
|
|
55
|
+
const keysToEnsure = discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];
|
|
56
|
+
for (const locale of locales) {
|
|
57
|
+
if (!result[locale]) result[locale] = {};
|
|
58
|
+
for (const key of keysToEnsure) if (!result[locale][key]) {
|
|
59
|
+
const builtPath = builder({
|
|
60
|
+
key,
|
|
61
|
+
locale
|
|
62
|
+
});
|
|
63
|
+
const absoluteBuiltPath = isAbsolute(builtPath) ? builtPath : resolve(baseDir, builtPath);
|
|
64
|
+
result[locale][key] = absoluteBuiltPath;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
};
|
|
69
|
+
const loadMessagePathMap = (source, configuration) => {
|
|
70
|
+
const messages = listMessages(source, configuration);
|
|
71
|
+
return Object.entries(messages).flatMap(([locale, keysRecord]) => Object.entries(keysRecord).map(([key, path]) => {
|
|
72
|
+
return {
|
|
73
|
+
path: isAbsolute(path) ? path : resolve(configuration.content.baseDir, path),
|
|
74
|
+
locale,
|
|
75
|
+
key
|
|
76
|
+
};
|
|
77
|
+
}));
|
|
78
|
+
};
|
|
79
|
+
const syncJSON = (options) => {
|
|
80
|
+
const { location, priority } = {
|
|
81
|
+
location: "plugin",
|
|
82
|
+
priority: 0,
|
|
83
|
+
...options
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
name: "sync-json",
|
|
87
|
+
loadDictionaries: async ({ configuration }) => {
|
|
88
|
+
const dictionariesMap = loadMessagePathMap(options.source, configuration);
|
|
89
|
+
let fill = options.source({
|
|
90
|
+
key: "{{key}}",
|
|
91
|
+
locale: "{{locale}}"
|
|
92
|
+
});
|
|
93
|
+
if (fill && !isAbsolute(fill)) fill = join(configuration.content.baseDir, fill);
|
|
94
|
+
const dictionaries = [];
|
|
95
|
+
for (const { locale, path, key } of dictionariesMap) {
|
|
96
|
+
const requireFunction = configuration.build?.require ?? getProjectRequire();
|
|
97
|
+
let json = {};
|
|
98
|
+
try {
|
|
99
|
+
json = requireFunction(path);
|
|
100
|
+
} catch {
|
|
101
|
+
json = {};
|
|
102
|
+
}
|
|
103
|
+
const filePath = relative(configuration.content.baseDir, path);
|
|
104
|
+
const dictionary = {
|
|
105
|
+
key,
|
|
106
|
+
locale,
|
|
107
|
+
fill,
|
|
108
|
+
localId: `${key}::${location}::${filePath}`,
|
|
109
|
+
location,
|
|
110
|
+
filled: locale !== configuration.internationalization.defaultLocale ? true : void 0,
|
|
111
|
+
content: json,
|
|
112
|
+
filePath,
|
|
113
|
+
priority
|
|
114
|
+
};
|
|
115
|
+
dictionaries.push(dictionary);
|
|
116
|
+
}
|
|
117
|
+
return dictionaries;
|
|
118
|
+
},
|
|
119
|
+
formatOutput: ({ dictionary }) => {
|
|
120
|
+
if (!dictionary.filePath || !dictionary.locale) return dictionary;
|
|
121
|
+
if (resolve(options.source({
|
|
122
|
+
key: dictionary.key,
|
|
123
|
+
locale: dictionary.locale
|
|
124
|
+
})) !== resolve(dictionary.filePath)) return dictionary;
|
|
125
|
+
return dictionary.content;
|
|
126
|
+
},
|
|
127
|
+
afterBuild: async ({ dictionaries, configuration }) => {
|
|
128
|
+
const { getLocalizedContent } = await import("@intlayer/core");
|
|
129
|
+
const { parallelize } = await import("@intlayer/chokidar");
|
|
130
|
+
const locales = configuration.internationalization.locales;
|
|
131
|
+
await parallelize(Object.entries(dictionaries.mergedDictionaries).flatMap(([key, dictionary]) => locales.map((locale) => ({
|
|
132
|
+
key,
|
|
133
|
+
dictionary: dictionary.dictionary,
|
|
134
|
+
locale
|
|
135
|
+
}))), async ({ key, dictionary, locale }) => {
|
|
136
|
+
const builderPath = options.source({
|
|
137
|
+
key,
|
|
138
|
+
locale
|
|
139
|
+
});
|
|
140
|
+
const localizedContent = getLocalizedContent(JSON.parse(JSON.stringify(dictionary.content)), locale, {
|
|
141
|
+
dictionaryKey: key,
|
|
142
|
+
keyPath: []
|
|
143
|
+
});
|
|
144
|
+
if (typeof localizedContent === "undefined" || typeof localizedContent === "object" && Object.keys(localizedContent).length === 0) return;
|
|
145
|
+
await mkdir(dirname(builderPath), { recursive: true });
|
|
146
|
+
await writeFile(builderPath, `${JSON.stringify(localizedContent, null, 2)}\n`, "utf-8");
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
//#endregion
|
|
153
|
+
export { extractKeyAndLocaleFromPath, syncJSON };
|
|
154
|
+
//# sourceMappingURL=syncJSON.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncJSON.mjs","names":["locale: Locale | undefined","key: string | undefined","result: MessagesRecord","messages: MessagesRecord","dictionariesMap: DictionariesMap","fill: string","dictionaries: Dictionary[]","json: JSONContent","dictionary: Dictionary"],"sources":["../../src/syncJSON.ts"],"sourcesContent":["import { mkdir, writeFile } from 'node:fs/promises';\nimport { dirname, isAbsolute, join, relative, resolve } from 'node:path';\nimport { getProjectRequire } from '@intlayer/config';\nimport type {\n ContentNode,\n Dictionary,\n IntlayerConfig,\n LocalDictionaryId,\n Locale,\n LocalesValues,\n Plugin,\n} from '@intlayer/types';\nimport fg from 'fast-glob';\n\ntype JSONContent = Record<string, any>;\n\ntype Builder = ({\n key,\n locale,\n}: {\n key: string;\n locale: LocalesValues | (string & {});\n}) => string;\n\ntype MessagesRecord = Record<Locale, Record<Dictionary['key'], FilePath>>;\n\nconst escapeRegex = (str: string) => str.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\nexport const extractKeyAndLocaleFromPath = (\n filePath: string,\n maskPattern: string,\n locales: Locale[],\n defaultLocale: Locale\n) => {\n const keyPlaceholder = '{{__KEY__}}';\n const localePlaceholder = '{{__LOCALE__}}';\n\n const escapedMask = escapeRegex(maskPattern);\n const localesAlternation = locales.join('|');\n\n // Build a regex from the mask to capture locale (and key if present)\n let regexStr = `^${escapedMask}$`;\n\n regexStr = regexStr.replace(\n escapeRegex(localePlaceholder),\n `(?<locale>${localesAlternation})`\n );\n\n if (maskPattern.includes(keyPlaceholder)) {\n regexStr = regexStr.replace(escapeRegex(keyPlaceholder), '(?<key>[^/]+)');\n }\n\n const maskRegex = new RegExp(regexStr);\n\n const match = maskRegex.exec(filePath);\n\n let locale: Locale | undefined;\n let key: string | undefined;\n\n if (match?.groups) {\n locale = match.groups.locale as Locale | undefined;\n key = (match.groups.key as string | undefined) ?? 'index';\n }\n\n if (typeof key === 'undefined') {\n key = 'index';\n }\n\n if (typeof locale === 'undefined') {\n locale = defaultLocale;\n }\n\n return {\n key,\n locale,\n };\n};\n\nconst listMessages = (\n builder: Builder,\n configuration: IntlayerConfig\n): MessagesRecord => {\n const { content, internationalization } = configuration;\n\n const baseDir = content.baseDir;\n const locales = internationalization.locales;\n const defaultLocale = internationalization.defaultLocale;\n\n const localePattern = `{${locales.map((locale) => locale).join(',')}}`;\n\n const globPattern = builder({ key: '*', locale: localePattern });\n const maskPattern = builder({ key: '{{__KEY__}}', locale: '{{__LOCALE__}}' });\n\n const files = fg.sync(globPattern, {\n cwd: baseDir,\n });\n\n const result: MessagesRecord = {} as MessagesRecord;\n\n for (const file of files) {\n const { key, locale } = extractKeyAndLocaleFromPath(\n file,\n maskPattern,\n locales,\n defaultLocale\n );\n\n const absolutePath = isAbsolute(file) ? file : resolve(baseDir, file);\n\n if (!result[locale as Locale]) {\n result[locale as Locale] = {};\n }\n\n result[locale as Locale][key as Dictionary['key']] = absolutePath;\n }\n\n // Ensure all declared locales are present even if the file doesn't exist yet\n // Derive the list of keys from discovered files; if no key placeholder in mask, default to 'index'\n const hasKeyInMask = maskPattern.includes('{{__KEY__}}');\n const discoveredKeys = new Set<string>();\n\n for (const locale of Object.keys(result)) {\n for (const key of Object.keys(result[locale as Locale] ?? {})) {\n discoveredKeys.add(key);\n }\n }\n\n if (!hasKeyInMask) {\n discoveredKeys.add('index');\n }\n\n // If no keys were discovered and mask expects a key, we cannot infer keys.\n // In that case, do not fabricate unknown keys.\n const keysToEnsure =\n discoveredKeys.size > 0 ? Array.from(discoveredKeys) : [];\n\n for (const locale of locales) {\n if (!result[locale]) {\n result[locale] = {} as Record<Dictionary['key'], FilePath>;\n }\n\n for (const key of keysToEnsure) {\n if (!result[locale][key as Dictionary['key']]) {\n const builtPath = builder({ key, locale });\n const absoluteBuiltPath = isAbsolute(builtPath)\n ? builtPath\n : resolve(baseDir, builtPath);\n\n result[locale][key as Dictionary['key']] = absoluteBuiltPath;\n }\n }\n }\n\n return result;\n};\n\ntype FilePath = string;\n\ntype DictionariesMap = { path: string; locale: Locale; key: string }[];\n\nconst loadMessagePathMap = (\n source: MessagesRecord | Builder,\n configuration: IntlayerConfig\n) => {\n const messages: MessagesRecord = listMessages(\n source as Builder,\n configuration\n );\n\n const dictionariesPathMap: DictionariesMap = Object.entries(messages).flatMap(\n ([locale, keysRecord]) =>\n Object.entries(keysRecord).map(([key, path]) => {\n const absolutePath = isAbsolute(path)\n ? path\n : resolve(configuration.content.baseDir, path);\n\n return {\n path: absolutePath,\n locale,\n key,\n } as DictionariesMap[number];\n })\n );\n\n return dictionariesPathMap;\n};\n\ntype SyncJSONPluginOptions = {\n /**\n * The source of the plugin.\n * Is a function to build the source from the key and locale.\n *\n * ```ts\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * })\n * ```\n */\n source: Builder;\n\n /**\n * Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.\n * Used to identify the plugin in the dictionary.\n *\n * In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.\n *\n * ```ts\n * const config ={\n * plugins: [\n * syncJSON({\n * source: ({ key, locale }) => `./resources/${locale}/${key}.json`\n * location: 'plugin-i18next',\n * }),\n * syncJSON({\n * source: ({ key, locale }) => `./messages/${locale}/${key}.json`\n * location: 'plugin-next-intl',\n * }),\n * ]\n * }\n * ```\n */\n location?: string;\n\n /**\n * The priority of the dictionaries created by the plugin.\n *\n * In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.\n *\n * Default is -1. (.content file priority is 0)\n *\n */\n priority?: number;\n};\n\nexport const syncJSON = (options: SyncJSONPluginOptions): Plugin => {\n const { location, priority } = {\n location: 'plugin',\n priority: 0,\n ...options,\n };\n\n return {\n name: 'sync-json',\n\n loadDictionaries: async ({ configuration }) => {\n const dictionariesMap: DictionariesMap = loadMessagePathMap(\n options.source,\n configuration\n );\n\n let fill: string = options.source({\n key: '{{key}}',\n locale: '{{locale}}',\n });\n\n if (fill && !isAbsolute(fill)) {\n fill = join(configuration.content.baseDir, fill);\n }\n\n const dictionaries: Dictionary[] = [];\n\n for (const { locale, path, key } of dictionariesMap) {\n const requireFunction =\n configuration.build?.require ?? getProjectRequire();\n let json: JSONContent = {};\n try {\n json = requireFunction(path as string);\n } catch {\n // File does not exist yet; default to empty content so it can be filled later\n json = {};\n }\n\n const filePath = relative(configuration.content.baseDir, path);\n\n const dictionary: Dictionary = {\n key,\n locale,\n fill,\n localId: `${key}::${location}::${filePath}` as LocalDictionaryId,\n location: location as Dictionary['location'],\n filled:\n locale !== configuration.internationalization.defaultLocale\n ? true\n : undefined,\n content: json,\n filePath,\n priority,\n };\n\n dictionaries.push(dictionary);\n }\n\n return dictionaries;\n },\n\n formatOutput: ({ dictionary }) => {\n if (!dictionary.filePath || !dictionary.locale) return dictionary;\n\n const builderPath = options.source({\n key: dictionary.key,\n locale: dictionary.locale,\n });\n\n // It's not one of the JSON that we synchronize, don't modify it\n if (resolve(builderPath) !== resolve(dictionary.filePath)) {\n return dictionary;\n }\n\n return dictionary.content;\n },\n afterBuild: async ({ dictionaries, configuration }) => {\n // Dynamic import to avoid circular dependency as core package import config, that load esbuild, that load the config file, that load the plugin\n const { getLocalizedContent } = await import('@intlayer/core');\n const { parallelize } = await import('@intlayer/chokidar');\n\n const locales = configuration.internationalization.locales;\n\n type RecordList = {\n key: string;\n dictionary: Dictionary;\n locale: Locale;\n };\n\n const recordList: RecordList[] = Object.entries(\n dictionaries.mergedDictionaries\n ).flatMap(([key, dictionary]) =>\n locales.map((locale) => ({\n key,\n dictionary: dictionary.dictionary as Dictionary,\n locale,\n }))\n );\n\n await parallelize(recordList, async ({ key, dictionary, locale }) => {\n const builderPath = options.source({\n key,\n locale,\n });\n\n // Remove function, Symbol, etc. as it can be written as JSON\n const flatContent = JSON.parse(JSON.stringify(dictionary.content));\n\n const localizedContent = getLocalizedContent(\n flatContent as unknown as ContentNode,\n locale,\n {\n dictionaryKey: key,\n keyPath: [],\n }\n );\n\n // The file is empty, don't write it\n if (\n typeof localizedContent === 'undefined' ||\n (typeof localizedContent === 'object' &&\n Object.keys(localizedContent as Record<string, unknown>).length ===\n 0)\n )\n return;\n\n // Ensure directory exists before writing the file\n await mkdir(dirname(builderPath), { recursive: true });\n\n const stringContent = JSON.stringify(localizedContent, null, 2);\n\n await writeFile(\n builderPath,\n `${stringContent}\\n`, // Add a new line at the end of the file to avoid formatting issues with VSCode\n 'utf-8'\n );\n });\n },\n };\n};\n"],"mappings":";;;;;;AA0BA,MAAM,eAAe,QAAgB,IAAI,QAAQ,uBAAuB,OAAO;AAE/E,MAAa,+BACX,UACA,aACA,SACA,kBACG;CACH,MAAM,iBAAiB;CACvB,MAAM,oBAAoB;CAE1B,MAAM,cAAc,YAAY,YAAY;CAC5C,MAAM,qBAAqB,QAAQ,KAAK,IAAI;CAG5C,IAAI,WAAW,IAAI,YAAY;AAE/B,YAAW,SAAS,QAClB,YAAY,kBAAkB,EAC9B,aAAa,mBAAmB,GACjC;AAED,KAAI,YAAY,SAAS,eAAe,CACtC,YAAW,SAAS,QAAQ,YAAY,eAAe,EAAE,gBAAgB;CAK3E,MAAM,QAFY,IAAI,OAAO,SAAS,CAEd,KAAK,SAAS;CAEtC,IAAIA;CACJ,IAAIC;AAEJ,KAAI,OAAO,QAAQ;AACjB,WAAS,MAAM,OAAO;AACtB,QAAO,MAAM,OAAO,OAA8B;;AAGpD,KAAI,OAAO,QAAQ,YACjB,OAAM;AAGR,KAAI,OAAO,WAAW,YACpB,UAAS;AAGX,QAAO;EACL;EACA;EACD;;AAGH,MAAM,gBACJ,SACA,kBACmB;CACnB,MAAM,EAAE,SAAS,yBAAyB;CAE1C,MAAM,UAAU,QAAQ;CACxB,MAAM,UAAU,qBAAqB;CACrC,MAAM,gBAAgB,qBAAqB;CAI3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AAoB;GAErD,MAAM,EAAE,wBAAwB,MAAM,OAAO;GAC7C,MAAM,EAAE,gBAAgB,MAAM,OAAO;GAErC,MAAM,UAAU,cAAc,qBAAqB;AAkBnD,SAAM,YAV2B,OAAO,QACtC,aAAa,mBACd,CAAC,SAAS,CAAC,KAAK,gBACf,QAAQ,KAAK,YAAY;IACvB;IACA,YAAY,WAAW;IACvB;IACD,EAAE,CACJ,EAE6B,OAAO,EAAE,KAAK,YAAY,aAAa;IACnE,MAAM,cAAc,QAAQ,OAAO;KACjC;KACA;KACD,CAAC;IAKF,MAAM,mBAAmB,oBAFL,KAAK,MAAM,KAAK,UAAU,WAAW,QAAQ,CAAC,EAIhE,QACA;KACE,eAAe;KACf,SAAS,EAAE;KACZ,CACF;AAGD,QACE,OAAO,qBAAqB,eAC3B,OAAO,qBAAqB,YAC3B,OAAO,KAAK,iBAA4C,CAAC,WACvD,EAEJ;AAGF,UAAM,MAAM,QAAQ,YAAY,EAAE,EAAE,WAAW,MAAM,CAAC;AAItD,UAAM,UACJ,aACA,GAJoB,KAAK,UAAU,kBAAkB,MAAM,EAAE,CAI5C,KACjB,QACD;KACD;;EAEL"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,58 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type Builder = ({
|
|
5
|
-
key,
|
|
6
|
-
locale
|
|
7
|
-
}: {
|
|
8
|
-
key: string;
|
|
9
|
-
locale: LocalesValues | (string & {});
|
|
10
|
-
}) => string;
|
|
11
|
-
type SyncJSONPluginOptions = {
|
|
12
|
-
/**
|
|
13
|
-
* The source of the plugin.
|
|
14
|
-
* Is a function to build the source from the key and locale.
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* syncJSON({
|
|
18
|
-
* source: ({ key, locale }) => `./messages/${locale}/${key}.json`
|
|
19
|
-
* })
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
source: Builder;
|
|
23
|
-
/**
|
|
24
|
-
* Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.
|
|
25
|
-
* Used to identify the plugin in the dictionary.
|
|
26
|
-
*
|
|
27
|
-
* In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.
|
|
28
|
-
*
|
|
29
|
-
* ```ts
|
|
30
|
-
* const config ={
|
|
31
|
-
* plugins: [
|
|
32
|
-
* syncJSON({
|
|
33
|
-
* source: ({ key, locale }) => `./resources/${locale}/${key}.json`
|
|
34
|
-
* location: 'plugin-i18next',
|
|
35
|
-
* }),
|
|
36
|
-
* syncJSON({
|
|
37
|
-
* source: ({ key, locale }) => `./messages/${locale}/${key}.json`
|
|
38
|
-
* location: 'plugin-next-intl',
|
|
39
|
-
* }),
|
|
40
|
-
* ]
|
|
41
|
-
* }
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
location?: string;
|
|
45
|
-
/**
|
|
46
|
-
* The priority of the dictionaries created by the plugin.
|
|
47
|
-
*
|
|
48
|
-
* In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.
|
|
49
|
-
*
|
|
50
|
-
* Default is -1. (.content file priority is 0)
|
|
51
|
-
*
|
|
52
|
-
*/
|
|
53
|
-
priority?: number;
|
|
54
|
-
};
|
|
55
|
-
declare const syncJSON: (options: SyncJSONPluginOptions) => Plugin;
|
|
56
|
-
//#endregion
|
|
57
|
-
export { syncJSON };
|
|
58
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
import { loadJSON } from "./loadJSON.js";
|
|
2
|
+
import { extractKeyAndLocaleFromPath, syncJSON } from "./syncJSON.js";
|
|
3
|
+
export { extractKeyAndLocaleFromPath, loadJSON, syncJSON };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Locale, Plugin } from "@intlayer/types";
|
|
2
|
+
|
|
3
|
+
//#region src/loadJSON.d.ts
|
|
4
|
+
type Builder = ({
|
|
5
|
+
key,
|
|
6
|
+
locale
|
|
7
|
+
}: {
|
|
8
|
+
key: string;
|
|
9
|
+
locale?: Locale | (string & {});
|
|
10
|
+
}) => string;
|
|
11
|
+
type LoadJSONPluginOptions = {
|
|
12
|
+
/**
|
|
13
|
+
* The source of the plugin.
|
|
14
|
+
* Is a function to build the source from the key and locale.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* loadJSON({
|
|
19
|
+
* source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,
|
|
20
|
+
* })
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
source: Builder;
|
|
24
|
+
/**
|
|
25
|
+
* Locale
|
|
26
|
+
*
|
|
27
|
+
* If not provided, the plugin will consider the default locale.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* loadJSON({
|
|
32
|
+
* source: ({ key }) => `blog/${'**'}/${key}.i18n.json`,
|
|
33
|
+
* locale: Locales.ENGLISH,
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
locale?: Locale;
|
|
38
|
+
/**
|
|
39
|
+
* Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.
|
|
40
|
+
* Used to identify the plugin in the dictionary.
|
|
41
|
+
*
|
|
42
|
+
* In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const config = {
|
|
47
|
+
* plugins: [
|
|
48
|
+
* loadJSON({
|
|
49
|
+
* source: ({ key }) => `./resources/${key}.json`,
|
|
50
|
+
* location: 'plugin-i18next',
|
|
51
|
+
* }),
|
|
52
|
+
* loadJSON({
|
|
53
|
+
* source: ({ key }) => `./messages/${key}.json`,
|
|
54
|
+
* location: 'plugin-next-intl',
|
|
55
|
+
* }),
|
|
56
|
+
* ]
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
location?: string;
|
|
61
|
+
/**
|
|
62
|
+
* The priority of the dictionaries created by the plugin.
|
|
63
|
+
*
|
|
64
|
+
* In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.
|
|
65
|
+
*
|
|
66
|
+
* Default is -1. (.content file priority is 0)
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
69
|
+
priority?: number;
|
|
70
|
+
};
|
|
71
|
+
declare const loadJSON: (options: LoadJSONPluginOptions) => Plugin;
|
|
72
|
+
//#endregion
|
|
73
|
+
export { loadJSON };
|
|
74
|
+
//# sourceMappingURL=loadJSON.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadJSON.d.ts","names":[],"sources":["../../src/loadJSON.ts"],"sourcesContent":[],"mappings":";;;KAcK,OAAA;;;;;EAAA,MAAA,CAAA,EAKM,MALC,GAAA,CAAA,MAAA,GAAA,CAAA,CAAA,CAAA;CACV,EAAA,GAAA,MAAA;KAkGG,qBAAA,GAjGH;EAGS;;AAAM;AA8JjB;;;;;;;;UApDU;;;;;;;;;;;;;;WAeC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAqCE,oBAAqB,0BAAwB"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Locale, LocalesValues, Plugin } from "@intlayer/types";
|
|
2
|
+
|
|
3
|
+
//#region src/syncJSON.d.ts
|
|
4
|
+
type Builder = ({
|
|
5
|
+
key,
|
|
6
|
+
locale
|
|
7
|
+
}: {
|
|
8
|
+
key: string;
|
|
9
|
+
locale: LocalesValues | (string & {});
|
|
10
|
+
}) => string;
|
|
11
|
+
declare const extractKeyAndLocaleFromPath: (filePath: string, maskPattern: string, locales: Locale[], defaultLocale: Locale) => {
|
|
12
|
+
key: string;
|
|
13
|
+
locale: Locale;
|
|
14
|
+
};
|
|
15
|
+
type SyncJSONPluginOptions = {
|
|
16
|
+
/**
|
|
17
|
+
* The source of the plugin.
|
|
18
|
+
* Is a function to build the source from the key and locale.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* syncJSON({
|
|
22
|
+
* source: ({ key, locale }) => `./messages/${locale}/${key}.json`
|
|
23
|
+
* })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
source: Builder;
|
|
27
|
+
/**
|
|
28
|
+
* Because Intlayer transform the JSON files into Dictionary, we need to identify the plugin in the dictionary.
|
|
29
|
+
* Used to identify the plugin in the dictionary.
|
|
30
|
+
*
|
|
31
|
+
* In the case you have multiple plugins, you can use this to identify the plugin in the dictionary.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const config ={
|
|
35
|
+
* plugins: [
|
|
36
|
+
* syncJSON({
|
|
37
|
+
* source: ({ key, locale }) => `./resources/${locale}/${key}.json`
|
|
38
|
+
* location: 'plugin-i18next',
|
|
39
|
+
* }),
|
|
40
|
+
* syncJSON({
|
|
41
|
+
* source: ({ key, locale }) => `./messages/${locale}/${key}.json`
|
|
42
|
+
* location: 'plugin-next-intl',
|
|
43
|
+
* }),
|
|
44
|
+
* ]
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
location?: string;
|
|
49
|
+
/**
|
|
50
|
+
* The priority of the dictionaries created by the plugin.
|
|
51
|
+
*
|
|
52
|
+
* In the case of conflicts with remote dictionaries, or .content files, the dictionary with the highest priority will override the other dictionaries.
|
|
53
|
+
*
|
|
54
|
+
* Default is -1. (.content file priority is 0)
|
|
55
|
+
*
|
|
56
|
+
*/
|
|
57
|
+
priority?: number;
|
|
58
|
+
};
|
|
59
|
+
declare const syncJSON: (options: SyncJSONPluginOptions) => Plugin;
|
|
60
|
+
//#endregion
|
|
61
|
+
export { extractKeyAndLocaleFromPath, syncJSON };
|
|
62
|
+
//# sourceMappingURL=syncJSON.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"syncJSON.d.ts","names":[],"sources":["../../src/syncJSON.ts"],"sourcesContent":[],"mappings":";;;KAgBK,OAAA;;;;;EAAA,MAAA,EAKK,aALE,GAAA,CAAA,MAAA,GAAA,CAAA,CAAA,CAAA;CACV,EAAA,GAAA,MAAA;AACA,cAUW,2BAVX,EAAA,CAAA,QAAA,EAAA,MAAA,EAAA,WAAA,EAAA,MAAA,EAAA,OAAA,EAaS,MAbT,EAAA,EAAA,aAAA,EAce,MAdf,EAAA,GAAA;EAGQ,GAAA,EAAA,MAAA;EAAa,MAAA,QAAA;AAOvB,CAAA;KA+JK,qBAAA,GA5JM;EACM;;;AA4Cf;AA8JF;;;;;;UApCU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAoCG,oBAAqB,0BAAwB"}
|