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

Request with no "accept-language" header #2

Closed knation closed 11 years ago

knation commented 11 years ago

"preferredLocale" function in i18n.js fails when there is no accept-language header. Proposed fix:

preferredLocale: function(req) {
        req = req || this.request;

        if (!req || !req.headers) {
            return;
        }

        var accept = req.headers["accept-language"],
            self = this,
            prefLocale;

        if (accept) {
            accept.match(/(^|,\s*)([a-z]+)/g).forEach(function(locale) {
                if (!prefLocale && self.locales[locale]) {
                    prefLocale = locale;
                }
            });
        } else {
            prefLocale = self.defaultLocale;
        }

        return prefLocale;
    }
sylvinus commented 11 years ago

I've fixed this and another undefined variable issue here: https://github.com/sylvinus/i18n-node-2/commit/85ffea9dd2b7fdcbc0d84adaa11fbaf9c39ab419

Waiting for John to check my previous PR before submitting a new one!

sylvinus commented 11 years ago

This should have been fixed now