Skip to content

Commit e0a449b

Browse files
committed
feat(vue): add i18n starter
This example overrides $app/index.js and provides new route creation as well as beforeEach handlers to enable locale prefix and domain routing.
1 parent 742e65e commit e0a449b

21 files changed

+801
-0
lines changed

pnpm-lock.yaml

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

starters/vue-i18n/.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

starters/vue-i18n/.eslintrc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
parser: '@babel/eslint-parser',
3+
parserOptions: {
4+
requireConfigFile: false,
5+
ecmaVersion: 2021,
6+
sourceType: 'module',
7+
babelOptions: {
8+
presets: ['@babel/preset-react'],
9+
},
10+
ecmaFeatures: {
11+
jsx: true,
12+
},
13+
},
14+
extends: [
15+
'plugin:react/recommended',
16+
'standard',
17+
],
18+
plugins: [
19+
'react',
20+
],
21+
rules: {
22+
'comma-dangle': ['error', 'always-multiline'],
23+
'react/prop-types': 'off',
24+
'import/no-absolute-path': 'off',
25+
},
26+
settings: {
27+
react: {
28+
version: '18.0',
29+
},
30+
},
31+
}

starters/vue-i18n/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<br>
2+
3+
The official **[@fastify/vue](https://github.com/fastify/fastify-vite/tree/dev/packages/fastify-vue)** i18n starter template.
Lines changed: 31 additions & 0 deletions
Loading

starters/vue-i18n/client/base.css

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
@import 'tailwindcss';
2+
3+
:root {
4+
--color-base: #f1f1f1;
5+
--color-highlight: #ff80ff;
6+
}
7+
html {
8+
background: #222;
9+
}
10+
#root {
11+
width: 800px;
12+
margin: 0 auto;
13+
padding: 2em;
14+
box-shadow: 5px 5px 30px rgba(0,0,0,0.4);
15+
border-radius: 10px;
16+
background-color: rgba(255, 255, 255, 0.1);
17+
font-family: Avenir, Helvetica, Arial, sans-serif;
18+
-webkit-font-smoothing: antialiased;
19+
-moz-osx-font-smoothing: grayscale;
20+
color: var(--color-base);
21+
margin-top: 60px;
22+
& a {
23+
color: var(--color-highlight);
24+
text-decoration: none;
25+
font-weight: bold;
26+
border-bottom: 1px solid var(--color-highlight);
27+
&:hover {
28+
color: #ffde00;
29+
}
30+
&:active {
31+
color: #eecf00
32+
}
33+
}
34+
& p {
35+
font-size: 1.2em;
36+
}
37+
& ul {
38+
& li {
39+
&:not(:last-child) {
40+
margin-bottom: 0.5em;
41+
}
42+
break-inside: avoid;
43+
font-size: 1em;
44+
}
45+
}
46+
& code {
47+
color: #ffde00;
48+
font-weight: bold;
49+
font-family: 'Consolas', 'Andale Mono', monospace;
50+
font-size: 0.9em;
51+
}
52+
& img {
53+
width: 14em;
54+
}
55+
& button {
56+
margin: 0 0.5em;
57+
}
58+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useRoute, useRouter } from 'vue-router';
2+
3+
export function useI18nUtils() {
4+
const router = useRouter();
5+
const routes = router.getRoutes();
6+
const route = useRoute();
7+
8+
const localePath = (path) => {
9+
if ('name' in path) {
10+
const nameWithLocalePrefix = `${route.meta.locale}__${path.name}`;
11+
for (const route of routes) {
12+
if (route.name === nameWithLocalePrefix) {
13+
path.name = nameWithLocalePrefix;
14+
break;
15+
}
16+
}
17+
}
18+
19+
return path;
20+
};
21+
22+
return {
23+
localePath,
24+
};
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// The default export function runs exactly once on
2+
// the server and once on the client during the
3+
// first render, that is, it's not executed again
4+
// in subsequent client-side navigation.
5+
export default async (ctx) => {
6+
// Set default params here for fetch/axios or similar XHR library
7+
ctx.state.locale = ctx.meta.locale
8+
}
9+
10+
// State initializer, must be a function called state
11+
// as this is a special context.js export and has
12+
// special processing, e.g., serialization and hydration
13+
export function state() {
14+
return {
15+
locale: 'sv',
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
locales: ['en', 'sv', 'da'], // The first locale is the default
3+
localePrefix: true,
4+
localeDomains: {},
5+
}

starters/vue-i18n/client/i18n.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createI18n } from 'vue-i18n'
2+
3+
const i18nConfig = createI18n({
4+
locale: 'en',
5+
legacy: false,
6+
fallbackLocale: 'en',
7+
messages: {
8+
sv: {
9+
message: {
10+
welcome: "Välkommen till {'@'}fastify/vue!",
11+
},
12+
},
13+
en: {
14+
message: {
15+
welcome: "Welcome to {'@'}fastify/vue!",
16+
},
17+
},
18+
da: {
19+
message: {
20+
welcome: "Velkommen til {'@'}fastify/vue!",
21+
},
22+
},
23+
},
24+
})
25+
26+
export const i18n = i18nConfig

0 commit comments

Comments
 (0)