question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot set property 'locale' of undefined

See original GitHub issue

I get following error:

Cannot set property 'locale' of undefined
TypeError: Cannot set property 'locale' of undefined
    at Object.i18nSetLocale [as setLocale] (C:\Users\...\project\node_modules\i18n\i18n.js:404:25)
    at C:\Users\...\project\app.js:67:10
    at Layer.handle [as handle_request] (C:\Users\...\project\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\...\project\node_modules\express\lib\router\index.js:312:13)
    at C:\Users\...\project\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (C:\Users\...\project\node_modules\express\lib\router\index.js:330:12)
    at next (C:\Users\...\project\node_modules\express\lib\router\index.js:271:10)
    at i18nInit (C:\Users\...\project\node_modules\i18n\i18n.js:201:14)
    at Layer.handle [as handle_request] (C:\Users\...\project\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\...\project\node_modules\express\lib\router\index.js:312:13)
    at C:\Users\...\project\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (C:\Users\...\project\node_modules\express\lib\router\index.js:330:12)
    at next (C:\Users\...\project\node_modules\express\lib\router\index.js:271:10)
    at SendStream.error (C:\Users\...\project\node_modules\express\node_modules\serve-static\index.js:120:7)
    at emitOne (events.js:77:13)
    at SendStream.emit (events.js:169:7)
    at SendStream.error (C:\Users\...\project\node_modules\express\node_modules\send\index.js:245:17)
    at SendStream.onStatError (C:\Users\...\project\node_modules\express\node_modules\send\index.js:356:12)
    at next (C:\Users\...\project\node_modules\express\node_modules\send\index.js:658:28)
    at C:\Users\...\project\node_modules\express\node_modules\send\index.js:666:23
    at FSReqWrap.oncomplete (fs.js:82:15)

Here is my code:

i18n.configure({
  locales: ['nl', 'fr'],
  register: global,
  cookie: 'gmlang',
  directory: __dirname + '/locales',
  defaultLocale: 'nl'
});

app.use(i18n.init);

app.use(function (req, res, next) {
  console.log(i18n);
  if (req.query.lang != undefined && i18n.locales.indexOf(req.query.lang) >= 0) {
    i18n.setLocale(req.query.lang);
  }else{
    i18n.setLocale(i18n.defaultLocale);
  }
  next();
});

When logging the i18n I also get an empty locales object:

{ locales: {},
  version: '0.8.2',
  configure: [Function: i18nConfigure],
  init: [Function: i18nInit],
  __: [Function: i18nTranslate],
  __mf: [Function: i18nMessageformat],
  __l: [Function: i18nTranslationList],
  __h: [Function: i18nTranslationHash],
  __n: [Function: i18nTranslatePlural],
  setLocale: [Function: i18nSetLocale],
  getLocale: [Function: i18nGetLocale],
  getCatalog: [Function: i18nGetCatalog],
  getLocales: [Function: i18nGetLocales],
  addLocale: [Function: i18nAddLocale],
  removeLocale: [Function: i18nRemoveLocale] }

Any ideas?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mashpiecommented, May 21, 2016

but invalid! Nativ JSON has no support for comments. Remove you comments and check again.

0reactions
shivangidascommented, Nov 16, 2017

Removing res.setLocale(req.cookies.i18n); worked, not sure how or why

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot set property 'locale' of undefined, not working in replit ...
Repl.it is just not reading the sub-prototype 'locale' from your targetObject you should just try switching hosts :).
Read more >
TypeError: Cannot read property 'locale' of undefined at ...
You'll have to share your code if you want help, because this error says, only that somewhere where you try to take 'locale'...
Read more >
How to fix Vue I18n Uncaught TypeError - Our Code World
How to fix Vue I18n Uncaught TypeError: Cannot read property 'config' of undefined. I've been working on a Vue.js app that needs to...
Read more >
Locale issue. Cannot read property 'replace' of undefined
Locale issue. Cannot read property 'replace' of undefined ... We have been getting the above error and I am guessing it it because...
Read more >
Localization - The "Cannot read property '0' of undefined" error ...
You need to remove all Globalize scripts if you're going to use Intl. Let us know if you need further clarification on this....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found