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.

how to preserve programmatically added translations on the client?

See original GitHub issue

I am using localization translations from the legacy application backend (under the hood, it converts Java .properties files into JSON format understood by VueI18n).

The logic works as follows:

  async fetch ({ $axios, store, route, app }: Context) {
    const [page, lang] = route.name?.split('___') ?? []
    const res = await $axios.get(`http://localhost:8080/apiroot/${lang}/${page}:renderData`)
    const renderData = res.data as TapestryRenderData
    for (const componentKey in renderData.components) {
      store.commit(`${componentKey}/setup`, renderData.components[componentKey])
    }
    app.i18n.setLocaleMessage('en', renderData.messages)
    app.i18n.setLocale('en')
  },

I observe the following effect:

  • everything loads correctly and localizations are present during the first fraction of second when the pre-rendered page is displayed
  • then, client-side hydration happens and all localizations are lost

Is there is a way to either (a) avoid updating localizations during the client-side hydration or (b) somehow pass localizations from the SSR phase to client-side hydration phase (so I could programmatically set messages again in beforeMount hook, for example)?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
xfyrecommented, Jun 1, 2020

@rchl I have eventually managed to fix this. If you’re curious, here’s how:

  • (a) in the method called from asyncData (where I actually retrieve server-side localizations): call app.i18n.mergeLocaleMessage(lang, renderData.messages) before updating the store
  • (b) in the created component hook (in fact I use a mixin for this): retrieve current page messages from the Vuex store (they were saved there during asyncData call), call this.$i18n.mergeLocaleMessage(storedLocale, this.$store.getters['t5/i18n/getMessages'])

Item (a) fixes flickering and warnings during the SPA navigation. Item (b) fixes warnings when it’s going through SSR.

Downside: since I’m using mergeLocaleMessage, it’ll end up with all my server-side localizations in the browser memory (I’m not sure it’s a bad thing, but there’s certainly some room for optimizations).

Thanks a lot for your help!

0reactions
sugoidesunecommented, Mar 19, 2021

What I do now is:

async asyncData(context){
   // fetch translation for your source
   var translation = await fetch('/translation')

   // get locale of current page
   var locale = context.app.i18n.locale
   // set translation for Server Side Rendering
   context.app.i18n.mergeLocaleMessage(locale, translation)
   // save it for use on client side
   return {translation: translation}
},
created(){
   // prevent reverting back to not found after hard-loading page.
   this.$i18n.mergeLocaleMessage(this.$i18n.locale, this.translation)
}

It is unfortunate that the step at created is necessary. I haven’t experienced any flickers with this solution method.

SEO: nuxt async i18n

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to preserve programmatically added translations on the ...
I am using localization translations from the legacy application backend (under the hood, it converts Java .properties files into JSON format ...
Read more >
Are there any APIs for programmatically translating text on the ...
3 Answers 3 ... There is nothing limiting the Google Translate API to client side code. There is a RESTful Google Translate interface...
Read more >
How to Programmatically Translate Your App or Website with ...
We'll show you how to get started with Google API tools so you can add language auto-translation capabilities to apps and websites.
Read more >
How to publish latest revision langcode programmatically in ...
I try the following code on drush eval, but "de" translations are changed as first revision in paragaraph field. $langcode = "fr"; $nid...
Read more >
Add or Load Translations - i18next documentation
There are a few options to load translations to your application instrumented by i18next. The most common approach to this adding a so...
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