using i18n in Vue typescripte

Viewed 309

While learning TS on VUE there was such a problem

I have a component for rendering a menu, where I connect data

...
import ArrowRoundedDown9x6Svg from '~/svg/arrow-rounded-down-9x6.svg'
import headerNavigation from '~/data/headerNavigation'

@Component({
    components: { AppLink, Megamenu, Menu, ArrowRoundedDown9x6Svg }
})
export default class NavLinks extends Vue {
    items: INav = headerNavigation
    hoveredItem: INavLink | null = null
...

in it I connect data

import headerNavigation from '~/data/headerNavigation'

however, in this file I need to connect i18n to switch the language and I don’t know how to do it. there is no constructor in it

import { INav } from '~/interfaces/menus/nav'
import { TranslateResult } from 'vue-i18n'   // I create import 

const dataHeaderNavigation: INav = [
{
    title: "Home"
    url: '/',
}
export default dataHeaderNavigation

and accordingly I get an error if I try to do this

const dataHeaderNavigation: INav = [
{
    title: this.TranslateResult.get("header.links.Home").subscribe(value => { this.Home= value });
           ^^^
    url: '/',
}

Error: Object is possibly 'undefined'.

en.json

{
    "header": {
        "links": {
            "Home": "Home Page"
        }
    }
}

how to make a link to the translated element correctly?

1 Answers

Need to add a Vue instance

import Vue from 'vue'
import { INav } from '~/interfaces/menus/nav'
const dataHeaderNavigation: (vue: Vue) => INav = vue => [
    {
        title: vue.$t('header.menu.home'),
        url: '/'
    }]

Also need to check that the interface allows you to return

import {LocaleMessage} from 'vue-i18n'
export interface ILink {
    title: string | LocaleMessage;
    url?: string;
}
Related