Ghost/core/shared/lang/i18n.js
Harry Wolff f16dc290b7 Improve bootstrap flow of a Ghost application
addresses #1789, #1364

- Moves ./core/server/loader -> ./core/bootstrap.
The bootstrap file is only accessed once during startup,
and it’s sole job is to ensure a config.js file exists
(creating one if it doesn’t) and then validates
the contents of the config file.

Since this is directly related to the initializing 
the application is is appropriate to have 
it in the ./core folder, named bootstrap as that
is what it does.

This also improves the dependency graph, as now
the bootstrap file require’s the ./core/server/config
module and is responsible for passing in the validated
config file.

Whereas before we had ./core/server/config
require’ing ./core/server/loader and running its
init code and then passing that value back to itself,
the flow is now more straight forward of
./core/bootstrap handling initialization and then
instatiation of config module

- Merges ./core/server/config/paths into 
./core/server/config
This flow was always confusing me to that some config
options were on the config object, and some were on
the paths object.

This change now incorporates all of the variables
previously defined in config/paths directly
into the config module, and in extension,
the config.js file.

This means that you now have the option of deciding
at startup where the content directory for ghost
should reside.

- broke out loader tests in config_spec to bootstrap_spec

- updated all relevant files to now use config().paths

- moved urlFor and urlForPost function into 
 ./server/config/url.js
2014-02-07 17:34:21 -05:00

54 lines
1.4 KiB
JavaScript

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';
}
fs.readFile(langFilePath, function (error, data) {
if (error) {
throw error;
}
try {
data = JSON.parse(data);
} catch (e) {
throw e; // TODO: do something better with the error here
}
ghost.polyglot().extend(data);
next();
});
});
};
};
module.exports.load = I18n;