bundle-tools icon indicating copy to clipboard operation
bundle-tools copied to clipboard

Support TS format for messages

Open luisdeka opened this issue 2 years ago • 0 comments

Clear and concise description of the problem

I am using locale files on ".ts" format (can be .js not only .ts) and i would love it to work.

Why? Because i have enums in all my application and have the key centralized por easy change and consistency.

Now is throwing error, of course:

Uncaught (in promise) SyntaxError: Unexpected return type in composer (at message-compiler.esm-bundler.js:54:19)
    at createCompileError (message-compiler.esm-bundler.js:54:19)
    at createI18nError (vue-i18n.runtime.esm-bundler.js:100:12)
    at wrapWithDeps (vue-i18n.runtime.esm-bundler.js:545:19)
    at Object.transrateVNode (vue-i18n.runtime.esm-bundler.js:582:16)
    at Proxy.<anonymous> (vue-i18n.runtime.esm-bundler.js:1326:35)
    at renderComponentRoot (runtime-core.esm-bundler.js:914:44)
    at ReactiveEffect.componentUpdateFn [as fn] (runtime-core.esm-bundler.js:5649:57)
    at ReactiveEffect.run (reactivity.esm-bundler.js:190:25)
    at instance.update (runtime-core.esm-bundler.js:5763:56)
    at setupRenderEffect (runtime-core.esm-bundler.js:5777:9)

Suggested solution

Support .ts/.js files with "export default" locales/en-US.ts

import { MyEnum } from '@/enums'

export default {
  enums: {
      [MyEnum.first]: 'I am the text of enum first',
      [MyEnum.second]: 'I am the text of enum second',
  },
}

Alternative

The alternative would be to inject at VueI18nPlugin the locale/messages vite.config.js

VueI18nPlugin({
      include: resolve(__dirname, './src/locales/**'),
      runtimeOnly: true,
    }),

vite.config.js

import enUS from './locales/en-US.ts'
import jaJP from './locales/ja-JP.ts'

..
....
.......
VueI18nPlugin({
      include: { jaJP, enUS },
      runtimeOnly: true,
    }),
......
....
..

Additional context

This allows static keys to be persistent from source, the locale files.

type HaveEnum {
    enum: MyEnum
}
let data: HaveEnum = ....

t(`enum.${data.enum}`)
t(`enum.${MyEnum.first}`)

If you change enum, all continue to work without more changes

Validations

  • [X] Read the Contributing Guidelines.
  • [X] Read the README
  • [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.

luisdeka avatar Feb 24 '23 10:02 luisdeka