Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueI18n check if translation exists

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?

like image 294
David Go Avatar asked Jun 26 '20 16:06

David Go


People also ask

What is Vue i18n localization?

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.

How to check if context is equal to component instance in Vue?

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.

How to avoid V-HTML in Vue I18N with I18N?

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>.

How to display plurals in Vue i18n messages?

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 |.


2 Answers

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

like image 52
Eric Guan Avatar answered Oct 16 '22 20:10

Eric Guan


<template>
    <p v-if="te('some_key')">{{ t('some_key') }}</p>
</template>

<script setup>
import { useI18n } from 'vue-i18n';
...
const { t, te } = useI18n();
</script>
like image 36
user19819191 Avatar answered Oct 16 '22 19:10

user19819191