javascriptvuejs3vuetify.js

How can I switch between languages during runtime?


I'm getting errors trying to switch language of the Vuetify. Following this guide to set locale but useLocale function throws

Error: [Vuetify] Could not find injected locale instance

Here my attempt in Vuetify Playground. Getting the same error:

<template>
  <v-app>
    <v-container>
      <v-btn text="Change Language" @click="switchLanguage" />
    </v-container>
  </v-app>
</template>

<script setup>
  import { useLocale } from 'vuetify'

  function switchLanguage() {
    const locale = useLocale()
    locale.current.value = locale.current.value === 'en' ? 'tr' : 'en'
  }
</script>

Solution

  • You have to run useLocale() during setup (in your example, you run it on click):

    <script setup>
      import { useLocale } from 'vuetify'
    
      const locale = useLocale()
    
      function switchLanguage() {
        locale.current.value = 'tr'
      }
    </script>
    

    Here is the updated playground.