Files
the-example-app-nodejs/lib/breadcrumb.js
David Litvak Bruno 81577c82fd Enhance Breadcrumbs (#39)
* chore(localization): remove duplicated locale files

* feat(localization): add localization for breadcrumbs

* feat(localization): add beadcrumb enhancements

* feat(debugging): add npm run debug command

* chore(localization): add unit tests

* chore(breadcrumbs): fix tests and typos

* chore(localization): add docs for translationAvailable

* chore(localization): refactor translationAvailable
2017-11-08 15:39:24 +01:00

41 lines
1.2 KiB
JavaScript

const url = require('url')
const { translate, translationAvaliable } = require('../i18n/i18n')
module.exports = (modifier) => {
return (request, response, next) => {
const baseUrl = url.format({ protocol: request.protocol, host: request.get('host') })
const urlComponents = url.parse(request.url).pathname.split('/').filter(Boolean)
let breadcrumbs = []
breadcrumbs.push({
label: translate('homeLabel', response.locals.currentLocale.code),
url: baseUrl
})
// Map components of the path to breadcrumbs with resolvable URLs
let mappedComponents = urlComponents.map((component, i, array) => {
const currentLocale = response.locals.currentLocale
const path = array.slice(0, i + 1).join('/')
let label = component.replace(/-/g, ' ')
if (translationAvaliable(`${label}Label`, currentLocale.code)) {
label = translate(`${label}Label`, currentLocale.code)
}
return {
label: label,
url: url.resolve(baseUrl, path),
path: path
}
})
breadcrumbs = breadcrumbs.concat(mappedComponents)
if (modifier) {
breadcrumbs = breadcrumbs.map(modifier)
}
// Make it global
request.app.locals.breadcrumb = breadcrumbs
next()
}
}