Ghost/core/shared/lang/i18n.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

var fs = require('fs'),
config = require('../../server/config'),
/**
* Create new Polyglot object
* @type {Polyglot}
*/
I18n;
I18n = function (ghost) {
// TODO: validate
var lang = ghost.settings('defaultLang'),
path = config.paths.lang,
langFilePath = path + lang + '.json';
return function (req, res, next) {
if (lang === 'en_US') {
// TODO: do stuff here to optimise for en
// Make jslint empty block error go away
lang = 'en_US';
}
/** TODO: potentially use req.acceptedLanguages rather than the default
* TODO: handle loading language file for frontend on frontend request etc
* TODO: switch this mess to be promise driven */
fs.stat(langFilePath, function (error) {
if (error) {
console.log('No language file found for language ' + lang + '. Defaulting to en_US');
lang = 'en_US';
2013-05-11 20:44:25 +04:00
}
fs.readFile(langFilePath, function (error, data) {
2013-05-11 20:44:25 +04:00
if (error) {
throw error;
2013-05-11 20:44:25 +04:00
}
try {
data = JSON.parse(data);
} catch (e) {
throw e; // TODO: do something better with the error here
}
2013-05-11 20:44:25 +04:00
ghost.polyglot().extend(data);
2013-05-11 20:44:25 +04:00
next();
2013-05-11 20:44:25 +04:00
});
});
2013-05-11 20:44:25 +04:00
};
};
2013-05-11 20:44:25 +04:00
module.exports.load = I18n;