locale-keysmith 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # locale-keysmith
2
+
3
+ Sort, audit, clean, and translate locale files from the command line.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add -D locale-keysmith
9
+ ```
10
+
11
+ ## Commands
12
+
13
+ ```bash
14
+ pnpm exec locale-keysmith scan
15
+ pnpm exec locale-keysmith sort --write
16
+ pnpm exec locale-keysmith missing
17
+ pnpm exec locale-keysmith unused
18
+ pnpm exec locale-keysmith remove-unused --dry-run
19
+ pnpm exec locale-keysmith translate --from en --to de,fr --write
20
+ ```
21
+
22
+ ## Config
23
+
24
+ Create `locale-keysmith.config.json` in your project:
25
+
26
+ ```json
27
+ {
28
+ "sourceLocale": "en",
29
+ "translationFiles": [
30
+ "src/locales/*.{json,yml,yaml}",
31
+ "public/locales/**/*.{json,yml,yaml}"
32
+ ],
33
+ "src": ["src/**/*.{ts,tsx,js,jsx,vue}"],
34
+ "ignoreKeys": ["debug.*"]
35
+ }
36
+ ```
37
+
38
+ ## Google Translate
39
+
40
+ Set a Google Translate API key before running the `translate` command:
41
+
42
+ ```bash
43
+ export GOOGLE_TRANSLATE_API_KEY="your-api-key"
44
+ pnpm exec locale-keysmith translate --from en --to de --write
45
+ ```
46
+
47
+ ## Publish
48
+
49
+ Validate without publishing:
50
+
51
+ ```bash
52
+ pnpm run check
53
+ pnpm publish --dry-run --access public --no-git-checks
54
+ ```
55
+
56
+ Publish from a clean `main` branch:
57
+
58
+ ```bash
59
+ pnpm publish --access public
60
+ ```
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,140 @@
1
+ #!/usr/bin/env node
2
+ import chalk from "chalk";
3
+ import { Command } from "commander";
4
+ import { loadConfig } from "./config.js";
5
+ import { deleteValueByPath, filterIgnoredKeys, flattenKeys, sortObjectKeys } from "./keys.js";
6
+ import { writeLocaleFile } from "./parser.js";
7
+ import { loadLocaleFiles } from "./scanner.js";
8
+ import { translateMissingKeys } from "./translate.js";
9
+ import { findUsedKeys } from "./usage.js";
10
+ const program = new Command();
11
+ program
12
+ .name("locale-keysmith")
13
+ .description("Sort, audit, clean, and translate locale files")
14
+ .version("0.1.0");
15
+ program.command("scan").description("Find translation files").action(async () => {
16
+ const files = await loadLocaleFiles();
17
+ if (files.length === 0) {
18
+ console.log(chalk.yellow("No locale files found."));
19
+ return;
20
+ }
21
+ for (const file of files) {
22
+ console.log(`${chalk.green(file.locale)} ${file.path}`);
23
+ }
24
+ });
25
+ program
26
+ .command("sort")
27
+ .description("Sort locale keys alphabetically")
28
+ .option("--write", "Write changes to disk")
29
+ .action(async (options) => {
30
+ const files = await loadLocaleFiles();
31
+ for (const file of files) {
32
+ file.data = sortObjectKeys(file.data);
33
+ if (options.write)
34
+ writeLocaleFile(file);
35
+ console.log(`${options.write ? "sorted" : "would sort"} ${file.path}`);
36
+ }
37
+ });
38
+ program.command("missing").description("Find keys missing from non-source locales").action(async () => {
39
+ const config = loadConfig();
40
+ const files = await loadLocaleFiles();
41
+ const source = files.find((file) => file.locale === config.sourceLocale);
42
+ if (!source) {
43
+ throw new Error(`Source locale "${config.sourceLocale}" was not found.`);
44
+ }
45
+ const sourceKeys = filterIgnoredKeys(flattenKeys(source.data), config.ignoreKeys);
46
+ for (const file of files.filter((candidate) => candidate.path !== source.path)) {
47
+ const targetKeys = new Set(flattenKeys(file.data));
48
+ const missing = sourceKeys.filter((key) => !targetKeys.has(key));
49
+ console.log(chalk.bold(file.locale));
50
+ if (missing.length === 0) {
51
+ console.log(chalk.green(" no missing keys"));
52
+ continue;
53
+ }
54
+ for (const key of missing)
55
+ console.log(` ${key}`);
56
+ }
57
+ });
58
+ program.command("unused").description("Find translation keys not used in source files").action(async () => {
59
+ const config = loadConfig();
60
+ const files = await loadLocaleFiles();
61
+ const source = files.find((file) => file.locale === config.sourceLocale);
62
+ if (!source) {
63
+ throw new Error(`Source locale "${config.sourceLocale}" was not found.`);
64
+ }
65
+ const used = await findUsedKeys();
66
+ const unused = filterIgnoredKeys(flattenKeys(source.data), config.ignoreKeys).filter((key) => !used.has(key));
67
+ if (unused.length === 0) {
68
+ console.log(chalk.green("No unused keys found."));
69
+ return;
70
+ }
71
+ for (const key of unused)
72
+ console.log(key);
73
+ });
74
+ program
75
+ .command("remove-unused")
76
+ .description("Remove unused translation keys from all locale files")
77
+ .option("--dry-run", "Preview changes without writing to disk")
78
+ .option("--write", "Write changes to disk")
79
+ .action(async (options) => {
80
+ const config = loadConfig();
81
+ const files = await loadLocaleFiles();
82
+ const source = files.find((file) => file.locale === config.sourceLocale);
83
+ if (!source) {
84
+ throw new Error(`Source locale "${config.sourceLocale}" was not found.`);
85
+ }
86
+ const used = await findUsedKeys();
87
+ const unused = filterIgnoredKeys(flattenKeys(source.data), config.ignoreKeys).filter((key) => !used.has(key));
88
+ if (unused.length === 0) {
89
+ console.log(chalk.green("No unused keys found."));
90
+ return;
91
+ }
92
+ for (const file of files) {
93
+ for (const key of unused)
94
+ deleteValueByPath(file.data, key);
95
+ file.data = sortObjectKeys(file.data);
96
+ if (options.write)
97
+ writeLocaleFile(file);
98
+ }
99
+ console.log(`${options.write ? "removed" : "would remove"} ${unused.length} unused keys`);
100
+ });
101
+ program
102
+ .command("translate")
103
+ .description("Translate missing keys from the source locale")
104
+ .requiredOption("--to <locales>", "Comma-separated target locales")
105
+ .option("--from <locale>", "Source locale", "en")
106
+ .option("--write", "Write changes to disk")
107
+ .action(async (options) => {
108
+ const files = await loadLocaleFiles();
109
+ const source = files.find((file) => file.locale === options.from);
110
+ if (!source)
111
+ throw new Error(`Source locale "${options.from}" was not found.`);
112
+ const sourceKeys = flattenKeys(source.data);
113
+ const targets = new Set(options.to.split(",").map((locale) => locale.trim()).filter(Boolean));
114
+ for (const target of files.filter((file) => targets.has(file.locale))) {
115
+ const targetKeys = new Set(flattenKeys(target.data));
116
+ const missing = sourceKeys.filter((key) => !targetKeys.has(key));
117
+ if (missing.length === 0) {
118
+ console.log(chalk.green(`${target.locale}: no missing keys`));
119
+ continue;
120
+ }
121
+ if (!options.write) {
122
+ console.log(`${target.locale}: would translate ${missing.length} keys`);
123
+ continue;
124
+ }
125
+ const changed = await translateMissingKeys({
126
+ source,
127
+ target,
128
+ targetLanguage: target.locale,
129
+ keys: missing
130
+ });
131
+ target.data = sortObjectKeys(target.data);
132
+ writeLocaleFile(target);
133
+ console.log(`${target.locale}: translated ${changed} keys`);
134
+ }
135
+ });
136
+ program.parseAsync().catch((error) => {
137
+ console.error(chalk.red(error instanceof Error ? error.message : String(error)));
138
+ process.exitCode = 1;
139
+ });
140
+ //# source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AC,GAAG,MAAM,CAAC,MAAM,gBAAgB,OAAO,OAAO,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC,CAAC,CAAC;AAEL,OAAO,CAAC,UAAU,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC5C,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACjF,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ import type { LocaleLintConfig } from "./types.js";
2
+ export declare function loadConfig(cwd?: string): LocaleLintConfig;
package/dist/config.js ADDED
@@ -0,0 +1,25 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ const defaultConfig = {
4
+ sourceLocale: "en",
5
+ translationFiles: [
6
+ "**/locales/**/*.{json,yml,yaml}",
7
+ "**/i18n/**/*.{json,yml,yaml}",
8
+ "**/translations/**/*.{json,yml,yaml}"
9
+ ],
10
+ src: ["src/**/*.{ts,tsx,js,jsx,vue,svelte}"],
11
+ ignoreKeys: []
12
+ };
13
+ export function loadConfig(cwd = process.cwd()) {
14
+ const path = join(cwd, "locale-keysmith.config.json");
15
+ if (!existsSync(path))
16
+ return defaultConfig;
17
+ const userConfig = JSON.parse(readFileSync(path, "utf8"));
18
+ return {
19
+ sourceLocale: userConfig.sourceLocale ?? defaultConfig.sourceLocale,
20
+ translationFiles: userConfig.translationFiles ?? defaultConfig.translationFiles,
21
+ src: userConfig.src ?? defaultConfig.src,
22
+ ignoreKeys: userConfig.ignoreKeys ?? defaultConfig.ignoreKeys
23
+ };
24
+ }
25
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,MAAM,aAAa,GAAqB;IACtC,YAAY,EAAE,IAAI;IAClB,gBAAgB,EAAE;QAChB,iCAAiC;QACjC,8BAA8B;QAC9B,sCAAsC;KACvC;IACD,GAAG,EAAE,CAAC,qCAAqC,CAAC;IAC5C,UAAU,EAAE,EAAE;CACf,CAAC;AAEF,MAAM,UAAU,UAAU,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE;IAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,6BAA6B,CAAC,CAAC;IACtD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,aAAa,CAAC;IAE5C,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAA8B,CAAC;IAEvF,OAAO;QACL,YAAY,EAAE,UAAU,CAAC,YAAY,IAAI,aAAa,CAAC,YAAY;QACnE,gBAAgB,EAAE,UAAU,CAAC,gBAAgB,IAAI,aAAa,CAAC,gBAAgB;QAC/E,GAAG,EAAE,UAAU,CAAC,GAAG,IAAI,aAAa,CAAC,GAAG;QACxC,UAAU,EAAE,UAAU,CAAC,UAAU,IAAI,aAAa,CAAC,UAAU;KAC9D,CAAC;AACJ,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./keys.js";
2
+ export * from "./parser.js";
3
+ export * from "./scanner.js";
4
+ export * from "./usage.js";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./keys.js";
2
+ export * from "./parser.js";
3
+ export * from "./scanner.js";
4
+ export * from "./usage.js";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC"}
package/dist/keys.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { LocaleObject, LocaleValue } from "./types.js";
2
+ export declare function sortObjectKeys<T extends LocaleValue>(value: T): T;
3
+ export declare function flattenKeys(value: LocaleObject, prefix?: string): string[];
4
+ export declare function filterIgnoredKeys(keys: string[], ignorePatterns: string[]): string[];
5
+ export declare function getValueByPath(value: LocaleObject, path: string): LocaleValue | undefined;
6
+ export declare function setValueByPath(value: LocaleObject, path: string, nextValue: LocaleValue): void;
7
+ export declare function deleteValueByPath(value: LocaleObject, path: string): void;
package/dist/keys.js ADDED
@@ -0,0 +1,76 @@
1
+ export function sortObjectKeys(value) {
2
+ if (Array.isArray(value)) {
3
+ return value.map((item) => sortObjectKeys(item));
4
+ }
5
+ if (isPlainObject(value)) {
6
+ const sorted = Object.keys(value)
7
+ .sort((a, b) => a.localeCompare(b))
8
+ .reduce((result, key) => {
9
+ result[key] = sortObjectKeys(value[key]);
10
+ return result;
11
+ }, {});
12
+ return sorted;
13
+ }
14
+ return value;
15
+ }
16
+ export function flattenKeys(value, prefix = "") {
17
+ return Object.entries(value).flatMap(([key, child]) => {
18
+ const path = prefix ? `${prefix}.${key}` : key;
19
+ if (isPlainObject(child)) {
20
+ return flattenKeys(child, path);
21
+ }
22
+ return [path];
23
+ });
24
+ }
25
+ export function filterIgnoredKeys(keys, ignorePatterns) {
26
+ if (ignorePatterns.length === 0)
27
+ return keys;
28
+ const matchers = ignorePatterns.map(patternToRegExp);
29
+ return keys.filter((key) => !matchers.some((matcher) => matcher.test(key)));
30
+ }
31
+ export function getValueByPath(value, path) {
32
+ return path.split(".").reduce((current, segment) => {
33
+ if (!isPlainObject(current))
34
+ return undefined;
35
+ return current[segment];
36
+ }, value);
37
+ }
38
+ export function setValueByPath(value, path, nextValue) {
39
+ const segments = path.split(".");
40
+ let current = value;
41
+ for (const segment of segments.slice(0, -1)) {
42
+ const child = current[segment];
43
+ if (!isPlainObject(child)) {
44
+ current[segment] = {};
45
+ }
46
+ current = current[segment];
47
+ }
48
+ current[segments.at(-1)] = nextValue;
49
+ }
50
+ export function deleteValueByPath(value, path) {
51
+ const segments = path.split(".");
52
+ let current = value;
53
+ const parents = [];
54
+ for (const segment of segments.slice(0, -1)) {
55
+ const child = current[segment];
56
+ if (!isPlainObject(child))
57
+ return;
58
+ parents.push({ object: current, key: segment });
59
+ current = child;
60
+ }
61
+ delete current[segments.at(-1)];
62
+ for (const parent of parents.reverse()) {
63
+ const child = parent.object[parent.key];
64
+ if (isPlainObject(child) && Object.keys(child).length === 0) {
65
+ delete parent.object[parent.key];
66
+ }
67
+ }
68
+ }
69
+ function isPlainObject(value) {
70
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
71
+ }
72
+ function patternToRegExp(pattern) {
73
+ const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replaceAll("*", ".*");
74
+ return new RegExp(`^${escaped}$`);
75
+ }
76
+ //# sourceMappingURL=keys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.js","sourceRoot":"","sources":["../src/keys.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,cAAc,CAAwB,KAAQ;IAC5D,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,CAAM,CAAC;IACxD,CAAC;IAED,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;aAC9B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;aAClC,MAAM,CAAe,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE;YACpC,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ChD,OAAO,GAAG,KAAK,CAAC;IAClB,CAAC;IAED,OAAO,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,CAAW,CAAC,CAAC;IAE1C,KAAK,MAAM,MAAM,IAAI,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;QACvC,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACxC,IAAI,aAAa,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC5D,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,eAAe,CAAC,OAAe;IACtC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IACpF,OAAO,IAAI,MAAM,CAAC,IAAI,OAAO,GAAG,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { LocaleFile } from "./types.js";
2
+ export declare function readLocaleFile(path: string): LocaleFile;
3
+ export declare function writeLocaleFile(file: LocaleFile): void;
4
+ export declare function inferLocale(path: string): string;
package/dist/parser.js ADDED
@@ -0,0 +1,26 @@
1
+ import { extname } from "node:path";
2
+ import { readFileSync, writeFileSync } from "node:fs";
3
+ import YAML from "yaml";
4
+ export function readLocaleFile(path) {
5
+ const extension = extname(path).toLowerCase();
6
+ const raw = readFileSync(path, "utf8");
7
+ const format = extension === ".json" ? "json" : "yaml";
8
+ const parsed = format === "json" ? JSON.parse(raw) : YAML.parse(raw);
9
+ return {
10
+ path,
11
+ locale: inferLocale(path),
12
+ format,
13
+ data: (parsed ?? {})
14
+ };
15
+ }
16
+ export function writeLocaleFile(file) {
17
+ const content = file.format === "json"
18
+ ? `${JSON.stringify(file.data, null, 2)}\n`
19
+ : YAML.stringify(file.data, { sortMapEntries: false });
20
+ writeFileSync(file.path, content);
21
+ }
22
+ export function inferLocale(path) {
23
+ const filename = path.split(/[\\/]/).at(-1) ?? "";
24
+ return filename.replace(/\.(json|ya?ml)$/i, "");
25
+ }
26
+ //# sourceMappingURL=parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parser.js","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,IAAI,MAAM,MAAM,CAAC;AAGxB,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;IAC9C,MAAM,GAAG,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACvC,MAAM,MAAM,GAAG,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC;IACvD,MAAM,MAAM,GAAG,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAErE,OAAO;QACL,IAAI;QACJ,MAAM,EAAE,WAAW,CAAC,IAAI,CAAC;QACzB,MAAM;QACN,IAAI,EAAE,CAAC,MAAM,IAAI,EAAE,CAAiB;KACrC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,IAAgB;IAC9C,MAAM,OAAO,GACX,IAAI,CAAC,MAAM,KAAK,MAAM;QACpB,CAAC,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI;QAC3C,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC;IAE3D,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACpC,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAClD,OAAO,QAAQ,CAAC,OAAO,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC;AAClD,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { LocaleFile } from "./types.js";
2
+ export declare function findLocaleFiles(cwd?: string): Promise<string[]>;
3
+ export declare function loadLocaleFiles(cwd?: string): Promise<LocaleFile[]>;
@@ -0,0 +1,16 @@
1
+ import fg from "fast-glob";
2
+ import { loadConfig } from "./config.js";
3
+ import { readLocaleFile } from "./parser.js";
4
+ export async function findLocaleFiles(cwd = process.cwd()) {
5
+ const config = loadConfig(cwd);
6
+ return fg(config.translationFiles, {
7
+ cwd,
8
+ absolute: true,
9
+ ignore: ["**/node_modules/**", "**/dist/**", "**/.next/**"]
10
+ });
11
+ }
12
+ export async function loadLocaleFiles(cwd = process.cwd()) {
13
+ const paths = await findLocaleFiles(cwd);
14
+ return paths.map(readLocaleFile);
15
+ }
16
+ //# sourceMappingURL=scanner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scanner.js","sourceRoot":"","sources":["../src/scanner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,WAAW,CAAC;AAC3B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG7C,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE;IACvD,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;IAC/B,OAAO,EAAE,CAAC,MAAM,CAAC,gBAAgB,EAAE;QACjC,GAAG;QACH,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,CAAC,oBAAoB,EAAE,YAAY,EAAE,aAAa,CAAC;KAC5D,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE;IACvD,MAAM,KAAK,GAAG,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC;IACzC,OAAO,KAAK,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { LocaleFile } from "./types.js";
2
+ export declare function translateMissingKeys(options: {
3
+ source: LocaleFile;
4
+ target: LocaleFile;
5
+ targetLanguage: string;
6
+ keys: string[];
7
+ }): Promise<number>;
@@ -0,0 +1,36 @@
1
+ import { getValueByPath, setValueByPath } from "./keys.js";
2
+ export async function translateMissingKeys(options) {
3
+ const apiKey = process.env.GOOGLE_TRANSLATE_API_KEY;
4
+ if (!apiKey) {
5
+ throw new Error("GOOGLE_TRANSLATE_API_KEY is required for the translate command.");
6
+ }
7
+ let changed = 0;
8
+ for (const key of options.keys) {
9
+ const sourceValue = getValueByPath(options.source.data, key);
10
+ if (typeof sourceValue !== "string" || sourceValue.trim() === "")
11
+ continue;
12
+ const translated = await translateText(sourceValue, options.targetLanguage, apiKey);
13
+ setValueByPath(options.target.data, key, translated);
14
+ changed += 1;
15
+ }
16
+ return changed;
17
+ }
18
+ async function translateText(text, target, apiKey) {
19
+ const response = await fetch(`https://translation.googleapis.com/language/translate/v2?key=${apiKey}`, {
20
+ method: "POST",
21
+ headers: {
22
+ "Content-Type": "application/json"
23
+ },
24
+ body: JSON.stringify({
25
+ q: text,
26
+ target,
27
+ format: "text"
28
+ })
29
+ });
30
+ if (!response.ok) {
31
+ throw new Error(`Google Translate request failed with status ${response.status}.`);
32
+ }
33
+ const payload = (await response.json());
34
+ return payload.data?.translations?.[0]?.translatedText ?? text;
35
+ }
36
+ //# sourceMappingURL=translate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"translate.js","sourceRoot":"","sources":["../src/translate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3D,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,OAK1C;IACC,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC;IACpD,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,MAAM,IAAI,KAAK,CAAC,iEAAiE,CAAC,CAAC;IACrF,CAAC;IAED,IAAI,OAAO,GAAG,CAAC,CAAC;IAEhB,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;QAC/B,MAAM,WAAW,GAAG,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAC7D,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE;YAAE,SAAS;QAE3E,MAAM,UAAU,GAAG,MAAM,aAAa,CAAC,WAAW,EAAE,OAAO,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;QACpF,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QACrD,OAAO,IAAI,CAAC,CAAC;IACf,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,KAAK,UAAU,aAAa,CAAC,IAAY,EAAE,MAAc,EAAE,MAAc;IACvE,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,gEAAgE,MAAM,EAAE,EAAE;QACrG,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;SACnC;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;YACnB,CAAC,EAAE,IAAI;YACP,MAAM;YACN,MAAM,EAAE,MAAM;SACf,CAAC;KACH,CAAC,CAAC;IAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,+CAA+C,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;IACrF,CAAC;IAED,MAAM,OAAO,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAErC,CAAC;IAEF,OAAO,OAAO,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,cAAc,IAAI,IAAI,CAAC;AACjE,CAAC"}
@@ -0,0 +1,16 @@
1
+ export type LocaleValue = string | number | boolean | null | LocaleObject | LocaleValue[];
2
+ export interface LocaleObject {
3
+ [key: string]: LocaleValue;
4
+ }
5
+ export interface LocaleFile {
6
+ path: string;
7
+ locale: string;
8
+ format: "json" | "yaml";
9
+ data: LocaleObject;
10
+ }
11
+ export interface LocaleLintConfig {
12
+ sourceLocale: string;
13
+ translationFiles: string[];
14
+ src: string[];
15
+ ignoreKeys: string[];
16
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export declare function findUsedKeys(cwd?: string): Promise<Set<string>>;
package/dist/usage.js ADDED
@@ -0,0 +1,27 @@
1
+ import { readFileSync } from "node:fs";
2
+ import fg from "fast-glob";
3
+ import { loadConfig } from "./config.js";
4
+ const usagePatterns = [
5
+ /\b(?:t|i18n\.t|\$t)\(\s*["'`]([^"'`]+)["'`]/g,
6
+ /\b(?:Trans|Translation)\s+[^>]*i18nKey=["'`]([^"'`]+)["'`]/g
7
+ ];
8
+ export async function findUsedKeys(cwd = process.cwd()) {
9
+ const config = loadConfig(cwd);
10
+ const files = await fg(config.src, {
11
+ cwd,
12
+ absolute: true,
13
+ ignore: ["**/node_modules/**", "**/dist/**", "**/.next/**"]
14
+ });
15
+ const used = new Set();
16
+ for (const file of files) {
17
+ const content = readFileSync(file, "utf8");
18
+ for (const pattern of usagePatterns) {
19
+ for (const match of content.matchAll(pattern)) {
20
+ if (match[1])
21
+ used.add(match[1]);
22
+ }
23
+ }
24
+ }
25
+ return used;
26
+ }
27
+ //# sourceMappingURL=usage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage.js","sourceRoot":"","sources":["../src/usage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,MAAM,WAAW,CAAC;AAC3B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,MAAM,aAAa,GAAG;IACpB,8CAA8C;IAC9C,6DAA6D;CAC9D,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE;IACpD,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;IAC/B,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC,GAAG,EAAE;QACjC,GAAG;QACH,QAAQ,EAAE,IAAI;QACd,MAAM,EAAE,CAAC,oBAAoB,EAAE,YAAY,EAAE,aAAa,CAAC;KAC5D,CAAC,CAAC;IACH,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAE/B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,OAAO,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAE3C,KAAK,MAAM,OAAO,IAAI,aAAa,EAAE,CAAC;YACpC,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC9C,IAAI,KAAK,CAAC,CAAC,CAAC;oBAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;YACnC,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "locale-keysmith",
3
+ "version": "0.1.0",
4
+ "description": "CLI helper for sorting, auditing, cleaning, and translating i18n locale keys.",
5
+ "type": "module",
6
+ "bin": {
7
+ "locale-keysmith": "./dist/cli.js"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "keywords": [
21
+ "i18n",
22
+ "l10n",
23
+ "locale",
24
+ "translation",
25
+ "cli"
26
+ ],
27
+ "author": "",
28
+ "license": "MIT",
29
+ "dependencies": {
30
+ "chalk": "^5.3.0",
31
+ "commander": "^12.1.0",
32
+ "fast-glob": "^3.3.2",
33
+ "yaml": "^2.5.1"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^22.5.5",
37
+ "tsx": "^4.19.1",
38
+ "typescript": "^5.6.2",
39
+ "vitest": "^4.1.5"
40
+ },
41
+ "engines": {
42
+ "node": ">=18.18"
43
+ },
44
+ "scripts": {
45
+ "dev": "tsx src/cli.ts",
46
+ "build": "tsc",
47
+ "test": "vitest run",
48
+ "check": "pnpm run build && pnpm run test"
49
+ }
50
+ }