Skip to content

Commit 8e4f95a

Browse files
authored
Merge pull request #146 from openscript/feat/support-nested-arrays-in-content
feat: support objects nested in arrays in i18n content
2 parents 20511e7 + 651a9e0 commit 8e4f95a

File tree

6 files changed

+201
-4
lines changed

6 files changed

+201
-4
lines changed

.changeset/plain-worms-find.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro-loader-i18n": patch
3+
---
4+
5+
Support objects nested in arrays in i18n content

libs/astro-loader-i18n/test/__fixtures__/collections.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,42 @@ export const contentFileFixture: DataEntry[] = [
201201
],
202202
},
203203
},
204+
{
205+
id: "gallery",
206+
filePath: "src/content/gallery/gallery.yml",
207+
data: {
208+
items: [
209+
{
210+
photo: "./team/robin.jpg",
211+
label: {
212+
"de-CH": "Robin improvisiert beim Arbeiten",
213+
"zh-CN": "Robin即兴工作",
214+
},
215+
},
216+
{
217+
photo: "./team/robin-presents.jpeg",
218+
label: {
219+
"de-CH": "Robin präsentiert",
220+
"zh-CN": "Robin演示",
221+
},
222+
},
223+
{
224+
photo: "./team/our-lab.jpg",
225+
label: {
226+
"de-CH": "Unser Labor",
227+
"zh-CN": "我们的实验室",
228+
},
229+
},
230+
{
231+
photo: "./team/retraite-2024.jpg",
232+
label: {
233+
"de-CH": "Team-Retreat 2024",
234+
"zh-CN": "团队静修2024",
235+
},
236+
},
237+
],
238+
},
239+
},
204240
];
205241

206242
export const contentFileWithoutFixture: DataEntry[] = [

libs/astro-loader-i18n/test/loaders/__snapshots__/i18n-file-loader.spec.ts.snap

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,68 @@ exports[`i18nFileLoader > should put common translation id and locale in data 1`
113113
"id": "space/zh-CN",
114114
},
115115
],
116+
[
117+
"gallery/de-CH",
118+
{
119+
"data": {
120+
"basePath": "/",
121+
"contentPath": "src/content/gallery",
122+
"items": [
123+
{
124+
"label": "Robin improvisiert beim Arbeiten",
125+
"photo": "./team/robin.jpg",
126+
},
127+
{
128+
"label": "Robin präsentiert",
129+
"photo": "./team/robin-presents.jpeg",
130+
},
131+
{
132+
"label": "Unser Labor",
133+
"photo": "./team/our-lab.jpg",
134+
},
135+
{
136+
"label": "Team-Retreat 2024",
137+
"photo": "./team/retraite-2024.jpg",
138+
},
139+
],
140+
"locale": "de-CH",
141+
"translationId": "src/content/gallery/gallery.yml",
142+
},
143+
"filePath": "src/content/gallery/gallery.yml",
144+
"id": "gallery/de-CH",
145+
},
146+
],
147+
[
148+
"gallery/zh-CN",
149+
{
150+
"data": {
151+
"basePath": "/",
152+
"contentPath": "src/content/gallery",
153+
"items": [
154+
{
155+
"label": "Robin即兴工作",
156+
"photo": "./team/robin.jpg",
157+
},
158+
{
159+
"label": "Robin演示",
160+
"photo": "./team/robin-presents.jpeg",
161+
},
162+
{
163+
"label": "我们的实验室",
164+
"photo": "./team/our-lab.jpg",
165+
},
166+
{
167+
"label": "团队静修2024",
168+
"photo": "./team/retraite-2024.jpg",
169+
},
170+
],
171+
"locale": "zh-CN",
172+
"translationId": "src/content/gallery/gallery.yml",
173+
},
174+
"filePath": "src/content/gallery/gallery.yml",
175+
"id": "gallery/zh-CN",
176+
},
177+
],
116178
]
117179
`;
118180

libs/astro-utils-i18n/src/utils/collection.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
export function getAllUniqueKeys(obj: Record<string, unknown>, keys = new Set<string>()): Set<string> {
1+
export function getAllUniqueKeys(obj: Record<string, unknown>, keys = new Set<string>(), ignore?: boolean): Set<string> {
22
Object.entries(obj).forEach(([key, value]) => {
3-
keys.add(key);
4-
if (value && typeof value === "object" && !Array.isArray(value)) {
5-
getAllUniqueKeys(value as Record<string, unknown>, keys);
3+
if (!ignore) keys.add(key);
4+
if (value && typeof value === "object") {
5+
if (Array.isArray(value)) {
6+
value.forEach((item) => {
7+
if (item && typeof item === "object") {
8+
// Recurse into objects and arrays within arrays
9+
if (Array.isArray(item)) {
10+
// Wrap array in an object to reuse the function and ignore its key
11+
getAllUniqueKeys({ array: item }, keys, true);
12+
} else {
13+
getAllUniqueKeys(item as Record<string, unknown>, keys);
14+
}
15+
}
16+
});
17+
} else {
18+
getAllUniqueKeys(value as Record<string, unknown>, keys);
19+
}
620
}
721
});
822
return keys;

libs/astro-utils-i18n/test/utils/__snapshots__/collection.spec.ts.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ Set {
1313
}
1414
`;
1515

16+
exports[`getAllUniqueKeys > should return all unique keys of an object nested in an array 1`] = `
17+
Set {
18+
"a",
19+
"z",
20+
"y",
21+
"de",
22+
"en",
23+
}
24+
`;
25+
26+
exports[`getAllUniqueKeys > should return all unique keys of an object with arrays in arrays 1`] = `
27+
Set {
28+
"a",
29+
"z",
30+
"y",
31+
"de",
32+
"en",
33+
}
34+
`;
35+
1636
exports[`pruneLocales > should prune locales with nested data 1`] = `
1737
{
1838
"array": [

libs/astro-utils-i18n/test/utils/collection.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,66 @@ describe("getAllUniqueKeys", () => {
2222
});
2323
expect(result).toMatchSnapshot();
2424
});
25+
26+
it("should return all unique keys of an object nested in an array", () => {
27+
const result = getAllUniqueKeys({
28+
a: [
29+
{
30+
z: 1,
31+
y: {
32+
de: 2,
33+
en: 3,
34+
},
35+
},
36+
{
37+
z: 4,
38+
y: {
39+
de: 5,
40+
en: 6,
41+
},
42+
},
43+
],
44+
});
45+
expect(result).toMatchSnapshot();
46+
});
47+
48+
it("should return all unique keys of an object with arrays in arrays", () => {
49+
const result = getAllUniqueKeys({
50+
a: [
51+
[
52+
{
53+
z: 1,
54+
y: [
55+
{
56+
de: 2,
57+
en: 3,
58+
},
59+
{
60+
de: 4,
61+
en: 5,
62+
},
63+
],
64+
},
65+
],
66+
[
67+
{
68+
z: 6,
69+
y: [
70+
{
71+
de: 7,
72+
en: 8,
73+
},
74+
{
75+
de: 9,
76+
en: 10,
77+
},
78+
],
79+
},
80+
],
81+
],
82+
});
83+
expect(result).toMatchSnapshot();
84+
});
2585
});
2686

2787
describe("pruneLocales", () => {

0 commit comments

Comments
 (0)