vue-custom-element icon indicating copy to clipboard operation
vue-custom-element copied to clipboard

i18n - sync language change on custom element

Open pawepaw opened this issue 3 years ago • 2 comments

Hi,

I've configured to pass i18n to my web component but when I change language on page where I have this web component embedded it's not synced with language on web component. How can I keep it in sync?

beforeCreateVueInstance: rootElement => {
    rootElement.i18n = i18n
    return rootElement
  }

and i18n

const i18n = new VueI18n({
  locale: 'en',
  fallbackLocale: 'en',
  messages,
  silentTranslationWarn: true
})

export default i18n

Where messages is just traditional json with translations.

The only way I figured out is to just pass language via binding but it seems to be stupid.

pawepaw avatar Dec 09 '20 12:12 pawepaw

Can You please prepare CodeSandbox?

karol-f avatar Dec 09 '20 12:12 karol-f

In your parent web page (where the Web Component is being used) when you change the locale - you should also update the locale attribute of the HTML tag for your Web Component. In your Vue app you should declare locale as a String prop on your App.vue and add a watcher for it. The watcher should change the locale of your Vue-i18n inside the Web Component. IF you want a two-way sync - you should emit an event from your App.vue whenever the locale is changed from inside your WebComponent as opposed to the wrapping web page.

tmcdos avatar Nov 08 '23 10:11 tmcdos