Below is an example of how to implement internationalization (i18n) in a Vue 3 project with TypeScript for handling two languages: English (en) and Russian (ru).
We'll use the official vue-i18n plugin for localization.
Steps to integrate i18n with Vue 3 & TypeScript:
1. Install Dependencies
Install the required dependency vue-i18n:
npm install vue-i18n
2. Set up project structure
Organize your project for localization. An example structure might look like:
src/
├── i18n/
│ ├── en.ts
│ ├── ru.ts
│ └── index.ts
├── main.ts
├── App.vue
3. Create Translation Files
src/i18n/en.ts:
export default {
message: {
hello: 'Hello!',
welcome: 'Welcome to Vue 3 i18n example!',
changeLanguage: 'Change Language',
},
};
src/i18n/ru.ts:
export default {
message: {
hello: 'Привет!',
welcome: 'Добро пожаловать на пример Vue 3 i18n!',
changeLanguage: 'Сменить язык',
},
};
4. Create the i18n Instance
src/i18n/index.ts:
import { createI18n } from 'vue-i18n';
import en from './en'; // English language file
import ru from './ru'; // Russian language file
export const messages = {
en,
ru,
};
// Create i18n instance
const i18n = createI18n({
legacy: false, // Use Vue 3 Composition API mode
locale: 'en', // Default language
fallbackLocale: 'en', // Fallback language
messages, // Messages
});
export default i18n;
5. Initialize i18n in main.ts
src/main.ts:
import { createApp } from 'vue';
import App from './App.vue';
import i18n from './i18n/index';
const app = createApp(App);
// Install i18n instance
app.use(i18n);
app.mount('#app');
6. Add i18n to App.vue
src/App.vue:
<template>
<div>
<h1>{{ $t('message.hello') }}</h1>
<p>{{ $t('message.welcome') }}</p>
<button @click="switchLanguage('en')">{{ $t('message.changeLanguage') }}: English</button>
<button @click="switchLanguage('ru')">{{ $t('message.changeLanguage') }}: Russian</button>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { useI18n } from 'vue-i18n';
export default defineComponent({
name: 'App',
setup() {
const { locale } = useI18n();
// Function to switch languages
const switchLanguage = (lang: string) => {
locale.value = lang; // Update the current locale
};
return {
switchLanguage,
};
},
});
</script>
Explanation
-
i18n Instance:
In src/i18n/index.ts, we set up an i18n instance using the createI18n method. We specify the available languages (en and ru) and configure the default locale.
-
Reactive Locale Change:
The useI18n hook allows reactive access to the locale and translation functions. By calling locale.value = lang, the language is instantly updated.
-
Translation Keys:
Use $t('key') within your templates to retrieve the translation based on the current language.
-
Dynamic Language Switching:
We've provided two buttons to switch between English and Russian in the App.vue file.
Result
- By default, the app is displayed in English.
- You can switch the language dynamically by clicking the buttons.
With this setup, your Vue 3 and TypeScript application is ready for multilingual support with i18n!