Skip to content

Instantly share code, notes, and snippets.

@defaye
Created March 11, 2021 12:38
Show Gist options
  • Save defaye/04e9eb0ad59a978f2e5a9ccf9a01e9a0 to your computer and use it in GitHub Desktop.
Save defaye/04e9eb0ad59a978f2e5a9ccf9a01e9a0 to your computer and use it in GitHub Desktop.
Translation helper to be used with messages in a Vue store
import { at } from 'lodash'
Vue.prototype.$t = function (translationKey, options) {
const rawTranslation = at(this.$store.state.translations.translations, translationKey)
let translation = rawTranslation[0]
for (let key in options) {
translation = translation.replace(`%{${key}}`, options[key])
}
return translation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment