-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathi18n.js
35 lines (32 loc) · 950 Bytes
/
i18n.js
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
const { renderFn, get } = require('../')
const localizationTable = {
en: {
greet: 'Hi! My name is {{name}} and today is {{today}}',
dayNames: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
},
sv: {
greet: 'Hej! Jag heter {{name}} och idag är det {{today}}',
dayNames: ['måndag', 'tisdag', 'onsdag', 'tursdag', 'fredag', 'lördag', 'söndag'],
},
}
function __(key, scope, lang) {
return renderFn(
localizationTable[lang][key],
(path) => {
const resolvedValue = get(scope, path)
if (resolvedValue instanceof Date) {
return localizationTable[lang].dayNames[resolvedValue.getDay()]
}
return resolvedValue
},
scope
)
}
const scope = {
name: 'Alex',
today: new Date(),
}
// Hi! My name is Alex and today is Friday
console.log(__('greet', scope, 'en'))
// Hej! Jag heter Alex och idag är det fredag
console.log(__('greet', scope, 'sv'))