Skip to content

Commit 185e535

Browse files
authored
Add Vue.js integration guide #1336 (#1512)
* Add Vue.js integration guide - Follows Laravel guide structure and format - Complete setup with vue-i18n - Language switching implementation - Dynamic content examples with interpolation * Clarify comments in Vue integration guide ✅ Rewrite or clarify each comment, ✅ Update the guide to be clear and concise
1 parent 3a4a7e5 commit 185e535

File tree

1 file changed

+340
-0
lines changed

1 file changed

+340
-0
lines changed

docs/vue-integration-guide.md

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
---
2+
title: "Vue.js"
3+
subtitle: "AI translation for Vue.js with Lingo.dev CLI"
4+
---
5+
6+
## What is Vue.js?
7+
8+
[Vue.js](https://vuejs.org/) is a progressive JavaScript framework for building user interfaces. It is designed to be incrementally adoptable and can easily scale between a library and a full-featured framework.
9+
10+
## What is Lingo.dev CLI?
11+
12+
Lingo.dev is an AI-powered translation platform. The Lingo.dev CLI reads source files, sends translatable content to large language models, and writes translated files back to your project.
13+
14+
## About this guide
15+
16+
This guide explains how to set up Lingo.dev CLI in a Vue.js application. You'll learn how to scaffold a project with Vue.js, configure a translation pipeline using vue-i18n, and implement language switching in your application.
17+
18+
## Step 1. Set up a Vue.js project
19+
20+
1. Install Vue CLI globally:
21+
22+
```bash
23+
npm install -g @vue/cli
24+
```
25+
26+
2. Create a new Vue.js project:
27+
28+
```bash
29+
vue create i18n-example
30+
```
31+
32+
When prompted for a preset, choose **"Manually select features"** and select:
33+
34+
- Babel
35+
- Router
36+
- Linter / Formatter
37+
38+
Then configure:
39+
40+
- **Vue version:** 3.x
41+
- **Router history mode:** Yes
42+
- **Linter:** ESLint with error prevention only (or your preference)
43+
44+
3. Navigate into the project directory:
45+
46+
```bash
47+
cd i18n-example
48+
```
49+
50+
4. Install vue-i18n for internationalization:
51+
52+
```bash
53+
npm install vue-i18n@9
54+
```
55+
56+
## Step 2. Create source content
57+
58+
1. Create a directory for storing localizable content:
59+
60+
```bash
61+
mkdir -p src/locales
62+
```
63+
64+
2. Create a file that contains some localizable content (e.g., `src/locales/en.json`):
65+
66+
```json
67+
{
68+
"welcome": "Welcome to Your Vue.js App",
69+
"description": "This text is translated by Lingo.dev",
70+
"greeting": "Hello, {name}!",
71+
"toggle": "Switch Language",
72+
"counter": "You clicked {count} times"
73+
}
74+
```
75+
76+
## Step 3. Configure the CLI
77+
78+
In the root of the project, create an `i18n.json` file:
79+
80+
```json
81+
{
82+
"$schema": "https://lingo.dev/schema/i18n.json",
83+
"version": "1.10",
84+
"locale": {
85+
"source": "en",
86+
"targets": ["es", "fr", "de", "ja"]
87+
},
88+
"buckets": {
89+
"json": {
90+
"include": ["src/locales/[locale].json"]
91+
}
92+
}
93+
}
94+
```
95+
96+
This file defines:
97+
98+
- the files that Lingo.dev CLI should translate
99+
- the languages to translate between
100+
101+
In this case, the configuration translates JSON files from English to Spanish, French, German, and Japanese.
102+
103+
It's important to note that:
104+
105+
- `[locale]` is a placeholder that's replaced at runtime. It ensures that content is read from one location (e.g., `src/locales/en.json`) and written to a different location (e.g., `src/locales/es.json`).
106+
- Lingo.dev CLI supports various file formats including JSON, MDX, and more.
107+
108+
To learn more, see [i18n.json configuration](/cli/fundamentals/i18n-json-config).
109+
110+
## Step 4. Translate the content
111+
112+
1. [Sign up for a Lingo.dev account](/app).
113+
114+
2. Log in to Lingo.dev via the CLI:
115+
116+
```bash
117+
npx lingo.dev@latest login
118+
```
119+
120+
3. Run the translation pipeline:
121+
122+
```bash
123+
npx lingo.dev@latest run
124+
```
125+
126+
The CLI will create translation files (e.g., `src/locales/es.json`, `src/locales/fr.json`, etc.) for storing the translated content and an `i18n.lock` file for keeping track of what has been translated (to prevent unnecessary retranslations).
127+
128+
## Step 5. Set up vue-i18n in your application
129+
130+
1. Create an i18n configuration file (`src/i18n.js`):
131+
132+
```javascript
133+
import { createI18n } from 'vue-i18n';
134+
135+
// It only imports 5 specific locales
136+
import en from './locales/en.json';
137+
import es from './locales/es.json';
138+
import fr from './locales/fr.json';
139+
import de from './locales/de.json';
140+
import ja from './locales/ja.json';
141+
142+
const messages = {
143+
en,
144+
es,
145+
fr,
146+
de,
147+
ja
148+
};
149+
150+
// Create i18n instance
151+
export default createI18n({
152+
legacy: false, //you must set this to `false` to use Composition API
153+
locale: 'en', // set locale
154+
fallbackLocale: 'en', // set fallback locale
155+
messages, // set locale messages
156+
});
157+
```
158+
159+
2. Update your main.js file to use i18n:
160+
161+
```javascript
162+
import { createApp } from 'vue';
163+
import App from './App.vue';
164+
import router from './router';
165+
import i18n from './i18n';
166+
167+
createApp(App)
168+
.use(router)
169+
.use(i18n)
170+
.mount('#app');
171+
```
172+
173+
## Step 6. Implement language switching in your Vue components
174+
175+
1. Create a language switcher component (`src/components/LanguageSwitcher.vue`):
176+
177+
```vue
178+
<template>
179+
<div class="language-switcher">
180+
<label for="language-select">Language:</label>
181+
<select
182+
id="language-select"
183+
v-model="currentLocale"
184+
@change="changeLocale"
185+
>
186+
<option value="en">English</option>
187+
<option value="es">Español</option>
188+
<option value="fr">Français</option>
189+
<option value="de">Deutsch</option>
190+
<option value="ja">日本語</option>
191+
</select>
192+
</div>
193+
</template>
194+
195+
<script>
196+
import { ref, watch } from 'vue';
197+
import { useI18n } from 'vue-i18n';
198+
199+
export default {
200+
name: 'LanguageSwitcher',
201+
setup() {
202+
const { locale } = useI18n();
203+
const currentLocale = ref(locale.value);
204+
205+
const changeLocale = () => {
206+
locale.value = currentLocale.value;
207+
};
208+
209+
watch(locale, (newLocale) => {
210+
currentLocale.value = newLocale;
211+
});
212+
213+
return {
214+
currentLocale,
215+
changeLocale
216+
};
217+
}
218+
};
219+
</script>
220+
221+
<style scoped>
222+
.language-switcher {
223+
margin: 1rem 0;
224+
display: flex;
225+
align-items: center;
226+
gap: 0.5rem;
227+
}
228+
229+
select {
230+
padding: 0.5rem;
231+
border: 1px solid #ddd;
232+
border-radius: 4px;
233+
cursor: pointer;
234+
}
235+
</style>
236+
```
237+
238+
2. Update your `App.vue` to use translations:
239+
240+
```vue
241+
<template>
242+
<div id="app">
243+
<header>
244+
<h1>{{ $t('welcome') }}</h1>
245+
<p>{{ $t('description') }}</p>
246+
<p>{{ $t('greeting', { name: 'Vue.js' }) }}</p>
247+
<LanguageSwitcher />
248+
<Counter />
249+
</header>
250+
<router-view />
251+
</div>
252+
</template>
253+
254+
<script>
255+
import LanguageSwitcher from './components/LanguageSwitcher.vue';
256+
import Counter from './components/Counter.vue';
257+
258+
export default {
259+
name: 'App',
260+
components: {
261+
LanguageSwitcher,
262+
Counter
263+
}
264+
};
265+
</script>
266+
267+
<style>
268+
#app {
269+
font-family: Avenir, Helvetica, Arial, sans-serif;
270+
text-align: center;
271+
color: #2c3e50;
272+
margin-top: 60px;
273+
}
274+
</style>
275+
```
276+
277+
3. Create a Counter component to demonstrate dynamic content (`src/components/Counter.vue`):
278+
279+
```vue
280+
<template>
281+
<div class="counter">
282+
<p>{{ $t('counter', { count }) }}</p>
283+
<button @click="increment">+</button>
284+
</div>
285+
</template>
286+
287+
<script>
288+
import { ref } from 'vue';
289+
290+
export default {
291+
name: 'Counter',
292+
setup() {
293+
const count = ref(0);
294+
295+
const increment = () => {
296+
count.value++;
297+
};
298+
299+
return {
300+
count,
301+
increment
302+
};
303+
}
304+
};
305+
</script>
306+
307+
<style scoped>
308+
.counter {
309+
margin: 1rem 0;
310+
}
311+
312+
button {
313+
padding: 0.5rem 1rem;
314+
margin-left: 0.5rem;
315+
border: none;
316+
border-radius: 4px;
317+
background-color: #42b983;
318+
color: white;
319+
cursor: pointer;
320+
}
321+
</style>
322+
```
323+
324+
## Step 7. Test the application
325+
326+
1. Start the development server:
327+
328+
```bash
329+
npm run serve
330+
```
331+
332+
2. Navigate to the following URL:
333+
334+
- http://localhost:8080
335+
336+
3. Verify that the language switching works:
337+
338+
- The application should display content in English by default
339+
- Selecting a different language from the dropdown should update all translated text
340+
- The counter should continue to function in all languages

0 commit comments

Comments
 (0)