-
Notifications
You must be signed in to change notification settings - Fork 29
/
makeTranslator.ts
30 lines (25 loc) · 1.06 KB
/
makeTranslator.ts
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
import { Map } from 'immutable'
import { TranslatorRelative } from '@ui-schema/system/TranslatorRelative'
import { translation, Translator, TranslatorDictionary } from '@ui-schema/system/Translator'
/**
* Dictionary Factory
* pass in the dictionary that should be used, returns a new function that can work with it.
* - supports strings or functions as translation
* - supports relative schema translation (keyword `t`)
* - translation results should either be strings or React Components
*/
export const makeTranslator =
<T = translation>(
dictionary: TranslatorDictionary<T>,
locale: string = ''
): Translator<T> =>
(text, context = Map(), schema = undefined) => {
const schemaT = TranslatorRelative(schema, context, locale)
if (schemaT) return schemaT
if (typeof text !== 'string') return undefined
const trans = dictionary.getIn(text.split('.'))
if (typeof trans === 'function') {
return trans(context, locale)
}
return trans
}