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:
- Created 3 years ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You must register the axios module after this module to make axios available.
it works like a charm!
Thanks @rchl for helping out!