Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

useI18n(); in setup function throws error #14

Closed
karladler opened this issue Oct 11, 2021 · 10 comments
Closed

useI18n(); in setup function throws error #14

karladler opened this issue Oct 11, 2021 · 10 comments

Comments

@karladler
Copy link

karladler commented Oct 11, 2021

calling useI18n() in composition API setup creates an error, since I updated my versions.

import { useI18n } from 'vue-i18n-composable';
// ... 

export default defineComponent({
  // ...
  setup(props) {
    const { t } = useI18n();

creates this:

[Vue warn]: Error in data(): "Error: [vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function."

Versions:
vue-i18n: 8.26.5
vue-i18n-composable@npm:0.2.2

main.ts (minimal version)

import Vue from 'vue';
import VueCompositionApi from '@vue/composition-api';
import de from '../locales/de.json';
import { createI18n } from 'vue-i18n-composable';

Vue.use(VueCompositionApi);

const i18n = createI18n({
  silentFallbackWarn: true,
  locale:  'de',
  fallbackLocale: 'de',
  messages: { de },
});

new Vue({
  render: (h) => h(App),
  i18n,
}).$mount('#app');

Is this still the correct way to use i18n programatically?

Edit: updated usage if of useI18n(); and added main.ts

@xitrox
Copy link

xitrox commented Oct 13, 2021

Struggling with the same problem.
Also it's the same behavior for me when I use the example and just:
setup() { return { ...useI18n() } }

Would like to add more value to this but can't since I'm also using the same versions.

@cyio
Copy link

cyio commented Nov 20, 2021

@karladler @xitrox

solved problem for me

import { useI18n } from 'vue-i18n-composable/src/index'

import { createI18n } from 'vue-i18n-composable/src/index'

@bigbossx
Copy link

bigbossx commented Nov 30, 2021

Has anyone solved this problem?Struggling with the same problem when i run example in this project.
I think that if the example on the repository cannot run normally, at least they should be marked as unavailable first, otherwise it will cause a lot of confusion.

maybe fix by #15

@Hienphamthe
Copy link

the trick from @cyio works for me

@bigbossx
Copy link

see vuejs/composition-api#837

@nagisaando
Copy link

nagisaando commented May 18, 2022

In my case, I was using script setup and import { useI18n } from 'vue-i18n-composable' was working okay, but started throwing the error in testing, and I changed the path as @cyio suggested and ta-da! Now it's working perfectly, thank you!

@kingyue737
Copy link
Contributor

I also meet this error in vitest

@nagisaando
Copy link

@kingyue737 Did you try #14 (comment)? I am using vitest as well, but changing path in all vue files fixed error

@kingyue737
Copy link
Contributor

Thanks @nagisaando , it should work. But I don't want to directly import source code from package. I would have liked to migrate to vue-i18n-bridge which seems to have a valid export field in package.json (so won't have the bug mentioned in this issue), but I found vue-i18n v9 and vue-i18n-bridge do not support using both option api and composition api in one project.

Finally, I modify the package.json file of vue-i18n-composable by patch-package to fix bug. It's a good way to patch packages no longer maintained.

@dawid-polak
Copy link

I solved this problem by installing the latest version of i18n.
command: npm install vue-i18n@9

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants