jeresig / i18n-node-2

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
507 stars 79 forks source link

readFile error checking #35

Closed ryleyb closed 8 years ago

ryleyb commented 10 years ago

While deploying my express 3.X app the first time to production, I ran into an issue with readFile where it wasn't able to read the locales directory, and also didn't have permissions to write new files. Relevant code from i18n.js (https://github.com/jeresig/i18n-node-2/blob/master/i18n.js#L268):

try {
    var localeFile = fs.readFileSync(file);
    //... continues
} catch (e) {
    this.writeFile(locale);
}

The end result is that my app complains that it can't find res.locals.__. Which makes sense - the middleware for express in expressBind calls new i18n(opts) which isn't successful, and thus (silently AFAIK) dies.

I think a reasonable thing to do might be to wrap the writeFile call in a try/catch and log an error. I can create a PR if that seems reasonable to you.

(PS: I'm not asking for help with my app - I just needed to explicitly pass the directory option)

ryleyb commented 10 years ago

Probably would have made my life easier if I had logging enabled in production.

paintedbicycle commented 10 years ago

I had this problem, too. Agreed it would be nice to fail gracefully.

Paul

gjuchault commented 9 years ago

I can create a PR if that seems reasonable to you.

Please do :smile: