Set a cookie when locales are accessed
Allows server to know language with each request
This commit is contained in:
parent
1aa4d6346d
commit
9f736d4731
|
@ -11,7 +11,12 @@ async function routes(fastify, options) {
|
|||
response.locale = fastify.i18n[request.params.locale];
|
||||
}
|
||||
|
||||
return response;
|
||||
return reply.setCookie('lang', request.params.locale, {
|
||||
path: '/',
|
||||
expires: new Date('December 31, 9999'), // Don't expire
|
||||
maxAge: new Date('December 31, 9999'), // Both are set as a "just in case"
|
||||
sameSite: true, // Prevents the cookie from being used outside of this site
|
||||
}).send(response);
|
||||
});
|
||||
|
||||
fastify.get('/locales/:locale/page/:page', async (request, reply) => {
|
||||
|
|
|
@ -82,6 +82,9 @@ fastify.addHook('onRequest', async (request, reply) => {
|
|||
request.user = user;
|
||||
}
|
||||
}
|
||||
if (typeof request.cookies.lang !== 'undefined') {
|
||||
request.language = request.cookies.lang;
|
||||
}
|
||||
});
|
||||
|
||||
// Store i18n files in fastify object and register locales routes
|
||||
|
|
Loading…
Reference in New Issue