intlify / vite-plugin-vue-i18n

:globe_with_meridians: Vite plugin for Vue I18n
MIT License
127 stars 8 forks source link

Unable to import messages as JSON files (Vite/Typescript) #63

Closed gevzak closed 3 years ago

gevzak commented 3 years ago

Hello. I am migrating a project from Vue2 to Vue3 and all locales files were stored in JSON format in /src/locales folder. In order to import them I attempted to use the recommended solution import messages from @intlify/vite-plugin-vue-i18n/messages, which sadly does not seem to work because @intlify/vite-plugin-vue-i18n/messages is missing module/type delarations. The error message is "Cannot find module '@intlify/vite-plugin-vue-i18n/messages' or its corresponding type declarations.ts(2307)". When I try to run it, I get "Cannot read property 'message' of undefined" error message. I am using the following versions: vue-i18n: ^9.0.0-rc.7, @intlify/vite-plugin-vue-i18n: ^2.0.0-rc.1, vue: ^3.0.5,

Could you please advise if I am missing something or if there is a way to bypass it. Thank you very much.

ananni13 commented 3 years ago

I think the TS error and the runtime error are unrelated.

The first one is just a TS error that your IDE (I assume VSCode? I had the same issue) shows you because there's no declaration file for the @intlify/vite-plugin-vue-i18n/messages module. The app should run and build normally even with that error. You can fix it by adding this snippet to a declaration file (like shims.d.ts) in your src folder:

declare module '@intlify/vite-plugin-vue-i18n/messages' {
  import { LocaleMessages } from '@intlify/core-base'
  import { VueMessageType } from 'vue-i18n'
  const messages: LocaleMessages<VueMessageType>
  export default messages
}

(@kazupon maybe you could include a client.d.ts file in the published package? Like vite itself or vite-plugin-pages, for example. Then, users will just need to add the type to their tsconfig.json like this)

For the second error, "Cannot read property 'message' of undefined", did you correctly configure the plugin with the locales directory path in your vite.config.ts file? Refer to the docs here, it's the include property.

kazupon commented 3 years ago

Thank you for your feedback!

I've just fixed it! You can check the @intlify/vite-plugin-vue-i18n lastet version :)

kazupon commented 3 years ago

You need to setting ts config. See the here: https://github.com/intlify/vite-plugin-vue-i18n#client-types