intlify / intlify/h3

Use translator after onAfterResponse

Open
#80 0 comments 0 reactions 0 assignees View on GitHub
status: review needed
Dominant language
TypeScript
Stars
46
Forks
6
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

That line of code cause problems - https://github.com/intlify/h3/blob/19a13c62e86f91b8a37178844fe32ef57d04ba0e/src/index.ts#L142

```
Translation error: undefined is not an object (evaluating 'event.context.i18n.locale = locale')

at useTranslation (node_modules/@intlify/h3/dist/index.mjs:41:19)
at processTicksAndRejections (native:7:39)

Translation error: middleware not initialized, please setup onRequest and onAfterResponse options of createApp with the middleware obtained with defineI18nMiddleware

at useTranslation (node_modules/@intlify/h3/dist/index.mjs:33:15)
at useTranslation (node_modules/@intlify/h3/dist/index.mjs:31:31)
at t (.nuxt/dev/index.mjs:5628:33)
at t (.nuxt/dev/index.mjs:5625:11)
at (.nuxt/dev/index.mjs:18522:68)
at processTicksAndRejections (native:7:39)
```

Who say what i will not need to translate something after onAfterResponse?
I have code that run after, and it should run after.

### Reproduction

https://github.com/intlify/h3/blob/19a13c62e86f91b8a37178844fe32ef57d04ba0e/src/index.ts#L142

### System Info

```Shell
latest linux, node, nuxt
```

### Used Package Manager

n/a

### Additional context

````
onAfterResponse(event) {
i18n.locale = orgLocale;
// delete event.context.i18n;
}
```

solve the problem

### Validations

- [x] Follow our [Code of Conduct](https://github.com/intlify/.github/blob/main/CODE_OF_CONDUCT.md)
- [x] Read the [Contributing Guide](https://github.com/intlify/h3/blob/main/CONTRIBUTING.md).
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
- [x] The provided reproduction is a [minimal reproducible](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.

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.