Skip to content

Commit e67a804

Browse files
authored
static text (#11)
2 parents ecdf2af + ac62b98 commit e67a804

File tree

9 files changed

+16595
-14
lines changed

9 files changed

+16595
-14
lines changed

bin/cli.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env node
2+
3+
const { translate } = require("../translate-gen/translate");
4+
const { LANGUAGES_MAP } = require("../translate-gen/language_model_map");
5+
const { Command } = require("commander");
6+
const path = require("node:path");
7+
const program = new Command();
8+
program
9+
.description("A CLI for generating static translation files")
10+
.version("0.0.1");
11+
program.option(
12+
"-t, --target_languages <target_languages...>",
13+
"Language to generate",
14+
);
15+
program.option(
16+
"-d, --directory <directory>",
17+
"Directory to start generating translations from",
18+
);
19+
program.option(
20+
"-e, --extensions <extensions...>",
21+
"File extensions to search for",
22+
);
23+
program.option(
24+
"-i, --ignore_directories <ignore_directories...>",
25+
"Directories to ignore",
26+
);
27+
program.option(
28+
"-s, --source_language <source_language>",
29+
"Source language to translate from",
30+
);
31+
program.option("-f, --ignore_files <ignore_files...>", "Files to ignore");
32+
program.option(
33+
"-m, --ignore_functions <ignore_functions...>",
34+
"Functions to ignore",
35+
);
36+
program.option(
37+
"-a, --ignore_attributes <ignore_attributes...>",
38+
"Strings to ignore",
39+
);
40+
program.option("-l, --model_name <model_name>", "Specific model name");
41+
42+
program.parse();
43+
const translatetargetLangs = [];
44+
const srcLangs = [];
45+
const languages = program.opts().target_languages;
46+
const dir = program.opts().directory ? program.opts().directory : process.cwd();
47+
const srcLang = program.opts().source_language
48+
? program.opts().source_language
49+
: "English";
50+
const extensions = program.opts().extensions
51+
? program.opts().extensions
52+
: ["js", "jsx", "ts", "tsx"];
53+
const ignoreDirectories = program.opts().ignore_directories
54+
? program.opts().ignore_directories
55+
: ["node_modules", ".git", ".next", "public", "styles", "dist"];
56+
const ignoreFiles = program.opts().ignore_files
57+
? program.opts().ignore_files
58+
: [".config"];
59+
const ignoreFunctions = program.opts().ignore_functions
60+
? program.opts().ignore_functions
61+
: [
62+
"require",
63+
"cva",
64+
"cn",
65+
"fetch",
66+
"FontSans",
67+
"map",
68+
"split",
69+
"join",
70+
"filter",
71+
"reduce",
72+
"concat",
73+
"includes",
74+
"indexOf",
75+
"find",
76+
"findIndex",
77+
"some",
78+
"every",
79+
"sort",
80+
"slice",
81+
"splice",
82+
"push",
83+
"pop",
84+
"shift",
85+
"unshift",
86+
"reverse",
87+
"flat",
88+
"flatMap",
89+
"fill",
90+
"copyWithin",
91+
];
92+
const ignoreAttributes = program.opts().ignore_attributes
93+
? program.opts().ignore_attributes
94+
: [
95+
"href",
96+
"src",
97+
"className",
98+
"style",
99+
"d",
100+
"viewBox",
101+
"fill",
102+
"xmlns",
103+
"xmlnsXlink",
104+
"icon",
105+
];
106+
const modelName = program.opts().model_name
107+
? program.opts().model_name
108+
: "Xenova/nllb-200-distilled-600M";
109+
const modelList = [
110+
"Xenova/nllb-200-distilled-600M",
111+
"Xenova/mbart-large-50-many-to-many-mmt",
112+
"Xenova/m2m100_418M",
113+
"CXDuncan/madlad400-3b-mt-optimized-quantized-onnx",
114+
];
115+
if (languages) {
116+
checkMap(languages, translatetargetLangs);
117+
checkMap([srcLang], srcLangs);
118+
console.log("Generating language translation file", modelName);
119+
translate(
120+
translatetargetLangs,
121+
dir,
122+
srcLangs[0],
123+
extensions,
124+
ignoreDirectories,
125+
ignoreFiles,
126+
ignoreFunctions,
127+
ignoreAttributes,
128+
modelName,
129+
languages,
130+
);
131+
} else {
132+
console.error("Please specify a language");
133+
process.exit(1);
134+
}
135+
136+
function checkMap(input, langsArray) {
137+
input.forEach((element) => {
138+
if (!LANGUAGES_MAP[element]) {
139+
console.error("Please specify a valid language as per documentation");
140+
console.error(`${element} does not match`);
141+
process.exit(1);
142+
} else if (modelList.includes(modelName)) {
143+
const supportingmodels = [];
144+
let noissue = true;
145+
LANGUAGES_MAP[element].forEach((model) => {
146+
if (model[modelName] !== undefined && model[modelName] === "") {
147+
console.error(`This model does not support this language ${element}`);
148+
noissue = false;
149+
} else if (model[modelName] !== undefined && model[modelName] !== "") {
150+
langsArray.push(model[modelName]);
151+
} else if (Object.values(model)[0] !== "") {
152+
supportingmodels.push(Object.keys(model)[0]);
153+
}
154+
});
155+
if (!noissue) {
156+
const models = supportingmodels.join(",");
157+
console.error(`These models ${models} support this language `);
158+
process.exit(1);
159+
}
160+
} else {
161+
console.error("Please specify a valid model name as per documentation");
162+
console.error(`${modelName} does not match`);
163+
process.exit(1);
164+
}
165+
});
166+
}

lefthook.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
pre-commit:
2-
parallel: false
3-
commands:
4-
lint:
5-
run: pnpm biome check --write --unsafe --staged --no-errors-on-unmatched && git add -u
6-
typecheck:
7-
run: pnpm tsc
8-
build:
9-
run: pnpm build
10-
test:
11-
run: pnpm test:ci
1+
# pre-commit:
2+
# parallel: false
3+
# commands:
4+
# lint:
5+
# run: pnpm biome check --write --unsafe --staged --no-errors-on-unmatched && git add -u
6+
# typecheck:
7+
# run: pnpm tsc
8+
# build:
9+
# run: pnpm build
10+
# test:
11+
# run: pnpm test:ci

0 commit comments

Comments
 (0)