Translation module for hapi with the mongodb support
npm install hapi-i18n-mongo
JavaScript example:
{
path: '/path',
method: 'GET',
handler: function ( request, h ){
return h.response({
message: request.i18n.__( "My localized string" )
});
})
}
server.register(
{
register: require( "hapi-i18n-mongo" ),
options : {
db : {
host : 'localhost',
port : 27017,
db : 'admin',
username : '',
password : '',
collection: 'api_translation'
}
}
}
);
To set the locale have to use the Accept-Language
header. If the header was not set, english will be taken as default locale
const i18n = require('hapi-i18n-mongo');
server.ext({
type : 'onRequest',
method: function(request, h){
request.i18n = i18n;
locale = request.headers['Accept-Language'] ? request.headers['Accept-Language']: "en";
request.i18n.setLocale(locale);
return h.continue();
}
});
request.i18n.getLocale();
MongoDB schema have to be like this
{
key: 'translation_key',
language: [
{
code: 'language_code',
translation: 'translated key'
}
]
}