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.

Render an app with externally loaded translations

See original GitHub issue
// ~/plugins/i18n.js
export default function ({ app }) {
  app.i18n.loadedLanguages = []

  app.router.beforeEach((to, from, next) => {
    loadLanguageAsync(app.i18n.locale).then(() => next())
    next() // still debugging this
  })

  async function loadLanguageAsync (lang) {
    // If the language was already loaded
    if (app.i18n.loadedLanguages.includes(lang)) {
      return Promise.resolve()
    }

    const { data } = await app.$axios.get('translate', { params: { lang } })
    app.i18n.loadedLanguages.push(lang)
    app.i18n.setLocaleMessage(lang, data.data)
    return Promise.resolve()
  }
}
// ~/nuxt.config.js
export default {
  plugins: [
    { src: '~/plugins/i18n.js' }
  ],
  modules: [
    '@nuxtjs/axios',
    'nuxt-i18n'
  ],
  i18n: {
    locales: [
      { code: 'en', iso: 'en-US', name: 'English' },
      { code: 'lv', iso: 'lv-LV', name: 'Latviešu' }
    ],
    defaultLocale: 'en',
    strategy: 'prefix',
    seo: false,
    vueI18n: {
      fallbackLocale: 'en',
      messages: {}
    }
  },
}

My main goal would be to render the page with the correct translations. I might add sligh delay while the initial language file loads or whatever, but currently this does not work well. The thing that I would like to see is that I dont have to manage translations on frontend side, nor on backend. At the moment I keep my translations in database behind backend.

I know that couple of years ago I had this thing working, but that was on Vue, and I haven’t written too much frontends till back then.

I really would like to hear some suggestions or hints how to achieve these things.

Thanks!

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
rchlcommented, Jan 24, 2021

You must register the axios module after this module to make axios available.

0reactions
Rozkalnscommented, Jan 24, 2021
// nuxt.config.js
export default {
  modules: [
    'nuxt-i18n',
    '@nuxtjs/axios'
  ],
  i18n: {
    lazy: true,
    langDir: 'lang/',
    locales: [
      { code: 'en', iso: 'en-US', name: 'English', file: 'en.js' },
      { code: 'lv', iso: 'lv-LV', name: 'Latviešu', file: 'lv.js' }
    ],
    defaultLocale: 'en',
    strategy: 'prefix',
    seo: false
  }
}```

```js
// ~/lang/en.js
// ~/lang/lv.js
export default async ({ app }, locale) => {
  const { data } = await app.$axios.get('translate', { params: { locale } })
  return data.data
}

it works like a charm!

Thanks @rchl for helping out!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Translating React Apps with i18next | by Daniel Duan | Medium
i18next-parser — automate extraction of strings embedded within JavaScript code to a dedicated JSON file to submit for translations.
Read more >
How to Render Components Outside the Main ReactJS App
React just renders the elements where they are defined within the JSX. ... the .content parent element of the modal a transform: translate(…); ......
Read more >
Translation outside React Component · Issue #909 - GitHub
I tried to translate strings exported outside React Components. But any methods in docs doesn't work, and I guess its normal, ...
Read more >
Step by step guide - react-i18next documentation
The Trans component is the best way to translate a JSX tree in one translation. This enables you to eg. easily translate text...
Read more >
How to translate your React app with react-intl / FormatJS
npm install react-intl ; ReactDOM.render(<App />, document.getElementById('root')); ; import {FormattedMessage} from 'react-intl'; ; npm install @formatjs/intl- ...
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