pickstack

Vue + Vue I18n

Translations and language switching for Vue. This page shows exactly what pickstack adds to a Vue project for Vue I18n: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.

$ npm create pickstack@latest my-app -- --framework vue --modules vue-i18n --yes
Customize in the configurator

Packages it adds

PackageVersionType
vue-i18n^11.4.23runtime

New files

src/components/LanguageSwitcher.vue
<script setup lang="ts">
import { useI18n } from "vue-i18n";

const { t, locale } = useI18n();
const languages = [
  { code: "en", label: "English" },
  { code: "es", label: "Español" },
];
</script>

<template>
  <div>
    <p class="text">{{ t("greeting") }}</p>
    <div class="row">
      <button
        v-for="{ code, label } in languages"
        :key="code"
        type="button"
        class="btn"
        :aria-pressed="locale === code"
        @click="locale = code"
      >
        {{ label }}
      </button>
    </div>
  </div>
</template>
src/i18n.ts
import { createI18n } from "vue-i18n";

export default createI18n({
  legacy: false,
  locale: "en",
  fallbackLocale: "en",
  messages: {
    en: { greeting: "Hello! Pick a language." },
    es: { greeting: "¡Hola! Elige un idioma." },
  },
});

Changes to existing files

src/App.vue (changed)
 <script setup lang="ts"> import CounterCard from "./components/CounterCard.vue";+import LanguageSwitcher from "./components/LanguageSwitcher.vue"; </script>   <template>   <main class="app">     <h1 class="title">my-app</h1>     <CounterCard />+    <LanguageSwitcher />   </main> </template>  
src/main.ts (changed)
 import { createApp } from "vue"; import App from "./App.vue";+import i18n from "./i18n"; import "./assets/main.css";  +const app = createApp(App);+ +app.use(i18n);+ +app.mount("#app");−createApp(App).mount("#app");  

Project files