mashpie / i18n-node

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

Object Notation: Cannot read property 'indexOf' of undefined #377

Open mrgcl opened 6 years ago

mrgcl commented 6 years ago

I'm using Express + Handlebars with the library. When I enable objectNotation, the project crashes. With object notation disabled, regular key/value pairs work just fine.

Relevant code:

i18n.configure({
    locales: availableLocales,
    defaultLocale: 'en',
    directory: __dirname + '/locales',
    cookie: 'locale',
    objectNotation: true
});

app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.png'))); // TODO: ver por que no funciona
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.engine('hbs', hbs.__express);
app.use(cookieParser());
app.use(i18n.init);
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

hbs.registerHelper('__', function () {
    return i18n.__.apply(this, arguments);
});
hbs.registerHelper('__n', function () {
    return i18n.__n.apply(this, arguments);
});

app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);

Route

router.get('/', function(req, res) {
    res.render('index', {});
});

Template

<p>{{{__ "this.is.nested"}}}</p>

Locale

{
    "this": {
        "is": {
            "nested": "whatever"
        }
    }
}

Error message:

TypeError: /Users/mrg/WebstormProjects/myproj/views/error.hbs: Cannot read property 'indexOf' of undefined
    at translate (/Users/mrg/WebstormProjects/myproj/node_modules/i18n/i18n.js:862:34)
    at Object.i18nTranslate (/Users/mrg/WebstormProjects/myproj/node_modules/i18n/i18n.js:220:13)
    at Object.<anonymous> (/Users/mrg/WebstormProjects/myproj/app.js:33:20)
    at Object.eval [as main] (eval at createFunctionContext (/Users/mrg/WebstormProjects/myproj/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:6:66)
    at main (/Users/mrg/WebstormProjects/myproj/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32)
    at ret (/Users/mrg/WebstormProjects/myproj/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12)
    at ret (/Users/mrg/WebstormProjects/myproj/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21)
    at /Users/mrg/WebstormProjects/myproj/node_modules/hbs/lib/hbs.js:87:17
    at /Users/mrg/WebstormProjects/myproj/node_modules/hbs/lib/hbs.js:69:11
    at Object.done (/Users/mrg/WebstormProjects/myproj/node_modules/hbs/lib/async.js:74:20)
lxinghe commented 3 years ago

Just encountered the same issue today in 2021. You have to turn that feature on by specifying the field "objectNotation" to be true in the configuration