Get translations inside the components method
See original GitHub issueI am trying to use translations object inside the methods. I lost more than 1 hour with this and finally came with this solution, but it is really intuitive to me. Is there any shorter way, because I can’t find it in documentation.
export default {
data () {
return {
trans: this.$store.state.i18n.translations[this.$store.state.i18n.locale],
...
}
},
methods: {
...
setTitle (index, item) {
console.log('--- TITLE ---');
console.log(this.trans['services.' + index + '.items.' + item + '.title']);
return this.trans['services.' + index + '.items.' + item + '.title'];
}
...
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
laravel - How to use vue-i18n translate function in script part of ...
I'm using laravel-vue-i18n-generator package to handle text translation in vuejs component in my laravel ...
Read more >Create dynamic translations with HTML & Components in ...
Create dynamic translations with HTML & Components in Angular. Most translation libraries are focused on static text and do not allow you to ......
Read more >Prepare component for translation - Angular
To prepare your project for translation, complete the following actions. Use the i18n attribute to mark text in component templates; Use the i18n-...
Read more >How to translate your Vue.js application with vue-i18n
Learn how to organise your translations in .vue or .json files. ... The SCF method is great if you want to re-use your...
Read more >How to Add Localization (l10n) to Your React App with react ...
You then use the keys from the file in the components to get the text. You can add additional JSON files with the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t really get the wizardy portion of vue but turning the metaInfo from an object into a function got things cooking. Oh, and I had to move
this.$t()
into a lifestyle function instead of being inside data, which makes sense but I wasn’t thinking. Here’s the final working code.Thanks again for hanging out with me as I got through this!
Hi @kikky7,
You should be able to simply use
this.$t('key')
to access the translations in methods. However please be aware, that the method result will not update if the locale or translation is changed. For this to work, you must use computed properties.