|
| 1 | + |
| 2 | +--- |
| 3 | +title: Svelte AI Translation with Lingo.dev |
| 4 | +--- |
| 5 | + |
| 6 | +## What is Svelte? |
| 7 | + |
| 8 | +[Svelte](https://svelte.dev/) is a modern framework for building web applications. |
| 9 | +Unlike other frameworks, Svelte compiles your code to highly optimized JavaScript at build time, so your app runs faster in the browser. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## What is Lingo.dev? |
| 14 | + |
| 15 | +[Lingo.dev](https://lingo.dev/) is an AI-powered translation platform. |
| 16 | +It allows you to automatically translate content in your project using a **CLI** for static translations. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## About this guide |
| 21 | + |
| 22 | +This guide explains how to set up **Lingo.dev CLI** in a Svelte project. |
| 23 | +You will learn how to: |
| 24 | + |
| 25 | +- Install Svelte or SvelteKit |
| 26 | +- Create static content |
| 27 | +- Configure and run Lingo.dev CLI |
| 28 | +- Use translated content in your Svelte components |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Step 1. Install Svelte (If you have an existing Svelte project, please skip to **Step 2**) |
| 33 | + |
| 34 | +### Using SvelteKit |
| 35 | + |
| 36 | +1. Open a terminal of your choice. |
| 37 | +2. Create a new project: |
| 38 | + |
| 39 | +```bash |
| 40 | + npx sv create <Name-of-the-app> |
| 41 | +``` |
| 42 | +- <img width="800" height="500" alt="C__Windows_system32_cmd exe 03-11-2025 13_13_47" src="https://github.com/user-attachments/assets/ed042e99-1299-4c3a-b847-90af28921dd8" /> |
| 43 | + |
| 44 | +3. Follow the prompts to choose options. |
| 45 | + |
| 46 | +- <img width="800" height="500" alt="C__Windows_system32_cmd exe 03-11-2025 13_14_22" src="https://github.com/user-attachments/assets/ce6cbe26-7414-4d06-bbe8-37e4c2cd693d" /> |
| 47 | + |
| 48 | +4. Navigate into the project directory: |
| 49 | + |
| 50 | +```bash |
| 51 | +cd <Name-of-the-app> |
| 52 | +``` |
| 53 | + |
| 54 | +5. Start the development server: |
| 55 | + |
| 56 | +```bash |
| 57 | +npm run dev -- --open |
| 58 | +``` |
| 59 | + |
| 60 | +6. This should open localhost with the built-in Svelte page. |
| 61 | + |
| 62 | +### Using Vite + Svelte (Svelte compiler only) |
| 63 | + |
| 64 | +1. Open a terminal of your choice. |
| 65 | +2. Create a new project: |
| 66 | + |
| 67 | +```bash |
| 68 | +npm create vite@latest <Name-of-the-app> -- --template svelte |
| 69 | +``` |
| 70 | + |
| 71 | +3. Navigate into the project directory: |
| 72 | + |
| 73 | +```bash |
| 74 | +cd <Name-of-the-app> |
| 75 | +``` |
| 76 | + |
| 77 | +4. Install dependencies: |
| 78 | + |
| 79 | +```bash |
| 80 | +npm install |
| 81 | +``` |
| 82 | + |
| 83 | +5. Start the development server: |
| 84 | + |
| 85 | +```bash |
| 86 | +npm run dev -- --open |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Step 2. Create a Lingo.dev account |
| 92 | + |
| 93 | +1. Go to [lingo.dev](https://lingo.dev/). |
| 94 | +2. Click **Get Started**. |
| 95 | +3. Sign in using your preferred authorization method. |
| 96 | +4. On the dashboard, click **Get API Key**. |
| 97 | +5. Copy the key using the **Copy** button. |
| 98 | + |
| 99 | +> The API key starts with `api_` followed by a 24-character alphanumeric string. |
| 100 | +
|
| 101 | +--- |
| 102 | + |
| 103 | +## Step 3. Install and Configure Lingo.dev CLI |
| 104 | + |
| 105 | +### 1. Initialize Lingo.dev in the project |
| 106 | + |
| 107 | +```bash |
| 108 | +npx lingo.dev@latest init |
| 109 | +``` |
| 110 | + |
| 111 | +### 2. Log in to Lingo.dev |
| 112 | + |
| 113 | +```bash |
| 114 | +npx lingo.dev@latest login |
| 115 | +``` |
| 116 | + |
| 117 | +3. ⚠ On Windows, `npx` may inject a shell script that cannot run without WSL or Git Bash. |
| 118 | + |
| 119 | + In that case, install globally: |
| 120 | + |
| 121 | + - <img width="800" height="500" alt="C__Windows_system32_cmd exe 03-11-2025 13_48_58" src="https://github.com/user-attachments/assets/12ae0df7-a942-4e86-8cc6-1d88290ab947" /> |
| 122 | + |
| 123 | + ```bash |
| 124 | + npm i -g lingo.dev |
| 125 | + lingo.dev login |
| 126 | + |
| 127 | +### 3. Create a directory for localizable content |
| 128 | + |
| 129 | +```bash |
| 130 | +mkdir -p src/lib/i18n |
| 131 | +``` |
| 132 | + |
| 133 | +### 4. Create an English content file |
| 134 | + |
| 135 | +```bash |
| 136 | +touch src/lib/i18n/en.json |
| 137 | +``` |
| 138 | + |
| 139 | +### 5. Populate it with your content, for example: |
| 140 | + |
| 141 | +```json |
| 142 | +{ |
| 143 | + "home": { |
| 144 | + "title": "Welcome", |
| 145 | + "subtitle": "This text is translated by Lingo.dev" |
| 146 | + }, |
| 147 | + "cta": "Get started" |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### 6. Create a root configuration file for the CLI |
| 152 | + |
| 153 | +```bash |
| 154 | +touch i18n.json |
| 155 | +``` |
| 156 | + |
| 157 | +Add the following content: |
| 158 | + |
| 159 | +```json |
| 160 | +{ |
| 161 | + "$schema": "https://lingo.dev/schema/i18n.json", |
| 162 | + "version": "1.10", |
| 163 | + "locale": { |
| 164 | + "source": "en", |
| 165 | + "targets": ["es"] |
| 166 | + }, |
| 167 | + "buckets": { |
| 168 | + "json": { |
| 169 | + "include": ["src/lib/i18n/[locale].json"] |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +### 7. Run the CLI to generate target language content |
| 176 | + |
| 177 | +```bash |
| 178 | +npx lingo.dev@latest run |
| 179 | +# or if globally installed |
| 180 | +lingo.dev run |
| 181 | +``` |
| 182 | +- <img width="800" height="500" alt="MINGW64__c_Users_SOHAM_Desktop_web development_my-lingo-app 03-11-2025 13_57_40" src="https://github.com/user-attachments/assets/85654e36-37ed-4ed0-9770-dbeaddd7905a" /> |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +## Step 4. Create an i18n Consumer |
| 187 | + |
| 188 | +Add the following Svelte store setup to manage translations: |
| 189 | + |
| 190 | +```ts |
| 191 | +// src/lib/i18n.ts |
| 192 | +import { writable } from "svelte/store"; |
| 193 | +import en from "$lib/i18n/en.json"; |
| 194 | +import es from "$lib/i18n/es.json"; |
| 195 | +
|
| 196 | +const translations = { en, es }; |
| 197 | +
|
| 198 | +type Locale = keyof typeof translations; |
| 199 | +
|
| 200 | +// Narrow the browser language to a supported locale or fallback to 'en' |
| 201 | +const detectedLang = (navigator.language.split("-")[0] || "en") as string; |
| 202 | +const browserLang: Locale = (Object.keys(translations) as Locale[]).includes( |
| 203 | + detectedLang as Locale |
| 204 | +) |
| 205 | + ? (detectedLang as Locale) |
| 206 | + : "en"; |
| 207 | +
|
| 208 | +// Svelte stores |
| 209 | +export const locale = writable<Locale>(browserLang); |
| 210 | +export const t = writable(translations[browserLang]); |
| 211 | +
|
| 212 | +// Function to change the locale dynamically |
| 213 | +export function setLocale(newLocale: Locale) { |
| 214 | + if (translations[newLocale]) { |
| 215 | + locale.set(newLocale); |
| 216 | + t.set(translations[newLocale]); |
| 217 | + } |
| 218 | +} |
| 219 | +``` |
| 220 | +
|
| 221 | +--- |
| 222 | +
|
| 223 | +## Step 5. Use Translations in a Svelte Component |
| 224 | +
|
| 225 | +```ts |
| 226 | +<script lang="ts"> |
| 227 | + import { t, locale, setLocale } from "$lib/i18n"; |
| 228 | + import { onMount } from "svelte"; |
| 229 | +
|
| 230 | + let translation: { |
| 231 | + home: { |
| 232 | + title: string, |
| 233 | + subtitle: string |
| 234 | + }, |
| 235 | + cta: string |
| 236 | + } |
| 237 | +
|
| 238 | + // Subscribe to translation store |
| 239 | + const unsubscribe = t.subscribe(value => { |
| 240 | + translation = value; |
| 241 | + }); |
| 242 | +
|
| 243 | + // Cleanup on destroy |
| 244 | + onDestroy(() => { |
| 245 | + unsubscribe(); |
| 246 | + }); |
| 247 | +
|
| 248 | + // Toggle language |
| 249 | + function toggleLocale() { |
| 250 | + const next = $locale === "en" ? "es" : "en"; |
| 251 | + setLocale(next); |
| 252 | + } |
| 253 | +</script> |
| 254 | +
|
| 255 | +<h2>{translation.home.title}</h2> |
| 256 | +<p>{translation.home.subtitle}</p> |
| 257 | +
|
| 258 | +<button>{translation.cta}</button> |
| 259 | +
|
| 260 | +<button on:click={toggleLocale}> |
| 261 | + { $locale === "en" ? "Switch to Español" : "Cambiar a English" } |
| 262 | +</button> |
| 263 | +``` |
| 264 | +
|
| 265 | +--- |
| 266 | +
|
| 267 | +## Step 6. Run and Test |
| 268 | +
|
| 269 | +Start your dev server again: |
| 270 | +
|
| 271 | +```bash |
| 272 | +npm run dev |
| 273 | +``` |
| 274 | +
|
| 275 | +Visit your app in the browser and click the toggle button — |
| 276 | +you should see your text switch between **English** and **Spanish**, powered by **Lingo.dev CLI** translations. |
| 277 | +
|
| 278 | +--- |
| 279 | +
|
| 280 | +## Docs |
| 281 | +
|
| 282 | +* [Lingo.dev CLI Documentation](https://lingo.dev/cli) |
| 283 | +* [Svelte Documentation](https://svelte.dev/docs) |
0 commit comments