gtx-cli 2.0.11 → 2.0.13
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/CHANGELOG.md +12 -0
- package/dist/config/optionPresets.js +15 -16
- package/dist/formats/json/flattenJson.js +1 -1
- package/dist/formats/json/mergeJson.d.ts +7 -0
- package/dist/formats/json/mergeJson.js +104 -56
- package/dist/formats/json/parseJson.js +37 -26
- package/dist/formats/json/utils.d.ts +24 -3
- package/dist/formats/json/utils.js +34 -9
- package/dist/react/jsx/utils/parseStringFunction.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# gtx-cli
|
|
2
2
|
|
|
3
|
+
## 2.0.13
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#505](https://github.com/generaltranslation/gt/pull/505) [`e8c5650`](https://github.com/generaltranslation/gt/commit/e8c5650c163119301d2f9d6b946c0ed8383c57e1) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - fix: json translation for composite arrays where there are multiple default locale items
|
|
8
|
+
|
|
9
|
+
## 2.0.12
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#503](https://github.com/generaltranslation/gt/pull/503) [`9549d88`](https://github.com/generaltranslation/gt/commit/9549d88485af4dc57fb19847016d53aa3375b380) Thanks [@brian-lou](https://github.com/brian-lou)! - Fix path resolution mechanism for useGT/getGT usage scanning
|
|
14
|
+
|
|
3
15
|
## 2.0.11
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -21,22 +21,21 @@ export function generatePreset(preset) {
|
|
|
21
21
|
},
|
|
22
22
|
},
|
|
23
23
|
},
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// },
|
|
24
|
+
'$.redirects': {
|
|
25
|
+
type: 'array',
|
|
26
|
+
key: '$.language',
|
|
27
|
+
include: [],
|
|
28
|
+
transform: {
|
|
29
|
+
'$.source': {
|
|
30
|
+
match: '^/{locale}/(.*)$',
|
|
31
|
+
replace: '/{locale}/$1',
|
|
32
|
+
},
|
|
33
|
+
'$.destination': {
|
|
34
|
+
match: '^/{locale}/(.*)$',
|
|
35
|
+
replace: '/{locale}/$1',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
40
39
|
},
|
|
41
40
|
};
|
|
42
41
|
default:
|
|
@@ -3,4 +3,11 @@ export declare function mergeJson(originalContent: string, filePath: string, opt
|
|
|
3
3
|
translatedContent: string;
|
|
4
4
|
targetLocale: string;
|
|
5
5
|
}[], defaultLocale: string): string[];
|
|
6
|
+
/**
|
|
7
|
+
* Apply transformations to the sourceItem in-place
|
|
8
|
+
* @param sourceItem - The source item to apply transformations to
|
|
9
|
+
* @param transform - The transformations to apply
|
|
10
|
+
* @param targetLocale - The target locale
|
|
11
|
+
* @param defaultLocale - The default locale
|
|
12
|
+
*/
|
|
6
13
|
export declare function applyTransformations(sourceItem: any, transform: SourceObjectOptions['transform'], targetLocale: string, defaultLocale: string): void;
|
|
@@ -12,7 +12,7 @@ export function mergeJson(originalContent, filePath, options, targets, defaultLo
|
|
|
12
12
|
try {
|
|
13
13
|
originalJson = JSON.parse(originalContent);
|
|
14
14
|
}
|
|
15
|
-
catch
|
|
15
|
+
catch {
|
|
16
16
|
logError(`Invalid JSON file: ${filePath}`);
|
|
17
17
|
exit(1);
|
|
18
18
|
}
|
|
@@ -55,57 +55,85 @@ export function mergeJson(originalContent, filePath, options, targets, defaultLo
|
|
|
55
55
|
exit(1);
|
|
56
56
|
}
|
|
57
57
|
// Get source item for default locale
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
60
|
-
logError(`Matching
|
|
58
|
+
const matchingDefaultLocaleItems = findMatchingItemArray(defaultLocale, sourceObjectOptions, sourceObjectPointer, sourceObjectValue);
|
|
59
|
+
if (!Object.keys(matchingDefaultLocaleItems).length) {
|
|
60
|
+
logError(`Matching sourceItems not found at path: ${sourceObjectPointer} for locale: ${defaultLocale}. Please check your JSON schema`);
|
|
61
61
|
exit(1);
|
|
62
62
|
}
|
|
63
|
-
const
|
|
63
|
+
const matchingDefaultLocaleItemKeys = new Set(Object.keys(matchingDefaultLocaleItems));
|
|
64
64
|
// For each target:
|
|
65
|
-
// 1.
|
|
66
|
-
// 2.
|
|
67
|
-
// 3.
|
|
68
|
-
// 4.
|
|
69
|
-
//
|
|
65
|
+
// 1. Get the target items
|
|
66
|
+
// 2. Track all array indecies to remove (will be overwritten)
|
|
67
|
+
// 3. Merge matchingDefaultLocaleItems and targetItems
|
|
68
|
+
// 4. Validate that the mergedItems is not empty
|
|
69
|
+
// For each target item:
|
|
70
|
+
// 5. Validate that all the array indecies are still present in the source json
|
|
71
|
+
// 6. Override the source item with the translated values
|
|
72
|
+
// 7. Apply additional mutations to the sourceItem
|
|
73
|
+
// 8. Track all items to add
|
|
74
|
+
// 9. Check that items to add is >= items to remove
|
|
75
|
+
// 10. Remove all items for the target locale (they can be identified by the key)
|
|
76
|
+
const indiciesToRemove = new Set();
|
|
77
|
+
const itemsToAdd = [];
|
|
70
78
|
for (const target of targets) {
|
|
71
79
|
const targetJson = JSON.parse(target.translatedContent);
|
|
72
|
-
|
|
73
|
-
|
|
80
|
+
let targetItems = targetJson[sourceObjectPointer];
|
|
81
|
+
// 1. Get the target items
|
|
82
|
+
if (!targetItems) {
|
|
83
|
+
// If no translation can be found, a transformation may need to happen still
|
|
84
|
+
targetItems = {};
|
|
85
|
+
}
|
|
86
|
+
// 2. Track all array indecies to remove (will be overwritten)
|
|
87
|
+
const targetItemsToRemove = findMatchingItemArray(target.targetLocale, sourceObjectOptions, sourceObjectPointer, sourceObjectValue);
|
|
88
|
+
Object.values(targetItemsToRemove).forEach(({ index }) => indiciesToRemove.add(index));
|
|
89
|
+
// 3. Merge matchingDefaultLocaleItems and targetItems
|
|
90
|
+
const mergedItems = {
|
|
91
|
+
...(sourceObjectOptions.transform ? matchingDefaultLocaleItems : {}),
|
|
92
|
+
...targetItems,
|
|
93
|
+
};
|
|
94
|
+
// 4. Validate that the mergedItems is not empty
|
|
95
|
+
if (Object.keys(mergedItems).length === 0) {
|
|
74
96
|
logWarning(`Translated JSON for locale: ${target.targetLocale} does not have a valid sourceObjectPointer: ${sourceObjectPointer}. Skipping this target`);
|
|
75
97
|
continue;
|
|
76
98
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const mutateSourceItemIndex = matchingTargetItem
|
|
83
|
-
? matchingTargetItem.itemIndex
|
|
84
|
-
: undefined;
|
|
85
|
-
const mutateSourceItemKeyPointer = defaultLocaleKeyPointer;
|
|
86
|
-
const { identifyingLocaleProperty: targetLocaleKeyProperty } = getSourceObjectOptionsArray(target.targetLocale, sourceObjectPointer, sourceObjectOptions);
|
|
87
|
-
// 3. Override the source item with the translated values
|
|
88
|
-
JSONPointer.set(mutateSourceItem, mutateSourceItemKeyPointer, targetLocaleKeyProperty);
|
|
89
|
-
for (const [translatedKeyJsonPointer, translatedValue,] of Object.entries(targetJson[sourceObjectPointer] || {})) {
|
|
90
|
-
try {
|
|
91
|
-
const value = JSONPointer.get(mutateSourceItem, translatedKeyJsonPointer);
|
|
92
|
-
if (!value)
|
|
93
|
-
continue;
|
|
94
|
-
JSONPointer.set(mutateSourceItem, translatedKeyJsonPointer, translatedValue);
|
|
99
|
+
for (const [sourceItemPointer, targetItem] of Object.entries(mergedItems)) {
|
|
100
|
+
// 5. Validate that all the array indecies are still present in the source json
|
|
101
|
+
if (!matchingDefaultLocaleItemKeys.has(sourceItemPointer)) {
|
|
102
|
+
logError(`Array index ${sourceItemPointer} is not present in the source json. It is possible that the source json has been modified since the translation was generated.`);
|
|
103
|
+
exit(1);
|
|
95
104
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
105
|
+
// 6. Override the source item with the translated values
|
|
106
|
+
const defaultLocaleSourceItem = matchingDefaultLocaleItems[sourceItemPointer].sourceItem;
|
|
107
|
+
const defaultLocaleKeyPointer = matchingDefaultLocaleItems[sourceItemPointer].keyPointer;
|
|
108
|
+
const mutatedSourceItem = structuredClone(defaultLocaleSourceItem);
|
|
109
|
+
const { identifyingLocaleProperty: targetLocaleKeyProperty } = getSourceObjectOptionsArray(target.targetLocale, sourceObjectPointer, sourceObjectOptions);
|
|
110
|
+
JSONPointer.set(mutatedSourceItem, defaultLocaleKeyPointer, targetLocaleKeyProperty);
|
|
111
|
+
for (const [translatedKeyJsonPointer, translatedValue,] of Object.entries(targetItem || {})) {
|
|
112
|
+
try {
|
|
113
|
+
const value = JSONPointer.get(mutatedSourceItem, translatedKeyJsonPointer);
|
|
114
|
+
if (!value)
|
|
115
|
+
continue;
|
|
116
|
+
JSONPointer.set(mutatedSourceItem, translatedKeyJsonPointer, translatedValue);
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
/* empty */
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// 7. Apply additional mutations to the sourceItem
|
|
123
|
+
applyTransformations(mutatedSourceItem, sourceObjectOptions.transform, target.targetLocale, defaultLocale);
|
|
124
|
+
itemsToAdd.push(mutatedSourceItem);
|
|
106
125
|
}
|
|
107
126
|
}
|
|
108
|
-
|
|
127
|
+
// 8. Check that items to add is >= items to remove (if this happens, something is very wrong)
|
|
128
|
+
if (itemsToAdd.length < indiciesToRemove.size) {
|
|
129
|
+
logError(`Items to add is less than items to remove at path: ${sourceObjectPointer}. Please check your JSON schema key field.`);
|
|
130
|
+
exit(1);
|
|
131
|
+
}
|
|
132
|
+
// 9. Remove all items for the target locale (they can be identified by the key)
|
|
133
|
+
const filteredSourceObjectValue = sourceObjectValue.filter((_, index) => !indiciesToRemove.has(index));
|
|
134
|
+
// 10. Add all items to the original JSON
|
|
135
|
+
filteredSourceObjectValue.push(...itemsToAdd);
|
|
136
|
+
JSONPointer.set(mergedJson, sourceObjectPointer, filteredSourceObjectValue);
|
|
109
137
|
}
|
|
110
138
|
else {
|
|
111
139
|
// Validate type
|
|
@@ -122,17 +150,19 @@ export function mergeJson(originalContent, filePath, options, targets, defaultLo
|
|
|
122
150
|
}
|
|
123
151
|
const { sourceItem: defaultLocaleSourceItem } = matchingDefaultLocaleItem;
|
|
124
152
|
// For each target:
|
|
125
|
-
// 1.
|
|
126
|
-
// 2.
|
|
127
|
-
// 3.
|
|
128
|
-
// 4.
|
|
129
|
-
// 5.
|
|
153
|
+
// 1. Get the target items
|
|
154
|
+
// 2. Find the source item for the target locale
|
|
155
|
+
// 3. Merge the target items with the source item
|
|
156
|
+
// 4. Validate that the mergedItems is not empty
|
|
157
|
+
// 5. Override the source item with the translated values
|
|
158
|
+
// 6. Apply additional mutations to the sourceItem
|
|
159
|
+
// 7. Merge the source item with the original JSON (if the source item is not a new item)
|
|
130
160
|
for (const target of targets) {
|
|
131
161
|
const targetJson = JSON.parse(target.translatedContent);
|
|
132
|
-
// 1.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
162
|
+
// 1. Get the target items
|
|
163
|
+
let targetItems = targetJson[sourceObjectPointer];
|
|
164
|
+
if (!targetItems) {
|
|
165
|
+
targetItems = {};
|
|
136
166
|
}
|
|
137
167
|
// 2. Find the source item for the target locale
|
|
138
168
|
const matchingTargetItem = findMatchingItemObject(target.targetLocale, sourceObjectPointer, sourceObjectOptions, sourceObjectValue);
|
|
@@ -140,19 +170,31 @@ export function mergeJson(originalContent, filePath, options, targets, defaultLo
|
|
|
140
170
|
// Otherwise, fallback to the default locale source item
|
|
141
171
|
const mutateSourceItem = structuredClone(defaultLocaleSourceItem);
|
|
142
172
|
const mutateSourceItemKey = matchingTargetItem.keyParentProperty;
|
|
143
|
-
// 3.
|
|
144
|
-
|
|
173
|
+
// 3. Merge the target items with the source item (if there are transformations to perform)
|
|
174
|
+
const mergedItems = {
|
|
175
|
+
...(sourceObjectOptions.transform ? defaultLocaleSourceItem : {}),
|
|
176
|
+
...targetItems,
|
|
177
|
+
};
|
|
178
|
+
// 4. Validate that the mergedItems is not empty
|
|
179
|
+
if (Object.keys(mergedItems).length === 0) {
|
|
180
|
+
logWarning(`Translated JSON for locale: ${target.targetLocale} does not have a valid sourceObjectPointer: ${sourceObjectPointer}. Skipping this target`);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
// 5. Override the source item with the translated values
|
|
184
|
+
for (const [translatedKeyJsonPointer, translatedValue,] of Object.entries(mergedItems || {})) {
|
|
145
185
|
try {
|
|
146
186
|
const value = JSONPointer.get(mutateSourceItem, translatedKeyJsonPointer);
|
|
147
187
|
if (!value)
|
|
148
188
|
continue;
|
|
149
189
|
JSONPointer.set(mutateSourceItem, translatedKeyJsonPointer, translatedValue);
|
|
150
190
|
}
|
|
151
|
-
catch
|
|
191
|
+
catch {
|
|
192
|
+
/* empty */
|
|
193
|
+
}
|
|
152
194
|
}
|
|
153
|
-
//
|
|
195
|
+
// 6. Apply additional mutations to the sourceItem
|
|
154
196
|
applyTransformations(mutateSourceItem, sourceObjectOptions.transform, target.targetLocale, defaultLocale);
|
|
155
|
-
//
|
|
197
|
+
// 7. Merge the source item with the original JSON
|
|
156
198
|
sourceObjectValue[mutateSourceItemKey] = mutateSourceItem;
|
|
157
199
|
}
|
|
158
200
|
JSONPointer.set(mergedJson, sourceObjectPointer, sourceObjectValue);
|
|
@@ -184,7 +226,13 @@ function replaceLocalePlaceholders(string, localeProperties) {
|
|
|
184
226
|
return match;
|
|
185
227
|
});
|
|
186
228
|
}
|
|
187
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Apply transformations to the sourceItem in-place
|
|
231
|
+
* @param sourceItem - The source item to apply transformations to
|
|
232
|
+
* @param transform - The transformations to apply
|
|
233
|
+
* @param targetLocale - The target locale
|
|
234
|
+
* @param defaultLocale - The default locale
|
|
235
|
+
*/
|
|
188
236
|
export function applyTransformations(sourceItem, transform, targetLocale, defaultLocale) {
|
|
189
237
|
if (!transform)
|
|
190
238
|
return;
|
|
@@ -12,7 +12,7 @@ export function parseJson(content, filePath, options, defaultLocale) {
|
|
|
12
12
|
try {
|
|
13
13
|
json = JSON.parse(content);
|
|
14
14
|
}
|
|
15
|
-
catch
|
|
15
|
+
catch {
|
|
16
16
|
logError(`Invalid JSON file: ${filePath}`);
|
|
17
17
|
exit(1);
|
|
18
18
|
}
|
|
@@ -31,6 +31,10 @@ export function parseJson(content, filePath, options, defaultLocale) {
|
|
|
31
31
|
// Construct lvl 2
|
|
32
32
|
const sourceObjectsToTranslate = {};
|
|
33
33
|
for (const [sourceObjectPointer, { sourceObjectValue, sourceObjectOptions },] of Object.entries(sourceObjectPointers)) {
|
|
34
|
+
// Skip if no includes
|
|
35
|
+
if (!sourceObjectOptions.include.length) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
34
38
|
// Find the default locale in each source item in each sourceObjectValue
|
|
35
39
|
// Array: use key field
|
|
36
40
|
if (sourceObjectOptions.type === 'array') {
|
|
@@ -39,38 +43,45 @@ export function parseJson(content, filePath, options, defaultLocale) {
|
|
|
39
43
|
logError(`Source object value is not an array at path: ${sourceObjectPointer}`);
|
|
40
44
|
exit(1);
|
|
41
45
|
}
|
|
42
|
-
//
|
|
43
|
-
const
|
|
44
|
-
if (!
|
|
46
|
+
// Find matching source items
|
|
47
|
+
const matchingItems = findMatchingItemArray(defaultLocale, sourceObjectOptions, sourceObjectPointer, sourceObjectValue);
|
|
48
|
+
if (!Object.keys(matchingItems).length) {
|
|
45
49
|
logError(`Matching sourceItem not found at path: ${sourceObjectPointer} for locale: ${defaultLocale}. Please check your JSON schema`);
|
|
46
50
|
exit(1);
|
|
47
51
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
52
|
+
// Construct lvl 3
|
|
53
|
+
const sourceItemsToTranslate = {};
|
|
54
|
+
for (const [arrayPointer, matchingItem] of Object.entries(matchingItems)) {
|
|
55
|
+
const { sourceItem, keyPointer } = matchingItem;
|
|
56
|
+
// Get the fields to translate from the includes
|
|
57
|
+
const matchingItemsToTranslate = [];
|
|
58
|
+
for (const include of sourceObjectOptions.include) {
|
|
59
|
+
try {
|
|
60
|
+
const matchingItems = JSONPath({
|
|
61
|
+
json: sourceItem,
|
|
62
|
+
path: include,
|
|
63
|
+
resultType: 'all',
|
|
64
|
+
flatten: true,
|
|
65
|
+
wrap: true,
|
|
66
|
+
});
|
|
67
|
+
if (matchingItems) {
|
|
68
|
+
matchingItemsToTranslate.push(...matchingItems);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
/* empty */
|
|
62
73
|
}
|
|
63
74
|
}
|
|
64
|
-
|
|
75
|
+
// Filter out the key pointer
|
|
76
|
+
sourceItemsToTranslate[arrayPointer] = Object.fromEntries(matchingItemsToTranslate
|
|
77
|
+
.filter((item) => item.pointer !== keyPointer)
|
|
78
|
+
.map((item) => [
|
|
79
|
+
item.pointer,
|
|
80
|
+
item.value,
|
|
81
|
+
]));
|
|
65
82
|
}
|
|
66
|
-
itemsToTranslate = Object.fromEntries(itemsToTranslate
|
|
67
|
-
.filter((item) => item.pointer !== keyPointer)
|
|
68
|
-
.map((item) => [
|
|
69
|
-
item.pointer,
|
|
70
|
-
item.value,
|
|
71
|
-
]));
|
|
72
83
|
// Add the items to translate to the result
|
|
73
|
-
sourceObjectsToTranslate[sourceObjectPointer] =
|
|
84
|
+
sourceObjectsToTranslate[sourceObjectPointer] = sourceItemsToTranslate;
|
|
74
85
|
}
|
|
75
86
|
else {
|
|
76
87
|
// Object: use the key in this object with the matching locale property
|
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
import { AdditionalOptions, JsonSchema, SourceObjectOptions } from '../../types/index.js';
|
|
2
|
-
export declare function findMatchingItemArray(locale: string, sourceObjectOptions: SourceObjectOptions, sourceObjectPointer: string, sourceObjectValue: any): {
|
|
2
|
+
export declare function findMatchingItemArray(locale: string, sourceObjectOptions: SourceObjectOptions, sourceObjectPointer: string, sourceObjectValue: any): Record<string, {
|
|
3
3
|
sourceItem: any;
|
|
4
4
|
keyParentProperty: string;
|
|
5
|
-
itemIndex: number;
|
|
6
5
|
keyPointer: string;
|
|
7
|
-
|
|
6
|
+
index: number;
|
|
7
|
+
}>;
|
|
8
8
|
export declare function findMatchingItemObject(locale: string, sourceObjectPointer: string, sourceObjectOptions: SourceObjectOptions, sourceObjectValue: any): {
|
|
9
9
|
sourceItem: any | undefined;
|
|
10
10
|
keyParentProperty: string;
|
|
11
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Get the identifying locale property for an object
|
|
14
|
+
* @param locale - The locale to get the identifying locale property for
|
|
15
|
+
* @param sourceObjectPointer - The path to the source object
|
|
16
|
+
* @param sourceObjectOptions - The source object options
|
|
17
|
+
* @returns The identifying locale property
|
|
18
|
+
*/
|
|
12
19
|
export declare function getIdentifyingLocaleProperty(locale: string, sourceObjectPointer: string, sourceObjectOptions: SourceObjectOptions): string;
|
|
20
|
+
/**
|
|
21
|
+
* Get the identifying locale property and the json path to the key for an array
|
|
22
|
+
* @param locale - The locale to get the identifying locale property for
|
|
23
|
+
* @param sourceObjectPointer - The path to the source object
|
|
24
|
+
* @param sourceObjectOptions - The source object options
|
|
25
|
+
* @returns The identifying locale property and the json path to the key
|
|
26
|
+
*/
|
|
13
27
|
export declare function getSourceObjectOptionsArray(locale: string, sourceObjectPointer: string, sourceObjectOptions: SourceObjectOptions): {
|
|
14
28
|
identifyingLocaleProperty: string;
|
|
15
29
|
localeKeyJsonPath: string;
|
|
@@ -17,6 +31,13 @@ export declare function getSourceObjectOptionsArray(locale: string, sourceObject
|
|
|
17
31
|
export declare function getSourceObjectOptionsObject(defaultLocale: string, sourceObjectPointer: string, sourceObjectOptions: SourceObjectOptions): {
|
|
18
32
|
identifyingLocaleProperty: string;
|
|
19
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* Generate a mapping of sourceObjectPointer to SourceObjectOptions
|
|
36
|
+
* where the sourceObjectPointer is a jsonpointer to the array or object containing
|
|
37
|
+
* @param jsonSchema - The json schema to generate the mapping from
|
|
38
|
+
* @param originalJson - The original json to generate the mapping from
|
|
39
|
+
* @returns A mapping of sourceObjectPointer to SourceObjectOptions
|
|
40
|
+
*/
|
|
20
41
|
export declare function generateSourceObjectPointers(jsonSchema: {
|
|
21
42
|
[sourceObjectPath: string]: SourceObjectOptions;
|
|
22
43
|
}, originalJson: any): Record<string, {
|
|
@@ -11,6 +11,7 @@ const { isMatch } = micromatch;
|
|
|
11
11
|
export function findMatchingItemArray(locale, sourceObjectOptions, sourceObjectPointer, sourceObjectValue) {
|
|
12
12
|
const { identifyingLocaleProperty, localeKeyJsonPath } = getSourceObjectOptionsArray(locale, sourceObjectPointer, sourceObjectOptions);
|
|
13
13
|
// Use the json pointer key to locate the source item
|
|
14
|
+
const matchingItems = {};
|
|
14
15
|
for (const [index, item] of sourceObjectValue.entries()) {
|
|
15
16
|
// Get the key candidates
|
|
16
17
|
const keyCandidates = JSONPath({
|
|
@@ -24,23 +25,28 @@ export function findMatchingItemArray(locale, sourceObjectOptions, sourceObjectP
|
|
|
24
25
|
logError(`Source item at path: ${sourceObjectPointer} does not have a key value at path: ${localeKeyJsonPath}`);
|
|
25
26
|
exit(1);
|
|
26
27
|
}
|
|
27
|
-
else if (keyCandidates.length
|
|
28
|
+
else if (keyCandidates.length === 0) {
|
|
29
|
+
// If no key candidates, skip the item
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
else if (keyCandidates.length > 1) {
|
|
33
|
+
// If multiple key candidates, exit with an error
|
|
28
34
|
logError(`Source item at path: ${sourceObjectPointer} has multiple matching keys with path: ${localeKeyJsonPath}`);
|
|
29
35
|
exit(1);
|
|
30
36
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
identifyingLocaleProperty !== keyCandidates[0].value) {
|
|
37
|
+
else if (identifyingLocaleProperty !== keyCandidates[0].value) {
|
|
38
|
+
// Validate the key is the identifying locale property
|
|
34
39
|
continue;
|
|
35
40
|
}
|
|
36
|
-
|
|
41
|
+
// Map the index to the source item
|
|
42
|
+
matchingItems[`/${index}`] = {
|
|
37
43
|
sourceItem: item,
|
|
38
44
|
keyParentProperty: keyCandidates[0].parentProperty,
|
|
39
|
-
itemIndex: index,
|
|
40
45
|
keyPointer: keyCandidates[0].pointer,
|
|
46
|
+
index,
|
|
41
47
|
};
|
|
42
48
|
}
|
|
43
|
-
return
|
|
49
|
+
return matchingItems;
|
|
44
50
|
}
|
|
45
51
|
export function findMatchingItemObject(locale, sourceObjectPointer, sourceObjectOptions, sourceObjectValue) {
|
|
46
52
|
const { identifyingLocaleProperty } = getSourceObjectOptionsObject(locale, sourceObjectPointer, sourceObjectOptions);
|
|
@@ -56,6 +62,13 @@ export function findMatchingItemObject(locale, sourceObjectPointer, sourceObject
|
|
|
56
62
|
keyParentProperty: identifyingLocaleProperty,
|
|
57
63
|
};
|
|
58
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Get the identifying locale property for an object
|
|
67
|
+
* @param locale - The locale to get the identifying locale property for
|
|
68
|
+
* @param sourceObjectPointer - The path to the source object
|
|
69
|
+
* @param sourceObjectOptions - The source object options
|
|
70
|
+
* @returns The identifying locale property
|
|
71
|
+
*/
|
|
59
72
|
export function getIdentifyingLocaleProperty(locale, sourceObjectPointer, sourceObjectOptions) {
|
|
60
73
|
// Validate localeProperty
|
|
61
74
|
const localeProperty = sourceObjectOptions.localeProperty || 'code';
|
|
@@ -66,6 +79,13 @@ export function getIdentifyingLocaleProperty(locale, sourceObjectPointer, source
|
|
|
66
79
|
}
|
|
67
80
|
return identifyingLocaleProperty;
|
|
68
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Get the identifying locale property and the json path to the key for an array
|
|
84
|
+
* @param locale - The locale to get the identifying locale property for
|
|
85
|
+
* @param sourceObjectPointer - The path to the source object
|
|
86
|
+
* @param sourceObjectOptions - The source object options
|
|
87
|
+
* @returns The identifying locale property and the json path to the key
|
|
88
|
+
*/
|
|
69
89
|
export function getSourceObjectOptionsArray(locale, sourceObjectPointer, sourceObjectOptions) {
|
|
70
90
|
const identifyingLocaleProperty = getIdentifyingLocaleProperty(locale, sourceObjectPointer, sourceObjectOptions);
|
|
71
91
|
const localeKeyJsonPath = sourceObjectOptions.key;
|
|
@@ -84,8 +104,13 @@ export function getSourceObjectOptionsObject(defaultLocale, sourceObjectPointer,
|
|
|
84
104
|
}
|
|
85
105
|
return { identifyingLocaleProperty };
|
|
86
106
|
}
|
|
87
|
-
|
|
88
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Generate a mapping of sourceObjectPointer to SourceObjectOptions
|
|
109
|
+
* where the sourceObjectPointer is a jsonpointer to the array or object containing
|
|
110
|
+
* @param jsonSchema - The json schema to generate the mapping from
|
|
111
|
+
* @param originalJson - The original json to generate the mapping from
|
|
112
|
+
* @returns A mapping of sourceObjectPointer to SourceObjectOptions
|
|
113
|
+
*/
|
|
89
114
|
export function generateSourceObjectPointers(jsonSchema, originalJson) {
|
|
90
115
|
const sourceObjectPointers = Object.entries(jsonSchema).reduce((acc, [sourceObjectPath, sourceObjectOptions]) => {
|
|
91
116
|
const sourceObjects = flattenJson(originalJson, [sourceObjectPath]);
|
|
@@ -11,7 +11,7 @@ import fs from 'node:fs';
|
|
|
11
11
|
import path from 'node:path';
|
|
12
12
|
import { parse } from '@babel/parser';
|
|
13
13
|
import { createMatchPath, loadConfig } from 'tsconfig-paths';
|
|
14
|
-
import
|
|
14
|
+
import resolve from 'resolve';
|
|
15
15
|
/**
|
|
16
16
|
* Processes a single translation function call (e.g., t('hello world', { id: 'greeting' })).
|
|
17
17
|
* Extracts the translatable string content and metadata, then adds it to the updates array.
|