Skip to content

Commit 69748e3

Browse files
authored
docs: diff with syntax highlighting in nuxt integration (#1490)
1 parent d381da0 commit 69748e3

File tree

1 file changed

+65
-62
lines changed

1 file changed

+65
-62
lines changed

docs/guide/integrations/nuxt3.md

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ Let's see if Vue I18n works with Nuxt 3.
107107

108108
We will edit `app.vue` of the setup Nuxt 3 application as follows:
109109

110-
```diff
111-
<template>
112-
<div>
113-
- <NuxtWelcome />
114-
+ <h1>{{ $t('hello', { name: 'vue-i18n' }) }}</h1>
115-
</div>
116-
</template>
110+
```vue
111+
<template>
112+
<div>
113+
<NuxtWelcome /> // [!code --]
114+
<h1>{{ $t('hello', { name: 'vue-i18n' }) }}</h1> // [!code ++]
115+
</div>
116+
</template>
117117
```
118118

119119
We have edited and saved, run the following command to run the Nuxt 3 application at local:
@@ -139,20 +139,20 @@ In the following sections, we will work on Nuxt 3 applications that support Engl
139139

140140
We will add language switching feature to `app.vue` as follows:
141141

142-
```diff
143-
<template>
144-
<div>
145-
<h1>{{ $t('hello', { name: 'vue-i18n' }) }}</h1>
146-
+ <form>
147-
+ <label for="locale-select">{{ $t('language') }}: </label>
148-
+ <select id="locale-select" v-model="$i18n.locale">
149-
+ <option value="en">en</option>
150-
+ <option value="fr">fr</option>
151-
+ <option value="ja">ja</option>
152-
+ </select>
153-
+ </form>
154-
</div>
155-
</template>
142+
```vue
143+
<template>
144+
<div>
145+
<h1>{{ $t('hello', { name: 'vue-i18n' }) }}</h1>
146+
<form> // [!code ++]
147+
<label for="locale-select">{{ $t('language') }}: </label> // [!code ++]
148+
<select id="locale-select" v-model="$i18n.locale"> // [!code ++]
149+
<option value="en">en</option> // [!code ++]
150+
<option value="fr">fr</option> // [!code ++]
151+
<option value="ja">ja</option> // [!code ++]
152+
</select> // [!code ++]
153+
</form> // [!code ++]
154+
</div>
155+
</template>
156156
```
157157

158158
Language switching is implemented using the `select` element on `form`.
@@ -183,6 +183,7 @@ touch locales/ja.json # for japanese
183183
And more then, We will have saved each created locale resource file for each language as follows:
184184

185185
For english at `locales/en.json`:
186+
186187
```json
187188
{
188189
"hello": "Hello, {name}!",
@@ -191,6 +192,7 @@ For english at `locales/en.json`:
191192
```
192193

193194
For french at `locales/fr.json`:
195+
194196
```json
195197
{
196198
"hello": "Bonjour, {name}!",
@@ -199,6 +201,7 @@ For french at `locales/fr.json`:
199201
```
200202

201203
For japanese at `locales/ja.json`:
204+
202205
```json
203206
{
204207
"hello": "こんにちは、{name}!",
@@ -212,29 +215,29 @@ We will import locale resources that is defined in the `locales` directory for u
212215

213216
And then, change `plugins/i18n.ts` as follows:
214217

215-
```diff
216-
import { createI18n } from 'vue-i18n'
217-
+import en from '../locales/en.json'
218-
+import fr from '../locales/fr.json'
219-
+import ja from '../locales/ja.json'
220-
221-
export default defineNuxtPlugin(({ vueApp }) => {
222-
const i18n = createI18n({
223-
legacy: false,
224-
globalInjection: true,
225-
locale: 'en',
226-
messages: {
227-
- en: {
228-
- hello: "Hello, {name}!"
229-
- }
230-
+ en,
231-
+ fr,
232-
+ ja
233-
}
234-
})
235-
236-
vueApp.use(i18n)
237-
})
218+
```js
219+
import { createI18n } from 'vue-i18n'
220+
import en from '../locales/en.json' // [!code ++]
221+
import fr from '../locales/fr.json' // [!code ++]
222+
import ja from '../locales/ja.json' // [!code ++]
223+
224+
export default defineNuxtPlugin(({ vueApp }) => {
225+
const i18n = createI18n({
226+
legacy: false,
227+
globalInjection: true,
228+
locale: 'en',
229+
messages: {
230+
en: { // [!code --]
231+
hello: "Hello, {name}!" // [!code --]
232+
} // [!code --]
233+
en, // [!code ++]
234+
fr, // [!code ++]
235+
ja // [!code ++]
236+
}
237+
})
238+
239+
vueApp.use(i18n)
240+
})
238241
```
239242

240243
It’s set locale resources for each imported language to `messages` option, so you can manage locale resources with separating from code in the `locales` directory. It also facilitates integration with the localization service.
@@ -265,24 +268,24 @@ npm install --save-dev @intlify/unplugin-vue-i18n
265268

266269
Configure `nuxt.config.ts` like the below:
267270

268-
```diff
269-
import { defineNuxtConfig } from 'nuxt'
270-
+import { resolve, dirname } from 'node:path'
271-
+import { fileURLToPath } from 'url'
272-
+import VueI18nVitePlugin from '@intlify/unplugin-vue-i18n/vite'
273-
274-
// https://nuxt.com/docs/api/configuration/nuxt-config
275-
export default defineNuxtConfig({
276-
+ vite: {
277-
+ plugins: [
278-
+ VueI18nVitePlugin({
279-
+ include: [
280-
+ resolve(dirname(fileURLToPath(import.meta.url)), './locales/*.json')
281-
+ ]
282-
+ })
283-
+ ]
284-
+ }
285-
})
271+
```js
272+
import { defineNuxtConfig } from 'nuxt'
273+
import { resolve, dirname } from 'node:path' // [!code ++]
274+
import { fileURLToPath } from 'url' // [!code ++]
275+
import VueI18nVitePlugin from '@intlify/unplugin-vue-i18n/vite' // [!code ++]
276+
277+
// https://nuxt.com/docs/api/configuration/nuxt-config
278+
export default defineNuxtConfig({
279+
vite: { // [!code ++]
280+
plugins: [ // [!code ++]
281+
VueI18nVitePlugin({ // [!code ++]
282+
include: [ // [!code ++]
283+
resolve(dirname(fileURLToPath(import.meta.url)), './locales/*.json') // [!code ++]
284+
] // [!code ++]
285+
}) // [!code ++]
286+
] // [!code ++]
287+
} // [!code ++]
288+
})
286289
```
287290
288291
The bundler for Nuxt 3 is vite by default. So we will use the `vite` option here for optimization.

0 commit comments

Comments
 (0)