Search code examples
vue.jsvuexvuejs3vue-i18n

How to use vue i18n $t in vuex getters in vue 3?


I want to use i18n $t in my getters for a static label. How can I do that?

I've tried importing like this:

import { i18n } from '../../locales/i18n.js';

const getters = {
    guaranteePolicies: state => {
        let guaranteesState = state.rangeUpdateData.guarantees;
        let guarantees = [{
            value : "",
            label :  i18n.t("chooseGuarantee"),
            disabled : true
        }];
        for (let index in guaranteesState) {
            guarantees.push({
                value : guaranteesState[index].ID,
                label : guaranteesState[index].Name
            });
        }
        return guarantees;
    }
}

but it gives me the error Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 't')

Thanks in advance :)


Solution

  • Figure it out how to use it!

    Instead of importing i18n with curly brackets, I'm importing like this:

    import i18n from '../../locales/i18n.js'
    

    and for accessing the $t function, I'm using

            label : i18n.global.t("chooseGuarantee")
    

    and it's working perfectly! hope this helps someone :)