Skip to content

Commit 7b2692a

Browse files
committed
test: add unit tests for replaceI18nVariables function
1 parent f2b4542 commit 7b2692a

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, it } from "vitest";
2+
import { replaceI18nVariables } from "./i18n.js";
3+
4+
describe("replaceI18nVariables", () => {
5+
it("should replace simple variables", () => {
6+
const baseString = "commands.{{name}}.name";
7+
const patterns = { name: "greet" };
8+
const result = replaceI18nVariables(baseString, patterns);
9+
expect(result).toBe("commands.greet.name");
10+
});
11+
12+
it("should replace path variable with only one folder", () => {
13+
const baseString = "commands.{{path.}}.name";
14+
const patterns = { path: "folder" };
15+
const result = replaceI18nVariables(baseString, patterns);
16+
expect(result).toBe("commands.folder.name");
17+
});
18+
19+
it("should replace path variables with custom separator", () => {
20+
const baseString = "commands.{{path/}}.name";
21+
const patterns = { path: "folder/subfolder" };
22+
const result = replaceI18nVariables(baseString, patterns);
23+
expect(result).toBe("commands.folder/subfolder.name");
24+
});
25+
26+
it("should replace path variables with different separator", () => {
27+
const baseString = "commands.{{path-}}.name";
28+
const patterns = { path: "folder/subfolder" };
29+
const result = replaceI18nVariables(baseString, patterns);
30+
expect(result).toBe("commands.folder-subfolder.name");
31+
});
32+
33+
it("should handle multiple replacements", () => {
34+
const baseString = "commands.{{name}}.{{path/}}.description";
35+
const patterns = { name: "greet", path: "folder/subfolder" };
36+
const result = replaceI18nVariables(baseString, patterns);
37+
expect(result).toBe("commands.greet.folder/subfolder.description");
38+
});
39+
40+
it("should return the original string if no patterns match", () => {
41+
const baseString = "commands.{{name}}.name";
42+
const patterns = { age: "30" };
43+
const result = replaceI18nVariables(baseString, patterns);
44+
expect(result).toBe("commands.{{name}}.name");
45+
});
46+
47+
it("should handle empty patterns", () => {
48+
const baseString = "commands.{{name}}.name";
49+
const patterns = {};
50+
const result = replaceI18nVariables(baseString, patterns);
51+
expect(result).toBe("commands.{{name}}.name");
52+
});
53+
});

packages/cli/src/utils/i18n.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import type { ArcscordFileData } from "../arcscord_file/type";
2+
3+
export function replaceI18nVariables(baseString: string, patterns: Record<string, string>): string {
4+
for (const [key, value] of Object.entries(patterns)) {
5+
if (key === "path") {
6+
const pathPatternMatch = baseString.match(/\{\{path(.)\}\}/);
7+
if (pathPatternMatch) {
8+
const separator = pathPatternMatch[1];
9+
const pathAlias = value.replace(/\//g, separator);
10+
baseString = baseString.replace(new RegExp(`\\{\\{path${separator}\\}\\}`, "g"), pathAlias);
11+
}
12+
continue;
13+
}
14+
baseString = baseString.replaceAll(`{{${key}}}`, value);
15+
}
16+
return baseString;
17+
}
18+
19+
export function getCommandNameI18nPath(options: ArcscordFileData, variables: {
20+
name: string;
21+
path: string;
22+
}): string {
23+
return replaceI18nVariables(
24+
options.i18n?.defaultCommandNamePattern ?? "commands.{{path.}}.{{name}}.name",
25+
variables,
26+
);
27+
};
28+
29+
export function getCommandDescriptionI18nPath(options: ArcscordFileData, variables: {
30+
name: string;
31+
path: string;
32+
}): string {
33+
return replaceI18nVariables(
34+
options.i18n?.defaultCommandDescriptionPattern ?? "commands.{{path.}}.{{name}}.description",
35+
variables,
36+
);
37+
};
38+
39+
export function getSubCommandNameI18nPath(options: ArcscordFileData, variables: {
40+
name: string;
41+
path: string;
42+
subName: string;
43+
}): string {
44+
return replaceI18nVariables(
45+
options.i18n?.defaultSubCommandNamePattern ?? "commands.{{path.}}.{{name}}.{{subName}}.name",
46+
variables,
47+
);
48+
}
49+
50+
export function getSubCommandDescriptionI18nPath(options: ArcscordFileData, variables: {
51+
name: string;
52+
path: string;
53+
subName: string;
54+
}): string {
55+
return replaceI18nVariables(
56+
options.i18n?.defaultSubCommandDescriptionPattern ?? "commands.{{path.}}.{{name}}.{{subName}}.description",
57+
variables,
58+
);
59+
}
60+
61+
export function getSubCommandWithGroupNameI18nPath(options: ArcscordFileData, variables: {
62+
name: string;
63+
path: string;
64+
subName: string;
65+
groupName: string;
66+
}): string {
67+
return replaceI18nVariables(
68+
options.i18n?.defaultSubCommandWithGroupNamePattern ?? "commands.{{path.}}.{{name}}.{{groupName}}.{{subName}}.name",
69+
variables,
70+
);
71+
}
72+
73+
export function getSubCommandWithGroupDescriptionI18nPath(options: ArcscordFileData, variables: {
74+
name: string;
75+
path: string;
76+
subName: string;
77+
groupName: string;
78+
}): string {
79+
return replaceI18nVariables(
80+
options.i18n?.defaultSubCommandWithGroupDescriptionPattern ?? "commands.{{path.}}.{{name}}.{{groupName}}.{{subName}}.description",
81+
variables,
82+
);
83+
}
84+
85+
export function getSubCommandGroupNameI18nPath(options: ArcscordFileData, variables: {
86+
name: string;
87+
path: string;
88+
groupName: string;
89+
}): string {
90+
return replaceI18nVariables(
91+
options.i18n?.defaultSubCommandGroupNamePattern ?? "commands.{{path.}}.{{name}}.{{groupName}}.name",
92+
variables,
93+
);
94+
}
95+
96+
export function getSubCommandGroupDescriptionI18nPath(options: ArcscordFileData, variables: {
97+
name: string;
98+
path: string;
99+
groupName: string;
100+
}): string {
101+
return replaceI18nVariables(
102+
options.i18n?.defaultSubCommandGroupDescriptionPattern ?? "commands.{{path.}}.{{name}}.{{groupName}}.description",
103+
variables,
104+
);
105+
}

0 commit comments

Comments
 (0)