-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
61 lines (51 loc) · 1.64 KB
/
index.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
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
// @ts-check
import i18n from 'i18n'
const I18n = i18n.I18n
/** @type {i18n.ConfigurationOptions} */
const defaults = {
queryParameter: 'lang',
directory: 'locales',
indent: ' ',
extension: '.json',
objectNotation: true,
logDebugFn: (_msg) => { },
logWarnFn: (_msg) => { },
logErrorFn: (_msg) => { },
}
/** @type {import('trifid-core/types').TrifidPlugin} */
const factory = async (trifid) => {
const { config, registerTemplateHelper, server } = trifid
// Force user to define the `directory` parameter
if (!config.directory || typeof config.directory !== 'string') {
throw new Error(
"The 'directory' configuration field should be a non-empty string.",
)
}
const i18nInstance = new I18n({
...defaults,
...config,
})
/**
* Hook to configure the language in the locals.
*
* @param {import('fastify').FastifyRequest<{ Querystring: { lang: string }}> & { session: Map<string, any> }} request Request.
* @param {import('fastify').FastifyReply} _reply Reply.
* @param {import('fastify').DoneFuncWithErrOrRes} done Done function.
*/
const onRequestHookHandler = (request, _reply, done) => {
const session = request.session
const currentLanguage = session.get('currentLanguage') || session.get('defaultLanguage') || 'en'
i18nInstance.setLocale(currentLanguage)
const t = (/** @type {string} **/ phrase) => i18nInstance.__({
phrase,
locale: currentLanguage,
})
session.set('t', t)
registerTemplateHelper('i18n', (/** @type {string} **/ value) => {
return t(value)
})
done()
}
server.addHook('onRequest', onRequestHookHandler)
}
export default factory