Skip to content

Commit

Permalink
chore: fix i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
kernoeb committed Apr 25, 2024
1 parent 6558b8d commit 7091e50
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 3 additions & 1 deletion server/resources/messages-en.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"notifications.message": "Answer by {{userName}} to your comment on {{comment}}"
}
4 changes: 3 additions & 1 deletion server/resources/messages-fr.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"notifications.message": "Réponse de {{userName}} à votre commentaire : {{content}}"
}
6 changes: 4 additions & 2 deletions server/routers/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ router.post('', asyncWrap(async (req, res) => {
send({
sender: { type: 'organization', id: req.user.activeAccount.id },
topic: { key: 'social::message-posted' },
title: { en: 'New message posted' },
extra: { message },
title: req.__all('notifications.message', {
userName: req.body.responseTo.user.name,
content: req.body.content
}),
recipient: { id: req.body.responseTo.user.id },
outputs: []
}).catch(console.error)
Expand Down
12 changes: 12 additions & 0 deletions server/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ const messages = {
en: { ...fr, ...require('../resources/messages-en.json') }
}

function getObjectI18n (__, key, args) {
return {
fr: __({ phrase: key, locale: 'fr' }, args),
en: __({ phrase: key, locale: 'en' }, args),
es: __({ phrase: key, locale: 'es' }, args),
pt: __({ phrase: key, locale: 'pt' }, args),
it: __({ phrase: key, locale: 'it' }, args),
de: __({ phrase: key, locale: 'de' }, args)
}
}

exports.middleware = (req, res, next) => {
const locales = acceptLangParser.parse(req.get('Accept-Language'))
const localeCode = req.cookies.i18n_lang || (locales && locales[0] && locales[0].code) || config.i18n.defaultLocale
req.locale = localeCode
req.messages = messages[localeCode] || messages.fr
req.__all = (key, args) => getObjectI18n(req.__, key, args)
next()
}

0 comments on commit 7091e50

Please sign in to comment.