Skip to content

Commit 5a4e516

Browse files
authored
feat(cli): add preservedKeys (#1960)
1 parent ad5ab2d commit 5a4e516

File tree

15 files changed

+379
-32
lines changed

15 files changed

+379
-32
lines changed

.changeset/crisp-signs-itch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@lingo.dev/_spec": patch
3+
"lingo.dev": patch
4+
---
5+
6+
Add `preservedKeys` configuration option to buckets. Preserved keys are added to targets using source values as placeholders, but once present in the target file, they are never overwritten by the CLI. This is useful for values like URLs or emails that should be copied initially but then customized per locale.

packages/cli/demo/json/en/example.json

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@
1010
"author": {
1111
"name": "John Doe"
1212
},
13-
"contributors": [
14-
{ "name": "Alice" },
15-
{ "name": "Bob" }
16-
],
17-
"messages": [
18-
"Welcome to MyApp",
19-
"Hello, world!"
20-
],
13+
"contributors": [{ "name": "Alice" }, { "name": "Bob" }],
14+
"messages": ["Welcome to MyApp", "Hello, world!"],
2115
"config": {
2216
"theme": {
2317
"primary": "Blue theme"
@@ -32,6 +26,9 @@
3226
}
3327
],
3428
"locked_key_1": "This value is locked and should not be changed",
35-
"ignored_key_1": "This value is ignored and should not appear in target locales"
36-
37-
}
29+
"ignored_key_1": "This value is ignored and should not appear in target locales",
30+
"preserved_key_1": "this value is preserved and should not be overwritten",
31+
"legal": {
32+
"preserved_nested": "this value is preserved and should not be overwritten"
33+
}
34+
}

packages/cli/demo/json/es/example.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@
3232
"nested_message": "Texto anidado"
3333
}
3434
],
35-
"locked_key_1": "This value is locked and should not be changed"
36-
}
35+
"locked_key_1": "This value is locked and should not be changed",
36+
"preserved_key_1": "this value is preserved and should not be overwritten",
37+
"legal": {
38+
"preserved_nested": "this value is preserved and should not be overwritten"
39+
}
40+
}

packages/cli/demo/json/i18n.json

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
{
2-
"version": "1.10",
2+
"version": "1.12",
33
"locale": {
44
"source": "en",
5-
"targets": [
6-
"es"
7-
]
5+
"targets": ["es"]
86
},
97
"buckets": {
108
"json": {
11-
"include": [
12-
"./[locale]/example.json"
13-
],
14-
"lockedKeys": ["locked_key_1"]
9+
"include": ["./[locale]/example.json"],
10+
"lockedKeys": ["locked_key_1"],
11+
"preservedKeys": ["preserved_key_1", "legal/preserved_nested"]
1512
}
1613
},
1714
"$schema": "https://lingo.dev/schema/i18n.json"
18-
}
15+
}

packages/cli/demo/json/i18n.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ checksums:
1414
config/theme/primary: 7535a3779d6934ea8ecf18f5cb5b93fd
1515
mixed_array/0: 001b5b003d96c133534f5907abffdf77
1616
mixed_array/3/nested_message: 5f0782dfc5993e99890c0475bc295a30
17+
ignored_key_1: 386cb6c3c496982b059671768905d66e
18+
legal/preserved_nested: 1f22d279b6be8d261e125e54c5a6cb64

packages/cli/i18n.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.10",
2+
"version": "1.12",
33
"locale": {
44
"source": "en",
55
"targets": ["es"]
@@ -34,7 +34,8 @@
3434
"json": {
3535
"include": ["demo/json/[locale]/example.json"],
3636
"lockedKeys": ["locked_key_1"],
37-
"ignoredKeys": ["ignored_key_1"]
37+
"ignoredKeys": ["ignored_key_1"],
38+
"preservedKeys": ["preserved_key_1", "legal/preserved_nested"]
3839
},
3940
"jsonc": {
4041
"include": ["demo/jsonc/[locale]/example.jsonc"],

packages/cli/src/cli/cmd/run/_types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type CmdRunTask = {
3232
lockedKeys: string[];
3333
lockedPatterns: string[];
3434
ignoredKeys: string[];
35+
preservedKeys: string[];
3536
onlyKeys: string[];
3637
formatter?: "prettier" | "biome";
3738
};

packages/cli/src/cli/cmd/run/execute.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ function createLoaderForTask(assignedTask: CmdRunTask) {
163163
assignedTask.lockedKeys,
164164
assignedTask.lockedPatterns,
165165
assignedTask.ignoredKeys,
166+
assignedTask.preservedKeys,
166167
);
167168
bucketLoader.setDefaultLocale(assignedTask.sourceLocale);
168169

packages/cli/src/cli/cmd/run/frozen.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export default async function frozen(input: CmdRunContext) {
6464
bucket.lockedKeys,
6565
bucket.lockedPatterns,
6666
bucket.ignoredKeys,
67+
bucket.preservedKeys,
6768
);
6869
loader.setDefaultLocale(resolvedSourceLocale);
6970
await loader.init();
@@ -103,6 +104,7 @@ export default async function frozen(input: CmdRunContext) {
103104
bucket.lockedKeys,
104105
bucket.lockedPatterns,
105106
bucket.ignoredKeys,
107+
bucket.preservedKeys,
106108
);
107109
loader.setDefaultLocale(resolvedSourceLocale);
108110
await loader.init();

packages/cli/src/cli/cmd/run/plan.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export default async function plan(
133133
lockedKeys: bucket.lockedKeys || [],
134134
lockedPatterns: bucket.lockedPatterns || [],
135135
ignoredKeys: bucket.ignoredKeys || [],
136+
preservedKeys: bucket.preservedKeys || [],
136137
onlyKeys: input.flags.key || [],
137138
formatter: input.config!.formatter,
138139
});

0 commit comments

Comments
 (0)