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.

Access $t from outside this (Global $t function)

See original GitHub issue

I have a use case where I would like to translate keys in places like:

  • props
  • component data
  • vuex state

However, this.$t() is not available there.

I have seen people solve this when using vanilla vue-i18n import;

// i18n.js
import Vue from "vue";
import VueI18n from "vue-i18n";

Vue.use(VueI18n);

const messages = {
  en: {
    messages: {
      placeholder: "Placeholder"
    }
  },
  cz: {
    messages: {
      placeholder: "Zástupný symbol :)"
    }
  }
};

export default new VueI18n({
  locale: "en",
  messages
});
// some-file.vue
import i18n from "../i18n";

export default {
  name: "HelloWorld",
  props: {
    placeholder: {
      type: String,
      // default: this.$t("value") - Wont work as `this` is not Vue instance
      default: i18n.t("messages.placeholder") // Works
    }
  }
};

Is there an equivalent way to do this using the nuxt-i18n module, or do I have to switch to this approach above with the vanilla package?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
roselily8commented, May 8, 2021

Hi @rchl is there an example for scenario 2?

For any place where you don’t have access to $i18n or Nuxt context, you need to just pass the translation key and resolve it later.

What the best way to localize vee-validate custom rules in plugin files?

0reactions
rchlcommented, May 12, 2021

I don’t know vee-validate but maybe you can create a Nuxt plugin from which you would do something like:

import { configure } from 'vee-validate';

export default function({ app }) {
  configure({
    generateMessage: context => {
      return app.i18n.t(context.rule.name);
    },
  });
}

I haven’t tested it and you would likely also need to extend this logic more but maybe it will work as a start.

reference: https://vee-validate.logaretm.com/v4/guide/i18n

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access $t from outside this (Global $t function) #1026 - GitHub
I have a use case where I would like to translate keys in places like: props component data vuex state However, this.$t() is...
Read more >
Giving my function access to outside variable - Stack Overflow
If you want your function to have access to an outer variable, you have to declare it as global , inside the function...
Read more >
Access local variable in global scope (outside function)
How to access local variable outside function ? reprex: x = 1 y <- function(a){ x = x + 2 z = x...
Read more >
Troubleshooting compliance error messages for a work or ...
If you get the You can't get there from here message saying that your device is out-of-compliance with your organization's access policy, make...
Read more >
Python Global Keyword (With Examples) - Programiz
In Python, the global keyword allows us to modify the variable outside of the current scope. It is used to create a global...
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