-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcollectUntranslatedEntries.ts
More file actions
55 lines (53 loc) · 1.78 KB
/
collectUntranslatedEntries.ts
File metadata and controls
55 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Dictionary, DictionaryEntry } from '../types/types';
import getEntryAndMetadata from './getEntryAndMetadata';
import { get } from './indexDict';
import { isDictionaryEntry } from './isDictionaryEntry';
/**
* @description Collects all untranslated entries from a dictionary
* @param dictionary - The dictionary to collect untranslated entries from
* @param translationsDictionary - The translated dictionary to compare against
* @param id - The id of the dictionary to collect untranslated entries from
* @returns An array of untranslated entries
*/
export function collectUntranslatedEntries(
dictionary: Dictionary,
translationsDictionary: Dictionary,
id: string = ''
): {
source: string | null;
metadata: { $id: string; $context?: string; $_hash: string };
}[] {
const untranslatedEntries: {
source: string | null;
metadata: { $id: string; $context?: string; $_hash: string };
}[] = [];
Object.entries(dictionary).forEach(([key, value]) => {
const wholeId = id ? `${id}.${key}` : key;
if (isDictionaryEntry(value)) {
const { entry, metadata } = getEntryAndMetadata(value);
if (get(translationsDictionary, key) === undefined) {
untranslatedEntries.push({
source: entry,
metadata: {
$id: wholeId,
$context: metadata?.$context,
$_hash: metadata?.$_hash || '',
},
});
}
} else {
let translationsValue = get(translationsDictionary, key);
if (translationsValue === undefined) {
translationsValue = Array.isArray(value) ? [] : {};
}
untranslatedEntries.push(
...collectUntranslatedEntries(
value,
translationsValue as Dictionary,
wholeId
)
);
}
});
return untranslatedEntries;
}