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.

Translate function breaks when used in store or asyncData

See original GitHub issue

Version

5.12.7

Reproduction link

https://github.com/Frankwarnaar/nuxt-i18n-translate-from-store-bug-report

Steps to reproduce

Run npm install and then npm run dev. Then open http://localhost:3000.

What is expected ?

In vue’s mounted lifecycle hook on the index page, I dispatch my action alertTranslation, in which I alert the output of i18n’s $t function.

What is actually happening?

When running the $t function in the store, an exception is thrown: TypeError: Cannot read property 'apply' of undefined

Additional comments?

I’ve seen the same issue before, when I was trying to use the $t function is nuxt’s asyncData lifecycle hook.

<div align="right">This bug report is available on Nuxt community (#c254)</div>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
rchlcommented, Apr 3, 2020

Actually it should just work (not sure if it was fixed or I was wrong before).

Example here: https://codesandbox.io/s/nuxt-i18n-string-from-asyncdata-fx64z

0reactions
lautrcommented, Apr 3, 2020

i’ll just leave this here in case somebody needs to use the translate method inside asyncData

export default ({ app }, inject) => {
  const _t = (key, values) => {
    // this looks odd, but is the way the translate has to be called outside of a template
    return app.i18n._t.apply(
      app.i18n,
      [key, app.i18n.locale, app.i18n._getMessages(), this].concat(values)
    )
  }

  inject('ta', (key, values) => {
    return _t(key, values)
  })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Translate function breaks when used in store or asyncData #343
Run npm install and then npm run dev . Then open http://localhost:3000. What is expected ? In vue's mounted lifecycle hook on the...
Read more >
Nuxt val.replace is not a function when get asyncData from APIs
Thats not the way to use asyncData . Your API call should be called inside the asyncData and then return your data.
Read more >
NuxtJS data, asyncData, fetch and nuxtServerInit - YouTube
Get data asynchronously from NuxtJS before redndering your app or use the Fetch / nuxtServerInit methods to populate your Vuex store on the ......
Read more >
Understanding Difference Between Asyncdata Fetch Nuxt
You can customize your fetch API calls using the following options: fetchOnServer (Boolean / Function | defaults to true ): When true ,...
Read more >
Vue to Nuxt: Server-Side Rendering Example
Most of the users probably will use Nuxt.js and their solution - just for a better ... That means, we have to call...
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