Composition API
You can access both the translation function
t
as well as the
i18next
instance used by
i18next-vue
using the composition API.
Make sure to set up i18next-vue for your app beforehand.
Even without calling
useTranslation()
you can use
$t
and
$i18next
in your
<template>
part.
Customize
t
Since 3.0
useTranslation()
can be used to get a custom
t
function with a specific namespace, language and/or key prefix:
useTranslation()
supports passing in a single name space or an array of namespaces to use. These namespaces will be loaded, if they are not available yet.
As a second argument, you can pass an object setting the
keyPrefix
(i.e. a prefix to put before all translation keys passed to this
t
) and/or a fixed translation language using
lng
.
Both this special
t
and the default
$t
(unaffected by the options) can be used alongside each other in the template.
You can call
useTranslation
multiple times, to e.g. create multiple translation functions for different namespaces.
TIP
If you use certain namespaces/key prefixes a lot, it might make sense to extract composition functions for those to a separate JS module and use these in your components.