Skip to content

Commit 1b5dce5

Browse files
committed
refactor(i18n): enhance path segment normalization and schema node retrieval
1 parent 20d84c2 commit 1b5dce5

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

dashboard/src/i18n/translations.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,49 @@ const PATH_SEGMENT_ALIASES: Record<string, string> = {
110110
'tool-use': 'tooluse'
111111
};
112112

113-
function normalizePathSegment(segment: string): string {
113+
function getSchemaNode(pathSegments: string[]): any {
114+
let node: any = translationSchema;
115+
for (const segment of pathSegments) {
116+
if (!node || typeof node !== 'object') {
117+
return null;
118+
}
119+
node = node[segment];
120+
}
121+
return node;
122+
}
123+
124+
function normalizePathSegment(segment: string, normalizedParentPath: string[]): string {
125+
// Prefer schema-driven normalization: only map when the target key exists under the same parent.
126+
const parentNode = getSchemaNode(normalizedParentPath);
127+
if (parentNode && typeof parentNode === 'object') {
128+
if (segment in parentNode) {
129+
return segment;
130+
}
131+
132+
const alias = PATH_SEGMENT_ALIASES[segment];
133+
if (alias && alias in parentNode) {
134+
return alias;
135+
}
136+
137+
// Heuristic: if file uses kebab-case but schema key is collapsed (e.g. tool-use -> tooluse)
138+
const collapsed = segment.replace(/-/g, '');
139+
if (collapsed !== segment && collapsed in parentNode) {
140+
return collapsed;
141+
}
142+
}
143+
144+
// Fallback to explicit aliases to keep backward compatibility.
114145
return PATH_SEGMENT_ALIASES[segment] ?? segment;
115146
}
116147

148+
function normalizePathSegments(segments: string[]): string[] {
149+
const normalized: string[] = [];
150+
for (const segment of segments) {
151+
normalized.push(normalizePathSegment(segment, normalized));
152+
}
153+
return normalized;
154+
}
155+
117156
function setNestedValue(target: Record<string, any>, pathSegments: string[], value: Record<string, any>): void {
118157
let current = target;
119158

@@ -143,7 +182,7 @@ function extractLocaleAndPath(modulePath: string): { locale: string; pathSegment
143182
}
144183

145184
const [, locale, relativePath] = match;
146-
const pathSegments = relativePath.split('/').map(normalizePathSegment);
185+
const pathSegments = normalizePathSegments(relativePath.split('/'));
147186

148187
return {
149188
locale,

0 commit comments

Comments
 (0)