Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $t function outside template #17

Closed
clementmas opened this issue Dec 21, 2015 · 8 comments
Closed

Use $t function outside template #17

clementmas opened this issue Dec 21, 2015 · 8 comments

Comments

@clementmas
Copy link

Hi,

I'm trying to do simple alert( $t('error') ) in my JavaScript file but $t is undefined.
I've also tried Vue.$t but it is also undefined.

Is there any way to use the $t function outside a template ?

Thank you,
clem.

@manudurgoni
Copy link

It works with vm.$t.

@aaronjpitts
Copy link

How can I use $t outside of a Vue component, for example in a service class which retrieves data from an API and sets strings? Another example is how could I also use it in my main init file where I set global things such as custom validator messages as below:

let required = Vue.validator('required');
Vue.validator('required', {
  message: function (field) {
    return 'This is a required field.'
  },
  check: required,
})

How could I use $t to translate the message above?

Thanks

@kazupon kazupon closed this as completed in 68935e3 Feb 6, 2016
@jezperp
Copy link

jezperp commented Sep 6, 2017

arronjpitts question is still valid, how do i translate text in components? and why is this closed?

@alsciende
Copy link

Seconded

@asvae
Copy link

asvae commented Oct 26, 2017

You can access translator globally like this:

import Vue from 'vue'

Vue.t('your.translation')

@po5i
Copy link

po5i commented Feb 16, 2018

I cannot use the Vue.t function on a single file component. The method is undefined.. Is there something more that I have to include at the <script> section?

@po5i
Copy link

po5i commented Feb 16, 2018

I figured it out like this comment at issue #149

@alex-gru
Copy link

alex-gru commented Dec 16, 2020

This approach works for me like a charm - I use this statement inside a store action:

this.app.i18n.t("example.message.key")

Found in the Vue forum here: https://forum.vuejs.org/t/how-to-use-t-from-vue-i18n-inside-vuex-action/22146/3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants