Is there a way to check if translation exist?
<p v-if="$t('some_key')">{{ $t('some_key') }}</p>
In this case if there is no translation it will print 'some_key'. Maybe there is a way to configure global fallback?
In this article, we’ll walk you through everything you need to dive into Vue localization with the extremely popular third-party Vue I18n library. Vue I18n plugs into your Vue apps and provides you with translation file management, message formatting, date and number formatting, and more to boot.
Note that you need to guarantee this context equal to component instance in lifecycle methods (e.g. in data options, const $tc = this.$tc.bind (this) ). Check whether key exists. In Vue instance, If not specified component locale messages, check with global locale messages. If you specified locale, check the locale messages of locale.
We can use Vue I18n’s <i18n> component to avoid v-html. First, let’s update our translation messages. Instead of having the <br /> directly in our message, we have a {0} placeholder. Now let’s update our Footer to use the <i18n> component. The "footer" key corresponds to the one in our translation files, and is provided as the path prop to <i18n>.
Vue I18n’s $tc () function can help with displaying plurals. To use it, we need to adopt a special syntax in our pluralized message. Let’s update our English message to see this. English has three plural forms: zero, one, and many. We write all three forms in our message, separated by the |.
You can use the $te
method.
Example usage:
<p v-if="$te('some_key')">{{ $t('some_key') }}</p>
https://kazupon.github.io/vue-i18n/api/#vue-injected-methods
<template>
<p v-if="te('some_key')">{{ t('some_key') }}</p>
</template>
<script setup>
import { useI18n } from 'vue-i18n';
...
const { t, te } = useI18n();
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With