25

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?

Dvdgld
  • 1,984
  • 2
  • 15
  • 41

3 Answers3

50

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

Eric Guan
  • 15,474
  • 8
  • 50
  • 61
6
<template>
    <p v-if="te('some_key')">{{ t('some_key') }}</p>
</template>

<script setup>
import { useI18n } from 'vue-i18n';
...
const { t, te } = useI18n();
</script>
user19819191
  • 61
  • 1
  • 1
  • 1
    This seems to just be a part of the accepted answer. Can you explain in your answer what more helpful information this gives to answer the question. It is very rare that a snipped of code without explanation can be regarded as a good answer. – Dragonthoughts Aug 22 '22 at 13:10
  • Because you have wrapped the existing [accepted answer](https://stackoverflow.com/a/62602121/7758804) in some unrelated code, I can only assume it was posted as a "thank you" or "confirmation". Please don't add "thank you", or "confirmation" answers. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. [From Review](https://stackoverflow.com/a/73442999/7758804) – Trenton McKinney Aug 22 '22 at 17:27
1

I was interested in this as well and went to look up the documentation. The options above are good and might be best usable as the following can become quite messy when you have large translation files, but it comes from i18n self and might work for some as well:

https://www.i18next.com/principles/fallback

There is a line in there that you can specify the key as the actual sentence. So when that key is not available the key becomes the text:

i18next.t('No one says a key can not be the fallback.')

This 'No one says a key can not be the fallback.' is now the key in your translation file, but if it does not exist it will show this english version.

Again I would not do this if you have huge translation files as short keys keep it better readable and if someone is using it like this.. don't do full texts as keys.. ow what a mess. should I even post this? :p