Take a look at this code snippet:
import Vue from 'vue'
import Component from 'vue-class-component'
@Component
export default class SomeComponent extends Vue {
public someText = this.$t('some.key')
}
An error is being thrown:
[Vue warn]: Error in data(): "TypeError: i18n is undefined"
I have made sure to initialize Vue with Vue.use(VueI18n)
and new Vue({ /* ... */, i18n })
. The initialization of the i18n
object looks like this:
new VueI18n({
locale: DEFAULT_LOCALE, // imported
fallbackLocale: 'en',
messages // imported
})
Translations seem to be working fine unless they are immediately called, such as in templates or component methods.
This particular issue regarding vue-i18n suggests that there might be an initialization problem.
A workaround could involve utilizing methods and translating only within templates. Nevertheless, there are instances where immediate calls cannot be avoided, for instance when dealing with Vuetify input rules
.
someRule = [(v) => !!v || this.$t('field.must_not_be_empty')]
These rules are triggered instantly, even with lazy-validation
activated on Vuetify forms.
I have identified two potential solutions:
- Finding a way to bypass the Vuetify
rules
system and directly include a string for translation in the template; - Solving the immediate availability issue of
$t
.
However, I have not been successful in implementing either option.
Is there any known solution to tackle this issue?