Skip to content

Commit 6b7de91

Browse files
authored
More robust i18n identifier matching (elastic#235603)
## Summary Bugfix which creates more robust matching of file paths to i18n translation IDs.
1 parent 5eb6887 commit 6b7de91

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

packages/kbn-eslint-plugin-i18n/helpers/get_i18n_identifier_from_file_path.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import { getI18nIdentifierFromFilePath } from './get_i18n_identifier_from_file_p
1212
const SYSTEMPATH = 'systemPath';
1313

1414
const testMap = [
15+
[
16+
'x-pack/solutions/observability/plugins/observability_onboarding/public/application/app.tsx',
17+
'xpack.observability_onboarding',
18+
],
1519
[
1620
'x-pack/solutions/observability/plugins/observability/public/header_actions.tsx',
1721
'xpack.observability',

packages/kbn-eslint-plugin-i18n/helpers/get_i18n_identifier_from_file_path.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import fs from 'fs';
1111
import { join, parse, resolve } from 'path';
12-
import { findKey } from 'lodash';
1312

1413
export function getI18nIdentifierFromFilePath(fileName: string, cwd: string) {
1514
const { dir } = parse(fileName);
@@ -43,11 +42,21 @@ export function getI18nIdentifierFromFilePath(fileName: string, cwd: string) {
4342

4443
if (Object.keys(allPaths).length === 0) return 'could_not_find_i18nrc';
4544

46-
return (
47-
findKey(allPaths, (value) =>
48-
Array.isArray(value)
49-
? value.find((el) => path.includes(el))
50-
: typeof value === 'string' && path.includes(value)
51-
) ?? 'app_not_found_in_i18nrc'
52-
);
45+
// First try exact matches
46+
for (const [k, v] of Object.entries(allPaths)) {
47+
if (Array.isArray(v)) {
48+
if (v.some((p) => path === p)) return k;
49+
} else if (typeof v === 'string' && path === v) {
50+
return k;
51+
}
52+
}
53+
54+
// Fallback to substring (legacy behavior)
55+
for (const [k, v] of Object.entries(allPaths)) {
56+
if (Array.isArray(v)) {
57+
if (v.some((p) => path.includes(p))) return k;
58+
} else if (typeof v === 'string' && path.includes(v)) {
59+
return k;
60+
}
61+
}
5362
}

0 commit comments

Comments
 (0)