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 --yesCustomize in the configurator
Packages it adds
| Package | Version | Type |
|---|---|---|
vue-i18n | ^11.4.23 | runtime |
New files
<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>
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
<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>
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
.gitignoreREADME.mdenv.d.tsindex.htmlpackage.jsonsrc/App.vuesrc/assets/main.csssrc/components/CounterCard.vuesrc/components/LanguageSwitcher.vuesrc/i18n.tssrc/main.tstsconfig.app.jsontsconfig.jsontsconfig.node.jsonvite.config.ts