Skip to content

Commit

Permalink
fix: i18n usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kernoeb committed Apr 26, 2024
1 parent 7091e50 commit 488227c
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 26 deletions.
90 changes: 90 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"event-to-promise": "^0.8.0",
"express": "^4.19.2",
"http-terminator": "^3.2.0",
"i18n": "^0.15.1",
"iframe-resizer": "^4.3.11",
"js-cookie": "^3.0.5",
"mongodb": "^5.9.2",
Expand Down
2 changes: 1 addition & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (process.env.NODE_ENV === 'development') {
// Create a mono-domain environment with other services in dev
const { createProxyMiddleware } = require('http-proxy-middleware')
app.use('/openapi-viewer', createProxyMiddleware({ target: 'http://localhost:5680', pathRewrite: { '^/openapi-viewer': '' } }))
app.use('/simple-directory', createProxyMiddleware({ target: 'http://localhost:6001', pathRewrite: { '^/simple-directory': '' } }))
app.use('/simple-directory', createProxyMiddleware({ target: 'http://localhost:8080', pathRewrite: { '^/simple-directory': '' } }))
app.use('/notify', createProxyMiddleware({ target: 'http://localhost:6009', pathRewrite: { '^/notify': '' } }))
}

Expand Down
2 changes: 1 addition & 1 deletion server/resources/messages-en.json → server/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"notifications.message": "Answer by {{userName}} to your comment on {{comment}}"
"notifications.message": "Answer by {{userName}} to your comment on {{content}}"
}
File renamed without changes.
3 changes: 2 additions & 1 deletion server/routers/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ObjectId = require('mongodb').ObjectId
const findUtils = require('../utils/find')
const asyncWrap = require('../utils/async-wrap')
const { send } = require('../utils/notifications')
const { getObjectI18n } = require('../utils/i18n')

const ajv = new Ajv()
ajvFormats(ajv)
Expand Down Expand Up @@ -46,7 +47,7 @@ router.post('', asyncWrap(async (req, res) => {
send({
sender: { type: 'organization', id: req.user.activeAccount.id },
topic: { key: 'social::message-posted' },
title: req.__all('notifications.message', {
title: getObjectI18n(req, 'notifications.message', {
userName: req.body.responseTo.user.name,
content: req.body.content
}),
Expand Down
39 changes: 16 additions & 23 deletions server/utils/i18n.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
const config = require('config')
const acceptLangParser = require('accept-language-parser')
const i18n = require('i18n')

const fr = require('../resources/messages-fr.json')
const messages = {
fr,
en: { ...fr, ...require('../resources/messages-en.json') }
}
const locales = ['fr', 'en']
const defaultLocale = 'fr'

i18n.configure({
locales,
directory: './server/i18n',
defaultLocale,
fallbacks: { en: 'fr' },
directoryPermissions: '444'
})

exports.middleware = i18n.init

function getObjectI18n (__, key, args) {
exports.getObjectI18n = (i18n, 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)
fr: i18n.__({ phrase: key, locale: 'fr' }, args),
en: i18n.__({ phrase: key, locale: 'en' }, 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 488227c

Please sign in to comment.