0
votes

I'm working on Vue project supporting two languages using VUEX and Vuetify.

Instead of direct referencing the text directly like this:
{{ $vuetify.t('$vuetify.my-component.text') }}, I put it as a state in a namespaced VUEX store module, then reference it as a mapstate computed property like this: {{ textProp }}
and in the script computed I put ...mapState('language', ['textProp']) which the language is a VUEX module:

export default {
    namespaced,
    state() {
        return {
            textProp: Vue.prototype.$vuetify.t('$vuetify.textProp')
        }
    }
}

Now lets come to my issue:
I need to loop through a list of items, each item contain a dynamic text which it change according to the selected language, so this is the HTML template:


  <v-list>
    <v-list-tile
            v-for="item in items"
            :key="item.title"
            :to="item.to"
            router>

      <v-list-tile-action>
        <v-icon>{{ item.icon }}</v-icon>
      </v-list-tile-action>

      <v-list-tile-content>
        <v-list-tile-title>

            {{ item.title }}

        </v-list-tile-title>
      </v-list-tile-content>

    </v-list-tile>
  </v-list>

and the script is:

export default {
    data() {
        return {
            items: [
                { title: this.home, to: '/', icon: 'home' },
                { title: this.orgsTxt, to: '/orgs', icon: 'business' },
                { title: this.peopleTxt, to: '/people', icon: 'people' },
                { title: this.servicesTxt, to: '/services', icon: 'store' }
            ],
        }
    },
    computed: {
        ...mapState('language', [
            'home',
            'orgsTxt',
            'peopleTxt',
            'servicesTxt',
        ]),
    },
}

my problem is with the referencing the text in title, and I can't put it created() because the text will not change when the user change the language, and I don't wont to hard code each list item.

sorry for explaining too much and thanks in advance.

1

1 Answers

0
votes

I fix it by adding a method instead of referencing the title from the array:

HTML template:

  <v-list>
    <v-list-tile
            v-for="(item, index) in items"
            :key="index"
            :to="item.to"
            router>

      <v-list-tile-action>
        <v-icon>{{ item.icon }}</v-icon>
      </v-list-tile-action>

      <v-list-tile-content>
        <v-list-tile-title>

            {{ navItem(index) }}

        </v-list-tile-title>
      </v-list-tile-content>

    </v-list-tile>
  </v-list>

script:

methods: {
    navItem(id){
        if(id === 0) return this.home;
        if(id === 1) return this.orgsTxt;
        if(id === 2) return this.peopleTxt;
        if(id === 3) return this.servicesTxt;
    }
},

now everything is working just fine.