-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathuseCreateInternalUseTranslationsFunction.ts
More file actions
152 lines (134 loc) · 4.27 KB
/
useCreateInternalUseTranslationsFunction.ts
File metadata and controls
152 lines (134 loc) · 4.27 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { useCallback } from 'react';
import {
Dictionary,
DictionaryTranslationOptions,
Translations,
} from '../../../types/types';
import {
getDictionaryEntry,
isValidDictionaryEntry,
} from '../../../dictionaries/getDictionaryEntry';
import getEntryAndMetadata from '../../../dictionaries/getEntryAndMetadata';
import {
createInvalidDictionaryEntryWarning,
createNoEntryFoundWarning,
} from '../../../errors/createErrors';
import { hashSource } from 'generaltranslation/id';
import { formatMessage } from 'generaltranslation';
import { TranslateIcuCallback } from '../../../types/runtime';
export default function useCreateInternalUseTranslationsFunction(
dictionary: Dictionary | undefined,
dictionaryTranslations: Dictionary | undefined,
translations: Translations | null,
locale: string,
defaultLocale: string,
translationRequired: boolean,
dialectTranslationRequired: boolean,
developmentApiEnabled: boolean,
registerIcuForTranslation: TranslateIcuCallback
) {
return useCallback(
(id: string, options: DictionaryTranslationOptions = {}): string => {
// Check: dictionary exists
if (!dictionary) {
return '';
}
// Get entry
const value = getDictionaryEntry(dictionary, id);
// Check: no entry found
if (!value) {
console.warn(createNoEntryFoundWarning(id));
return '';
}
// Check: invalid entry
if (!isValidDictionaryEntry(value)) {
console.warn(createInvalidDictionaryEntryWarning(id));
return '';
}
// Get entry and metadata
const { entry, metadata } = getEntryAndMetadata(value);
// ----- SET UP ----- //
// Check: reject invalid content
if (!entry || typeof entry !== 'string') return '';
// Render method
const renderMessage = (message: string, locales: string[]) => {
return formatMessage(message, {
locales,
variables: options,
});
};
// Check: translation not required
if (!translationRequired) return renderMessage(entry, [defaultLocale]);
// ----- CHECK DICTIONARY TRANSLATIONS ----- //
const dictionaryTranslation = getDictionaryEntry(
dictionaryTranslations || {},
id
);
if (
dictionaryTranslation &&
isValidDictionaryEntry(dictionaryTranslation)
) {
const { entry } = getEntryAndMetadata(dictionaryTranslation);
return renderMessage(entry || '', [locale, defaultLocale]);
}
// ----- CHECK TRANSLATIONS ----- //
let translationEntry = translations?.[id];
let hash = '';
const getHash = () =>
hashSource({
source: entry,
...(metadata?.$context && { context: metadata.$context }),
id,
dataFormat: 'ICU',
});
if (!translationEntry) {
hash = getHash();
translationEntry = translations?.[hash];
}
// Check translation successful
if (translationEntry) {
return renderMessage(translationEntry as string, [
locale,
defaultLocale,
]);
}
if (translationEntry === null) {
return renderMessage(entry, [defaultLocale]);
}
// Check if runtime translation is enabled
if (!developmentApiEnabled) {
return renderMessage(entry, [defaultLocale]);
}
// ----- TRANSLATE ON DEMAND ----- //
// development only
// Don't translate non-string entries
if (typeof entry !== 'string') {
return renderMessage(entry, [defaultLocale]);
}
// Translate Content
registerIcuForTranslation({
source: entry,
targetLocale: locale,
metadata: {
...(metadata?.$context && { context: metadata.$context }),
id,
hash: hash || getHash(),
},
});
// renderSettings.method must be ignored because t() is synchronous &
// t() should never return an empty string because functions reason about strings based on falsiness
return renderMessage(entry, [defaultLocale]);
},
[
dictionary,
dictionaryTranslations,
translations,
locale,
defaultLocale,
translationRequired,
developmentApiEnabled,
registerIcuForTranslation,
dialectTranslationRequired,
]
);
}