0

I don't have any problem in localizing the components and views strings but I am lock into finding a way to localize dynamically the Toolbar items ( and of course the same items in the navigation drawer..

Currently they are displayed in App.vue as menuItems[i].title

    <v-toolbar-items class="hidden-xs-only">
      <v-btn flat :to="menuItems[0].link">
        <v-icon left>{{ menuItems[0].icon }}</v-icon>
        <span>{{ menuItems[0].title }}</span>
      </v-btn>

with the script:

    <script>
    export default {
      data () {
        return {
          appName: 'myAPP',
          sideNav: false,
          menuItems: [
            { icon: 'home', title: 'Home', link: '/home' },
            { icon: 'info', title: 'About', menu: [{ title: 'Company', link: '/company' }, { title: 'Office', link: '/office' }] },
            { icon: 'people', title: 'Members', menu: [], link: '/members' },
            { icon: 'local_library', title: 'Blog', link: '/blog' },
            { icon: 'local_grocery_store', title: 'Shopping', link: '/shopping' }
          ]
        }
      },
      methods: {
          switchLocale: function (newLocale) {
            this.$store.dispatch('switchI18n', newLocale)
          }
        }
      }
    </script>

Should I use a computed value ? or use directly $t() in the template ?

feedback, advices and links appreciated

UPDATE

main.js

Vue.filter('translate', function (value) {
  if (!value) return ''
  value = 'lang.views.global.' + value.toString()
  return i18n.t(value)
})

locales/i18n/en_US

{
  "views": {
    "global": {
      "Home": "Home",
      "Section1": "Section 1",
      ..
  • Did you try `{{ $t("menuItems[0].title") }}`? what's wrong with that? – Traxo Apr 19 '18 at 17:24
  • I will consider [vue filter](https://vuejs.org/v2/guide/filters.html) – Sphinx Apr 19 '18 at 17:29
  • @Sphinx If I use it , I get the string "menuItems[1].title" displayed ... not the translation. –  Apr 20 '18 at 04:59
  • @Sphin Your second comment IS the answer ! Can you set it as an answer , so I can vote for it ... I used a global filter in my main.js .. awesome , never used filters before ... –  Apr 20 '18 at 05:11
  • @Sphinx . Ok, got it running fine ... see my UPDATE –  Apr 21 '18 at 06:56

1 Answers1

1

Vue provides filter to help us to format the common text.

So I think it will be one of your choices.

You can click above link to follow the guide to set up your filters.

Edit:

I just realized Vue-filters should not be dependent on this context as the Vue author said. So updated my answer as below:

Then the codes will be like below:

// create vue-i18n instance
const i18n = new VueI18n({
  locale: getDefaultLanguage(),
  messages: langs
})

// create global filter
Vue.filter('myLocale', function (value) {
  return i18n.t(value)
})

In your views or components:

<template>
    <v-toolbar-items class="hidden-xs-only">
      <v-btn flat :to="menuItems[0].link">
        <v-icon left>{{ menuItems[0].icon }}</v-icon>
        <span>{{ menuItems[0].title | myLocale }}</span>
      </v-btn>
</template> 

<script>
export default {
  data () {
    return {
      appName: 'myAPP',
      sideNav: false,
      menuItems: [
        { icon: 'home', title: 'Home', link: '/home' },
        { icon: 'info', title: 'About', menu: [{ title: 'Company', link: '/company' }, { title: 'Office', link: '/office' }] },
        { icon: 'people', title: 'Members', menu: [], link: '/members' },
        { icon: 'local_library', title: 'Blog', link: '/blog' },
        { icon: 'local_grocery_store', title: 'Shopping', link: '/shopping' }
      ]
    }
  },
  filters: {
      myLocaleWhichNotWork: function (value) {
        return this.$t(value) // this won't work because filters should not be dependent on this context
      }
    }
  }
</script>
tony19
  • 125,647
  • 18
  • 229
  • 307
Sphinx
  • 10,519
  • 2
  • 27
  • 45