Skip to content

Commit

Permalink
feat: add vue-i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 10, 2020
1 parent 7e7c796 commit b93c6c8
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"octref.vetur",
"antfu.i18n-ally",
"dbaeumer.vscode-eslint",
"bradlc.vscode-tailwindcss"
]
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"i18n-ally.localesPaths": "locales",
"i18n-ally.keystyle": "nested",
"i18n-ally.sortKeys": true,

"vetur.experimental.templateInterpolationService": true
}
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<br>

<p align='center'>
<a href="https://vitesse.netilfy.app">Live Example</a>
<a href="https://vitesse.netilfy.app">Live Demo</a>
</p>

<br>
Expand All @@ -32,6 +32,7 @@

### Utils

- [vue-i18n](https://github.com/intlify/vue-i18n-next)
- [VueUse](https://github.com/antfu/vueuse)

### Misc
Expand All @@ -44,16 +45,22 @@
- [pnpm](https://pnpm.js.org/)
- [Netlify](https://www.netlify.com/)
- [ESLint](https://eslint.org/) with [@antfu/eslint-config-vue](https://github.com/antfu/eslint-config)
- [VS Code Extensions](./.vscode/extensions.json)

## Use It!
## Try it now!

[Create a repo from this template on Github](https://github.com/antfu/vitesse/generate).

Or if you prefers do to manually with cleaner git history

```bash
npx degit antfu/vitesse my-vitesse-app \
cd my-vitesse-app \
git init \
npx degit antfu/vitesse my-vitesse-app
cd my-vitesse-app
pnpm i # If you don't have pnpm installed, run: npm install -g pnpm
```

## Why

I have created several Vite apps recently. Setting the configs up is kinda the bottleneck for me to make the idea simply comes true in very a short time. So I made this starter template for myself to create apps more easily, along with some good practices that I have learned during making those apps. It's strongly opinionated, but feel free to tweak it or even maintains your own forks.
I have created several Vite apps recently. Setting the configs up is kinda the bottleneck for me to make the idea simply comes true in a very short time.

So I made this starter template for myself to create apps more easily, along with some good practices that I have learned from making those apps. It's strongly opinionated, but feel free to tweak it or even maintains your own forks.
10 changes: 10 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"intro": {
"whats-your-name": "What's your name?",
"hi": "Hi, {name}!"
},
"button": {
"go": "GO",
"back": "Back"
}
}
10 changes: 10 additions & 0 deletions locales/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"button": {
"back": "Arrière",
"go": "ALLER"
},
"intro": {
"hi": "Salut, {name}!",
"whats-your-name": "Quel est ton nom?"
}
}
10 changes: 10 additions & 0 deletions locales/zh-CN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"button": {
"back": "返回",
"go": "确定"
},
"intro": {
"hi": "你好,{name}!",
"whats-your-name": "输入你的名字"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@vueuse/core": "^4.0.0-beta.4",
"variantwind": "^0.3.4",
"vue": "^3.0.0-rc.1",
"vue-i18n": "^9.0.0-alpha.13",
"vue-router": "^4.0.0-beta.6"
},
"devDependencies": {
Expand Down
14 changes: 13 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions src/components/Footer.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

<template>
<div
class="text-xl mt-4"
>
<div class="text-xl mt-6">
<div
class="inline-block mx-2 cursor-pointer select-none"
class="icon-btn mx-2"
@click="isDark = !isDark"
>
<Icon
Expand All @@ -14,7 +11,7 @@
</div>

<a
class="mx-2"
class="icon-btn mx-2"
href="https://github.com/antfu/vitesse"
target="_blank"
>
Expand All @@ -23,9 +20,28 @@
icon="carbon:code"
/>
</a>

<div
class="icon-btn mx-2"
@click="toggleLocales"
>
<Icon
class="inline-block"
icon="carbon:globe"
/>
</div>
</div>
</template>

<script setup lang='ts'>
import { useI18n } from 'vue-i18n'
import { locales } from '../messages'
export { isDark } from '../utils/dark'
const i18n = useI18n()
export const toggleLocales = () => {
// change to some real logic
i18n.locale.value = locales[(locales.indexOf(i18n.locale.value) + 1) % locales.length]
}
</script>
8 changes: 8 additions & 0 deletions src/main.postcss
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ html, body, #app {

.btn[disabled] {
@apply cursor-default bg-gray-600 opacity-50;
}

.icon-btn {
@apply opacity-75 transition duration-200 ease-in-out cursor-pointer inline-block select-none;
}

.icon-btn:hover {
@apply opacity-100;
}
11 changes: 10 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import './main.postcss'
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
// @ts-ignore
import { createI18n } from 'vue-i18n'

// @ts-ignore: this is generated from voie, which TypeScript is not able to infer types correctly
import routes from '/@voie/pages'
import { registerComponents } from './components'
import App from './App.vue'
import { messages } from './messages'

const app = createApp(App)
const router = createRouter({
history: createWebHistory(),
routes,
})

const i18n = createI18n({
locale: 'en',
messages,
})

app.use(i18n)
app.use(router)
app.use(registerComponents)

Expand Down
11 changes: 11 additions & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import en from '../locales/en.json'
import zhCN from '../locales/zh-CN.json'
import fr from '../locales/fr.json'

export const messages = {
en,
'zh-CN': zhCN,
fr,
}

export const locales = Object.keys(messages)
8 changes: 6 additions & 2 deletions src/pages/hi/[name].vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
<Icon class="iconify inline-block" icon="carbon:pedestrian" />
</p>
<p>
Hi, {{ name }}!
{{ t('intro.hi', {name}) }}
</p>

<div>
<button
class="btn m-3 text-sm mt-8"
@click="back"
>
Back
{{ t('button.back') }}
</button>
</div>
</div>
</template>

<script setup='props' lang='ts'>
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
declare const props: {
name: string
}
const router = useRouter()
export const back = () => router.push('/')
const { t } = useI18n()
export { t }
</script>
8 changes: 6 additions & 2 deletions src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<input
v-model="name"
placeholder="What's your name?"
:placeholder="t('intro.whats-your-name')"
class="px-4 py-2 border border-gray-200 rounded text-center outline-none active:outline-none bg-transparent dark:border-gray-700"
@keydown.enter="go"
>
Expand All @@ -23,7 +23,7 @@
:disabled="!name"
@click="go"
>
GO
{{ t('button.go') }}
</button>
</div>
</div>
Expand All @@ -32,6 +32,7 @@
<script setup='props' lang='ts'>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
export const name = ref('')
Expand All @@ -40,4 +41,7 @@ export const go = () => {
if (name.value)
router.push(`/hi/${name.value}`)
}
const { t } = useI18n()
export { t }
</script>

0 comments on commit b93c6c8

Please sign in to comment.