Skip to content

🌍 <i18n-switcher> Component

The <i18n-switcher> component in Nuxt I18n Micro provides a user-friendly dropdown interface for switching between different locales in your application. This component is highly customizable, allowing seamless integration with your application's design and layout.

Locales with disabled: true are omitted from the dropdown (they remain available via $getLocales() for SEO / head tags). Only switchable locales get NuxtLink + hreflang.

⚙️ Props

PropTypeDefaultDescription
customLabelsRecord<string, string>() => ({})Display name per locale code, overriding the displayName from the locale config.
customWrapperStyleCSSProperties() => ({})Inline style for the outer wrapper.
customButtonStyleCSSProperties() => ({})Inline style for the button that opens the dropdown.
customDropdownStyleCSSProperties() => ({})Inline style for the dropdown panel.
customItemStyleCSSProperties() => ({})Inline style for each item in the dropdown.
customLinkStyleCSSProperties() => ({})Inline style for the link inside an item.
customActiveLinkStyleCSSProperties() => ({})Inline style for the link of the currently active locale.
customDisabledLinkStyleCSSProperties() => ({})Inline style for the current locale button when it is disabled: true in config.
customIconStyleCSSProperties() => ({})Inline style for the caret icon on the button.

Slots

SlotBindingsDescription
before-buttonContent before the button that opens the dropdown.
before-selected-localeContent before the active locale label inside the button.
after-selected-localeContent after the active locale label inside the button.
before-dropdownContent between the button and the dropdown panel.
before-dropdown-itemsContent at the top of the dropdown, above the locale list.
before-itemlocaleContent before each locale entry.
before-link-contentlocaleContent before a locale's label inside its link.
after-link-contentlocaleContent after a locale's label inside its link.
after-itemlocaleContent after each locale entry.
after-dropdown-itemsContent at the bottom of the dropdown, below the locale list.
after-dropdownContent after the dropdown panel.

Every part is unstyled by default: the custom*Style props take inline style objects, and the slots below replace the markup entirely when styling is not enough.

🛠️ Example Usages

Basic Usage

vue
<template>
  <i18n-switcher />
</template>

This renders a locale switcher with default styling and behavior.

Custom Labels and Inline Styles

vue
<template>
  <i18n-switcher
    :customLabels="{ en: 'English', fr: 'Français' }"
    :customWrapperStyle="{ backgroundColor: '#f8f9fa', padding: '10px' }"
    :customButtonStyle="{ backgroundColor: '#007bff', color: '#fff', borderRadius: '4px' }"
    :customDropdownStyle="{ border: '1px solid #007bff', boxShadow: '0 2px 8px rgba(0, 0, 0, 0.1)' }"
    :customItemStyle="{ margin: '5px 0', padding: '5px' }"
    :customLinkStyle="{ padding: '8px 16px', color: '#333', textDecoration: 'none' }"
    :customActiveLinkStyle="{ color: 'green', fontWeight: 'bold', backgroundColor: '#f0f0f0' }"
    :customIconStyle="{ fontSize: '20px', color: '#007bff' }"
  />
</template>

This example demonstrates a fully customized locale switcher with custom labels and inline styles.

🔌 Slots

Every visual part of the switcher can be replaced. The slots are listed with the props table above; each one takes template content:

vue
<i18n-switcher>
  <template #before-button>
    <span>🌐</span>
  </template>

  <template #before-item="{ locale }">
    <img :src="`/flags/${locale.code}.svg`" :alt="locale.code">
  </template>

  <template #after-dropdown-items>
    <a href="/help/languages">Missing a language?</a>
  </template>
</i18n-switcher>

Slots whose name ends in -item, -link-content or starts with before-item receive the locale object they belong to.

🎨 Styles Overview

The <i18n-switcher> component comes with basic styles defined using inline styles that can easily be overridden by passing custom styles via props. Here’s a brief overview of the default styling:

  • Wrapper: Controls the positioning of the dropdown.
  • Button: Styles the dropdown toggle button.
  • Dropdown: Styles the dropdown list.
  • Items: Styles each item in the list.
  • Links: Styles the links inside each item.
  • Icon: Styles the dropdown indicator icon.

You can customize these styles by passing custom styles through the respective props, ensuring that the locale switcher integrates seamlessly into your application's UI.

Released under the MIT License.