TypeError: _ctx.$t is not a function when using $t in child component

See original GitHub issue

vue & vue-i18n version

  • vue: 3.0.0
  • vue-i18n: 9.0.0-rc.7

Reproduction Link

https://github.com/boussadjra/vue3-tailwind2

Steps to reproduce

I added a file named i18n.ts next to main.ts which contains the following snippet :

import { createI18n } from "vue-i18n";

const i18n = createI18n({
  legacy: false,
  locale: "ja",
  messages: {
    en: {
      message: {
        language: "English",
        greeting: "Hello !"
      }
    },
    ar: {
      message: {
        language: "العربية",
        greeting: "السلام عليكم"
      }
    },
    es: {
      message: {
        language: "Español",
        greeting: "Hola !"
      }
    }
  }
});
export default i18n;

then I imported the file inside main.ts and use it :

...
import i18n from './i18n'
let root = app.use(i18n).use(store).use(router).mount('#app')

then I added the following snippet to home.vue :

<h2>{{ $t("message.greeting", {}, { locale: "en" }) }}</h2>

What is Expected?

Render the message Hello !

What is actually happening?

but this is giving the following error :

Uncaught (in promise) TypeError: _ctx.$t is not a function at Proxy.render (Home.vue?bb51:2)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:24

github_iconTop GitHub Comments

132reactions
danielpetricacommented, Feb 19, 2021

Hi, I think this may be similar to my issue, I found out that to have the global $t injected like on previous version I neet to pass globalInjection: true, in the createI18n() function options.

So your i18n.ts may look like this:

import { createI18n } from "vue-i18n";

const i18n = createI18n({
  legacy: false,
  locale: "ja",
  globalInjection: true,
  messages: {
    en: {
      message: {
        language: "English",
        greeting: "Hello !"
      }
    },
    ar: {
      message: {
        language: "العربية",
        greeting: "السلام عليكم"
      }
    },
    es: {
      message: {
        language: "Español",
        greeting: "Hola !"
      }
    }
  }
});
export default i18n;
7reactions
hks2002commented, Apr 27, 2021

Vue3 and vue-i18n-next sample, it could works as bellow:

<template>
  <q-page class="flex flex-center">
    <img alt="Quasar logo" src="/imgs/logo.svg" />
    <div>{{ t("success") }}</div>
  </q-page>
</template>

<script>
import { defineComponent } from "vue";
import { useI18n } from "vue-i18n";

export default defineComponent({
  name: "PageIndex",
  setup() {
    const { t } = useI18n();
    return {
      t,
    };
  },
});
</script>
  • import useI18n:
import { useI18n } from "vue-i18n";
  • define function in setup():
const { t } = useI18n();
  • using t in the template:
<div>{{ t("success") }}</div>
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: ctx.onCreditChange is not a function angular
CHILD COMPONENT. The error start here, because Output is not a function, it's an object that allow to you to send events to...
Read more >
vue-i18n插件报错:Uncaught TypeError: _ctx.$t is not a function
在vite脚手架项目当中,使用vue-i18n插件进行国际化多语言时,爆出. Uncaught TypeError: _ctx.$t is not a function.
Read more >
How to solve the "is not a function" error in JavaScript
js we use require() to load external modules and files. This can cause, in some cases, an error like this: TypeError: require(...) is...
Read more >
Uncaught TypeError | Is Not A Function | Solution - YouTube
Transcript · DIV to PDF, Iframe to PDF using JavaScript Only ( with CSS & Images Support) · Top 5 Excel Functions for...
Read more >
Making the most out of Vue.js Injections - Abdelrahman Awad
In other words, it "provides" some dependency to child components. ... allows you to pass complex reactive objects along with functions and ...
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 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