Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18n server #61

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions hapi18server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const fs = require('fs');
const path = require('path');

async function loadLocalesDir(options){
console.log('load locales dir');
let opts = {};
fs.readdirSync(options.directory).forEach(function (file) {
const local = file.split('.').slice(0, -1).join('.')
opts[local] = JSON.parse(fs.readFileSync(path.join(options.directory, file), 'utf8'));
});
console.log('fine opts');
return opts;
}

exports.plugin = {
name: 'hapi18server',
version: '0.1',

register: async function (server, options) {
let locales = await loadLocalesDir(options);
console.log('loc');
console.log(locales);

server.expose('getCatalog', function getCatalog() {
return locales;
});

//await someAsyncMethods();
server.ext('onPreAuth', function (request, h) {
request.i18n = {};
request.i18n.getCatalog = function (){
var locale = request.server.plugins['hapi18server'].getCatalog();
var param = request.query['lang']
if (!param){
return locale['it'];
}

return locale[param];
}
return h.continue;
});
}
}


Loading