2013-06-25 15:43:15 +04:00
|
|
|
var fs = require('fs'),
|
|
|
|
/**
|
|
|
|
* Create new Polyglot object
|
|
|
|
* @type {Polyglot}
|
|
|
|
*/
|
|
|
|
I18n;
|
|
|
|
|
|
|
|
I18n = function (ghost) {
|
|
|
|
|
|
|
|
// TODO: validate
|
2013-09-15 20:03:31 +04:00
|
|
|
var lang = ghost.settings().defaultLang.value,
|
2013-06-25 15:43:15 +04:00
|
|
|
path = ghost.paths().lang,
|
|
|
|
langFilePath = path + lang + '.json';
|
|
|
|
|
|
|
|
return function (req, res, next) {
|
|
|
|
|
2013-08-16 18:11:36 +04:00
|
|
|
if (lang === 'en_US') {
|
2013-06-25 15:43:15 +04:00
|
|
|
// TODO: do stuff here to optimise for en
|
|
|
|
|
|
|
|
// Make jslint empty block error go away
|
2013-08-16 18:11:36 +04:00
|
|
|
lang = 'en_US';
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 20:58:26 +04:00
|
|
|
/** 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 */
|
2013-06-25 15:43:15 +04:00
|
|
|
fs.stat(langFilePath, function (error) {
|
|
|
|
if (error) {
|
2013-08-16 18:11:36 +04:00
|
|
|
console.log('No language file found for language ' + lang + '. Defaulting to en_US');
|
|
|
|
lang = 'en_US';
|
2013-05-11 20:44:25 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
fs.readFile(langFilePath, function (error, data) {
|
2013-05-11 20:44:25 +04:00
|
|
|
if (error) {
|
2013-06-25 15:43:15 +04:00
|
|
|
throw error;
|
2013-05-11 20:44:25 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
try {
|
|
|
|
data = JSON.parse(data);
|
|
|
|
} catch (e) {
|
2013-06-25 20:58:26 +04:00
|
|
|
throw e; // TODO: do something better with the error here
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.polyglot().extend(data);
|
2013-05-11 20:44:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
next();
|
2013-05-11 20:44:25 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-11 20:44:25 +04:00
|
|
|
};
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-05-11 20:44:25 +04:00
|
|
|
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports.load = I18n;
|