intlify / intlify/bundle-tools

Support TS format for messages

Open
#228 0 comments 2 reactions 0 assignees View on GitHub
status: proposal
Dominant language
TypeScript
Stars
270
Forks
46
PR merge metrics
No merged PRs in 30d

Description

### 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:
```shell
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. (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**_
```shell
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**_
```shell
VueI18nPlugin({
include: resolve(__dirname, './src/locales/**'),
runtimeOnly: true,
}),
```
_**vite.config.js**_
```shell
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.

```shell
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](https://github.com/intlify/bundle-tools/blob/main/.github/contributing.md).
- [X] Read the README
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.