-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathinjectFallbacks.ts
More file actions
44 lines (40 loc) · 1.63 KB
/
injectFallbacks.ts
File metadata and controls
44 lines (40 loc) · 1.63 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
import { Dictionary } from '../types/types';
import { getDictionaryEntry } from './getDictionaryEntry';
import getEntryAndMetadata from './getEntryAndMetadata';
import { injectEntry } from './injectEntry';
import { isDictionaryEntry } from './isDictionaryEntry';
/**
* @description Injects fallbacks into a dictionary
* @param dictionary - The dictionary to inject translations into
* @param translationsDictionary - The translations to inject into the dictionary
* @param translations - The translations to inject into the dictionary
* @param id - The id of the dictionary to inject translations into
*/
export function injectFallbacks(
dictionary: Dictionary,
translationsDictionary: Dictionary,
missingTranslations: {
source: string | null;
metadata: { $id: string; $context?: string; $_hash: string };
}[],
prefixToRemove: string = ''
) {
const prefixToRemoveArray = prefixToRemove ? prefixToRemove.split('.') : [];
missingTranslations.forEach(({ source, metadata }) => {
const { $id } = metadata;
const id =
prefixToRemoveArray.length > 0
? $id.split('.').slice(prefixToRemoveArray.length).join('.')
: $id;
// Look up in translations object
const translationEntry = getDictionaryEntry(translationsDictionary, id);
// Look up in translations dictionary
let dictTransEntry = undefined;
if (isDictionaryEntry(translationEntry))
dictTransEntry = getEntryAndMetadata(translationEntry).entry;
// Fall back to source
const value = dictTransEntry || source;
injectEntry(value as string, translationsDictionary, id, dictionary);
});
return translationsDictionary;
}