Vue.js
This guide walks you through integrating Clocale into your Vue.js application for seamless internationalization (i18n) support.
Getting Started
Installing Dependencies
Begin by adding the Clocale package to your Vue.js project using npm:
This package provides all the necessary tools and composables for managing translations in your Vue application.
Configuration
Setting Up the Plugin
Configure the Clocale plugin in your main application file. This establishes the translation system across your entire Vue app:
Configuration Options:
- type: Set to
'client'to enable client-side fetching of translations from your Clocale endpoint - locale: The initial locale for your application (e.g.,
'en'for English,'fr'for French,'es'for Spanish) - baseUrl: Your Clocale HTTP endpoint URL where translation files are hosted
- isDev: Enable development mode for additional helpers and encoding features during development
Managing Locales
Switching Languages Dynamically
Use the useLocale composable to access and change the current locale throughout your application:
The locale reactive value always reflects the current language, and setLocale() allows you to change it programmatically based on user interaction or preferences.
Using Translations
Translating Content in Components
The useTranslation composable provides the t function for accessing translated strings within your components:
Key Points:
- Pass a namespace (e.g.,
'common') touseTranslation()to organize your translations by feature or section - Use dot notation (e.g.,
'home.title') to access nested translation keys - The
t()function automatically returns the translation for the current locale
This pattern keeps your components clean and makes translations easy to manage as your application grows.
DevTool (Optional)
The DevTool highlights translatable elements and opens an editor on Alt+Click.
Note: If your editor dialog requires an API key, be sure your integration provides it (for example via your extension or app storage).
Quick Reference
| Composable/Utility | Purpose | Usage |
|---|---|---|
useTranslation('namespace') | Access translations | const { t } = useTranslation('common') |
useLocale() | Get/set current locale | const { locale, setLocale } = useLocale() |
DevTool | Visual translation editor | <DevTool>...</DevTool> |