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