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.

[Feature request] translations in mixins

See original GitHub issue

Reading other issues, you seem to discourage ppl from using mixins, but they’re so useful.

I would like to share some behavior between a bunch of components (especially across different builds, where “global” translations would still have to be duplicated)

const ChickenMixin = {
  methods: {
    scream () {
      alert(this.$t('scream'))
    }
  },
  i18n: {
    messages: {
      en: {
      	scream: 'cock-a-doodle-do'
      },
      fr: {
      	scream: 'cocorico'
      }
    }
  }
}

it does not work at the moment : http://jsfiddle.net/2hs4pgwe/2/

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:8
  • Comments:10

github_iconTop GitHub Comments

5reactions
yfwz100commented, Oct 10, 2019

I think adding a flag to enable mixin merging is a solution. Is there any plans for this feature? @kazupon

3reactions
pmrotulecommented, Dec 19, 2020

I had the same issue and found the solution using optionMergeStrategies a bit intrusive. I tried a few things and came up with this helper function which solved the problem:

export function mergeLocaleMessages(i18n, messages) {
  for (const locale in messages) i18n.mergeLocaleMessage(locale, messages[locale])
}

You can simply use it within a beforeCreate hook by providing the $i18n plugin of the current Vue instance (a.k.a. this.$i18n). As you don’t provide the i18n root instance, your translations will be scoped to the component which means you will still have component-based localization.

import { mergeLocaleMessages } from '@/helpers/i18n'
import i18nChicken from '@/locales/chicken.json'

const ChickenMixin = {
  beforeCreate() {
    mergeLocaleMessages(this.$i18n, i18nChicken)
  },
  methods: {
    scream() {
      alert(this.$t('scream'))
    },
  },
}

@/locales/chicken.json

{
  "en": {
    "scream": "cock-a-doodle-do"
  },
  "fr": {
    "scream": "cocorico"
  }
}

I personally like having my translations in separate files, but of course, you can define the object directly in your mixin. Hope it helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mixins - Vue.js
Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component...
Read more >
Including mixins using variables for name in Sass
I'm trying to call a mixin using a variable as the name, like so: ... This sounds more like a feature request for...
Read more >
Vue.js Mixins, extending components and High Order ...
The translate function looks for the translated string using the locale , configured by the Vue instance and the key. In case the...
Read more >
Mixins - The Modern JavaScript Tutorial
As defined in Wikipedia, a mixin is a class containing methods that ... An important feature of many browser objects (for instance) is...
Read more >
Apache Tapestry - Component Mixins
A mixin for this purpose would need access to at least the disabled, and value parameters, and possibly the translate parameter (for a...
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