Nuxt.js
This guide demonstrates how to integrate Clocale into your Nuxt.js application, supporting both client-side rendering (CSR) and server-side rendering (SSR) for a seamless internationalization experience.
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.
Client-Side Setup
Step 1: Register the Client Plugin
Create a client-side plugin to enable reactive translations throughout your application. This plugin initializes Clocale with client-side capabilities and reads the user's locale preference from a cookie:
What this does:
- Retrieves the user's saved locale from a cookie (defaults to
'en') - Configures Clocale to fetch translations from your specified endpoint
- Enables development mode helpers when running in dev environment
Step 2: Use Translations in Your Components
After setting up the plugin, you can access translations and locale controls in any component:
Key composables:
useTranslation('namespace')- Provides thet()function for accessing translationsuseLocale()- Gives you the current locale and ability to switch languages
Server-Side Rendering (SSR)
For optimal performance and SEO, you'll want translations to be available during server-side rendering. This eliminates the "flash of untranslated content" and ensures search engines see properly localized pages.
Step 1: Configure the Server Plugin
Create a server-side plugin that preloads translations before rendering your pages:
This plugin fetches translations on the server for each request, ensuring your HTML is fully translated before it reaches the browser.
Step 2: Create Server Utilities
Build reusable utilities for server-side translation fetching:
This creates a per-request Clocale client that properly handles locale isolation in SSR contexts.
Step 3: Set Up Locale Management
Create helpers to read and write the user's locale preference using cookies:
These utilities provide a single source of truth for locale management across your application.
Step 4: Create a Locale API Endpoint
Build a server API route to handle locale updates from the client:
Since cookies can only be set on the server, this endpoint allows your client-side components to update the locale preference. When users switch languages, the API persists the change server-side for subsequent SSR requests.
Step 5: Use SSR Translations in Components
Here's a complete example showing server-side translation fetching with dynamic locale switching:
Notes: Keep the cookie/locale logic consistent between server and client to avoid hydration mismatches.
Quick Reference
| Composable/Utility | Purpose | Usage Context |
|---|---|---|
useTranslation('namespace') | Access translations | Client & Server components |
useLocale() | Get/set current locale | Client-side only |
getTranslation('namespace') | Fetch server translations | Server-side data fetching |
getClocaleClient() | Get server client instance | Server plugin |
getLocale() | Retrieve saved locale | Server & Client utilities |
setLocale(locale) | Save locale preference | Server & Client utilities |