index.js 781 B

12345678910111213141516171819202122232425262728293031323334
  1. import messages from './fr.json'
  2. function getMessage (keyedPath, alt) {
  3. let message
  4. try {
  5. message = keyedPath.split('.').reduce((parent, key) => {
  6. return parent[key]
  7. }, messages)
  8. } catch {
  9. // Silence errors like accessing a key on `undefined`.
  10. }
  11. return message === undefined
  12. ? alt === undefined ? keyedPath : alt
  13. : message
  14. }
  15. /*
  16. * Allows separtion of static text content from its templates.
  17. */
  18. export default class VueMessages {
  19. static install (Vue) {
  20. // Adds `this.$t('path.to.string', 'alt')` to every instances.
  21. Vue.prototype.$t = getMessage
  22. // Adds `v-t="'path.to.string'"` directive to every elements.
  23. Vue.directive('t', {
  24. bind (el, { value }) {
  25. el.textContent = getMessage(value)
  26. }
  27. })
  28. }
  29. }