π Custom Localized Routes with localeRoutes
in Nuxt I18n Micro
β
π Introduction to localeRoutes
β
The localeRoutes
feature in Nuxt I18n Micro
allows you to define custom routes for specific locales, offering flexibility and control over the routing structure of your application. This feature is particularly useful when certain locales require different URL structures, tailored paths, or need to follow specific regional or linguistic conventions.
π Primary Use Case of localeRoutes
β
The primary use case for localeRoutes
is to provide distinct routes for different locales, enhancing the user experience by ensuring URLs are intuitive and relevant to the target audience. For example, you might have different paths for English and Russian versions of a page, where the Russian locale follows a localized URL format.
π Example: Defining localeRoutes
in $defineI18nRoute
β
Hereβs an example of how you might define custom routes for specific locales using localeRoutes
in your $defineI18nRoute
function:
$defineI18nRoute({
localeRoutes: {
ru: '/localesubpage', // Custom route path for the Russian locale
de: '/lokaleseite', // Custom route path for the German locale
},
})
π How localeRoutes
Work β
- Default Behavior: Without
localeRoutes
, all locales use a common route structure defined by the primary path. - Custom Behavior: With
localeRoutes
, specific locales can have their own routes, overriding the default path with locale-specific routes defined in the configuration.
π± Use Cases for localeRoutes
β
π Example: Using localeRoutes
in a Page β
Hereβs a simple Vue component demonstrating the use of $defineI18nRoute
with localeRoutes
:
<template>
<div>
<!-- Display greeting message based on the current locale -->
<p>{{ $t('greeting') }}</p>
<!-- Navigation links -->
<div>
<NuxtLink :to="$localeRoute({ name: 'index' })">
Go to Index
</NuxtLink>
|
<NuxtLink :to="$localeRoute({ name: 'about' })">
Go to About Page
</NuxtLink>
</div>
</div>
</template>
<script setup>
import { useNuxtApp } from '#imports'
const { $getLocale, $switchLocale, $getLocales, $localeRoute, $t, $defineI18nRoute } = useNuxtApp()
// Define translations and custom routes for specific locales
$defineI18nRoute({
localeRoutes: {
ru: '/localesubpage', // Custom route path for Russian locale
},
})
</script>
π οΈ Using localeRoutes
in Different Contexts β
- Landing Pages: Use custom routes to localize URLs for landing pages, ensuring they align with marketing campaigns.
- Documentation Sites: Provide distinct routes for each locale to better match the localized content structure.
- E-commerce Sites: Tailor product or category URLs per locale for improved SEO and user experience.
π Best Practices for Using localeRoutes
β
- π Use for Relevant Locales: Apply
localeRoutes
primarily where the URL structure significantly impacts the user experience or SEO. Avoid overuse for minor differences. - π§ Maintain Consistency: Keep a consistent routing pattern across locales unless there's a strong reason to deviate. This approach helps in maintaining clarity and reducing complexity.
- π Document Custom Routes: Clearly document any custom routes you define with
localeRoutes
, especially in modular applications, to ensure team members understand the routing logic.