Skip to content

useLocale ​

Language of the components' default texts: accessible labels ("Clear selection", "Close"…), placeholders, error messages and screen reader announcements. French is the default locale; English is provided.

An explicit component prop always wins over the locale.

Choosing the language ​

ts
// main.ts
import { createApp } from 'vue'
import SurgeUI, { locales } from '@surgeui/ds-vue'

createApp(App)
  .use(SurgeUI, { locale: locales.en })
  .mount('#app')

Switching language at runtime ​

provideLocale accepts a ref or a getter: every component in the subtree follows.

vue
<script setup>
import { ref } from 'vue'
import { provideLocale, locales } from '@surgeui/ds-vue'

const lang = ref('en')
provideLocale(() => locales[lang.value])
</script>

<template>
  <SuButton @click="lang = lang === 'fr' ? 'en' : 'fr'">FR / EN</SuButton>
  <RouterView />
</template>

The plugin's locale option accepts a ref too.

Customizing messages ​

defineLocale completes a locale with partial messages; the others stay those of the base.

ts
import SurgeUI, { defineLocale, locales } from '@surgeui/ds-vue'

const myEnglish = defineLocale(locales.en, {
  messages: {
    selectBox: { placeholder: 'Pick one…' },
    holyGrailLayout: { skipLink: 'Skip navigation' }
  }
})

app.use(SurgeUI, { locale: myEnglish })

For another language, start from a complete locale and give a BCP 47 code (de, es, fr-CA…): it drives plural rules and formats.

ts
const de = defineLocale(locales.en, {
  code: 'de',
  messages: {
    common: { loading: 'Wird geladen…', close: 'Schließen' }
    // …
  }
})

Messages with parameters and plurals ​

A message holds {name} parameters, or an object of plural forms picked with Intl.PluralRules from {count}:

ts
{
  removeTag: 'Remove {label}',
  selectedCount: { one: '{count} item selected', other: '{count} items selected' }
}

In English only 1 takes the one form; in French, 0 and 1 do.

In your components ​

vue
<script setup>
import { computed } from 'vue'
import { useLocale } from '@surgeui/ds-vue'

const props = defineProps({ clearLabel: String })
const { t, formatBytes } = useLocale()

const label = computed(() => props.clearLabel ?? t('selectBox.clear'))
const count = computed(() => t('fileUpload.selectedCount', { count: 3 })) // "3 files selected"
const size = formatBytes(1536) // "1.5 kB" (en), "1,5 ko" (fr)
</script>

API ​

useLocale() ​

ReturnTypeDescription
localeComputedRef<SurgeLocale>Current locale
t(key, params?) => stringTranslated message; the key is typed (namespace.message)
formatBytes(bytes: number) => stringFile size in the current language

Functions ​

FunctionDescription
provideLocale(locale)Provides a locale (complete, partial, ref or getter) to the subtree; it completes the parent's
defineLocale(base, overrides)Creates a locale from a base and partial messages
formatMessage(message, params?, code?)Formats a message (plural, parameters)
locales.fr, locales.enProvided locales

Message namespaces ​

NamespaceComponentsKeys
commonButton, Avatar, Dropdown, Dialog, Popoverloading, close
formFieldFormFieldrequired
alertAlertdismiss
avatarAvataralt, anonymous, initials, placeholder, status, statusOnline, statusOffline, statusBusy, statusAway, notifications
imageImageloading, error, errorText
linkLinkexternal
progressProgressindeterminate
spinnerSpinnerlabel
textareaTextarearemaining, overLimit
selectBoxSelectBoxplaceholder, searchPlaceholder, noOptions, noResults, clear, removeTag, selectedCount, selected, deselected, removed, cleared, maxReached
checkboxGroupCheckboxGroupmaxReached
sliderSlidermin, max
fileUploadFileUploadplaceholder, drop, browse, loading, loadingHint, tooLarge, invalidType, maxFiles, added, removed, cleared, selectedCount, preview, remove, progress, maxSize
otpInputOtpInputlabel, progress, character
passwordPasswordstrength
toastToast, Toasterregion, dismiss
breadcrumbBreadcrumblabel, expand
paginationPaginationlabel, page, previous, next, first, last
tagTagremove
numberInputNumberInputincrement, decrement
stepperStepperlabel, complete, error
confirmConfirmDialog, useConfirmtitle, confirm, cancel
autocompleteAutocompletenoResults, loading, results, clear
dataTableDataTableselectAll, selectRow, sortAscending, sortDescending, sortCleared, empty
calendarCalendarpreviousMonth, nextMonth, previousYear, nextYear, previousYears, nextYears, chooseMonth, chooseYear
datePickerDatePickeropen, openSelected, dialog, day, month, year
commandPaletteCommandPalettelabel, placeholder, empty, results
gridLayoutGridLayoutviewToggle, grid, list
holyGrailLayoutHolyGrailLayoutskipLink, nav, aside

Default values shown on component pages are those of the French locale, except values marked "(locale)", which are the English ones. The demos on the English pages of this documentation use locales.en.

Publié sous licence MIT.