Skip to content

Commit 8af5982

Browse files
authored
example: back-end integration and i18n resources compilation (#891)
1 parent 24a67cc commit 8af5982

File tree

18 files changed

+724
-17
lines changed

18 files changed

+724
-17
lines changed

examples/backend/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
dist-ssr
5+
*.local

examples/backend/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Backend integration & i18n resource compilation
2+
3+
This is an example of back-end integration, and i18n resources compilation
4+
5+
- `vite.config.ts`: define the plugin that is simulated as the back-end
6+
- `db`: simulate the loading of resources from the database
7+
- `scripts/generate.ts`: compile the i18n resources
8+
- `src/locales.ts`: define the i18n resources load function for development and production

examples/backend/db/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "hello!"
3+
}

examples/backend/db/ja.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "こんにちは!"
3+
}

examples/backend/db/message.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import en from './en.json'
2+
3+
export type ResourceSchema = typeof en

examples/backend/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" href="/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite App</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>

examples/backend/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "backend",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "vite",
7+
"generate": "npx jiti ./scripts/generate.ts",
8+
"build": "vite build",
9+
"check": "vue-tsc --noEmit",
10+
"preview": "vite preview"
11+
},
12+
"dependencies": {
13+
"vue": "next",
14+
"vue-i18n": "workspace:*"
15+
},
16+
"devDependencies": {
17+
"@types/express": "^4.17.13",
18+
"@types/body-parser": "^1.19.2",
19+
"express": "^4.17.1",
20+
"body-parser": "^1.19.1",
21+
"jiti": "^1.12.9",
22+
"ohmyfetch": "^0.4.15",
23+
"@vitejs/plugin-vue": "^2.0.0",
24+
"@intlify/vite-plugin-vue-i18n": "next",
25+
"@intlify/bundle-utils": "latest",
26+
"typescript": "^4.5.4",
27+
"vite": "^2.7.2",
28+
"vue-tsc": "^0.29.8"
29+
}
30+
}
4.19 KB
Binary file not shown.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { promises as fs } from 'fs'
2+
import path from 'path'
3+
import { createServer } from 'vite'
4+
import { $fetch } from 'ohmyfetch'
5+
import { generateJSON } from '@intlify/bundle-utils'
6+
import { locales } from '../src/constants'
7+
8+
import type { Locale } from 'vue-i18n'
9+
import type { ResourceSchema } from '../db/message'
10+
11+
async function compile(locale: string, message: ResourceSchema) {
12+
const filename = path.join(__dirname, `../public/${locale}.js`)
13+
const ret = generateJSON(JSON.stringify(message), {
14+
type: 'plain',
15+
filename,
16+
env: 'production',
17+
onError(msg) {
18+
console.error('compile error', msg)
19+
},
20+
onWarn(msg) {
21+
console.warn('compile waring', msg)
22+
}
23+
})
24+
return { ...ret, filename }
25+
}
26+
27+
;(async (locales: readonly Locale[]) => {
28+
// start vite server
29+
const vite = await createServer({ root: process.cwd() })
30+
await vite.listen(3000)
31+
vite.printUrls()
32+
33+
// compile reousrces
34+
for (const locale of locales) {
35+
const resource = await $fetch<ResourceSchema>(
36+
`http://localhost:3000/api/resources/${locale}`
37+
)
38+
const { code, map, filename } = await compile(locale, resource)
39+
await fs.writeFile(filename, code)
40+
}
41+
42+
process.on('uncaughtException', err => {
43+
console.error(`uncaught exception: ${err}\n`)
44+
process.exit(1)
45+
})
46+
47+
process.on('unhandledRejection', (reason, p) => {
48+
console.error('unhandled rejection at:', p, 'reason:', reason)
49+
process.exit(1)
50+
})
51+
52+
process.exit(0)
53+
})(locales)

examples/backend/src/App.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script setup lang="ts">
2+
import { useI18n } from 'vue-i18n'
3+
4+
const { t } = useI18n()
5+
</script>
6+
7+
<template>
8+
<h1>{{ t('hello') }}</h1>
9+
</template>
10+
11+
<style>
12+
#app {
13+
font-family: Avenir, Helvetica, Arial, sans-serif;
14+
-webkit-font-smoothing: antialiased;
15+
-moz-osx-font-smoothing: grayscale;
16+
text-align: center;
17+
color: #2c3e50;
18+
margin-top: 60px;
19+
}
20+
</style>

0 commit comments

Comments
 (0)