question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get translations inside the components method

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
daveo1001commented, Dec 15, 2017

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.


export default {
  data: () => ({
    title: '' // can't put this.$t() here
  }),
  metaInfo () {
    return {
      title: this.title
    }
  },
  created () {
    this.title = this.$t('login')
  }
}

Thanks again for hanging out with me as I got through this!

3reactions
tikiatuacommented, Dec 12, 2017

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found