Skip to content

Commit

Permalink
feat: add playground
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Oct 17, 2023
1 parent cd231e0 commit 1fd9053
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"email": "kawakazu80@gmail.com"
},
"license": "MIT",
"funding": "https://github.com/sponsors/kazupon",
"bugs": {
"url": "https://github.com/intlify/h3/issues"
},
Expand All @@ -32,6 +33,7 @@
"engines": {
"node": ">= 18"
},
"sideEffects": false,
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
Expand All @@ -47,12 +49,12 @@
},
"scripts": {
"prepare": "git config --local core.hooksPath .githooks",
"preinstall": "node scripts/preinstall.js",
"changelog": "gh-changelogen --repo=intlify/h3",
"release": "bumpp --commit \"release: v%s\" --push --tag",
"lint": "deno lint",
"format": "deno fmt",
"build": "unbuild",
"play": "bun run ./playground/index.ts",
"test": "vitest run",
"test:coverage": "npm test -- --reporter verbose --coverage"
},
Expand Down
16 changes: 16 additions & 0 deletions playground/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `@intlify/h3` playground

This playground is translation with `accept-language` header.

## Usage

```sh
npm run dev
```

and then, you try to access to `http://localhost:3000` with `accept-language`
header with another shell:

```sh
curl -H 'Accept-Language: ja,en-US;q=0.7,en;q=0.3' http://localhost:3000
```
33 changes: 33 additions & 0 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createServer } from 'node:http'
import { createApp, createRouter, eventHandler, toNodeListener } from 'h3'
import {
defineI18nMiddleware,
detectLocaleFromAcceptLanguageHeader,
useTranslation,
} from '../src/index'

const middleware = defineI18nMiddleware({
locale: detectLocaleFromAcceptLanguageHeader,
messages: {
en: {
hello: 'hello, {name}',
},
ja: {
hello: 'こんにちは, {name}',
},
},
})

const app = createApp({ ...middleware })

const router = createRouter()
router.get(
'/',
eventHandler((event) => {
const t = useTranslation(event)
return t('hello', { name: 'h3' })
}),
)

app.use(router)
createServer(toNodeListener(app)).listen(3000)

0 comments on commit 1fd9053

Please sign in to comment.