|
| 1 | +# Getting Started Internationalizing (i18n) with Intlayer and Vite and Svelte |
| 2 | + |
| 3 | +<iframe title="Vite + Svelte: Build a Multilingual App from Scratch using Intlayer" class="m-auto aspect-[16/9] w-full overflow-hidden rounded-lg border-0" allow="autoplay; gyroscope;" loading="lazy" width="1080" height="auto" src="https://www.youtube.com/embed/dS9L7uJeak4?autoplay=0&origin=http://intlayer.org&controls=0&rel=1"/> |
| 4 | +
|
| 5 | +See [Application Template](https://github.com/aymericzip/intlayer-vite-react-template) on GitHub. |
| 6 | +
|
| 7 | +## What is Intlayer? |
| 8 | +
|
| 9 | +**Intlayer** is an innovative, open-source internationalization (i18n) library designed to simplify multilingual support in modern web applications. |
| 10 | +
|
| 11 | +With Intlayer, you can: |
| 12 | +
|
| 13 | +- **Easily manage translations** using declarative dictionaries at the component level. |
| 14 | +- **Dynamically localize metadata**, routes, and content. |
| 15 | +- **Ensure TypeScript support** with autogenerated types, improving autocompletion and error detection. |
| 16 | +- **Benefit from advanced features**, like dynamic locale detection and switching. |
| 17 | +
|
| 18 | +--- |
| 19 | +
|
| 20 | +## Step-by-Step Guide to Set Up Intlayer in a Vite and Svelte Application |
| 21 | +
|
| 22 | +### Step 1: Install Dependencies |
| 23 | +
|
| 24 | +Install the necessary packages using npm: |
| 25 | +
|
| 26 | +```bash packageManager="npm" |
| 27 | +npm install intlayer svelte-intlayer vite-intlayer |
| 28 | +``` |
| 29 | +
|
| 30 | +```bash packageManager="pnpm" |
| 31 | +pnpm add intlayer svelte-intlayer vite-intlayer |
| 32 | +``` |
| 33 | +
|
| 34 | +```bash packageManager="yarn" |
| 35 | +yarn add intlayer svelte-intlayer vite-intlayer |
| 36 | +``` |
| 37 | +
|
| 38 | +- **intlayer** |
| 39 | +
|
| 40 | + The core package that provides internationalization tools for configuration management, translation, [content declaration](https://github.com/aymericzip/intlayer/blob/main/docs/en/dictionary/get_started.md), transpilation, and [CLI commands](https://github.com/aymericzip/intlayer/blob/main/docs/en/intlayer_cli.md). |
| 41 | +
|
| 42 | +- **svelte-intlayer** |
| 43 | + The package that integrates Intlayer with Svelte application. It provides context providers and hooks for Svelte internationalization. |
| 44 | +
|
| 45 | +- **vite-intlayer** |
| 46 | + Includes the Vite plugin for integrating Intlayer with the [Vite bundler](https://vite.dev/guide/why.html#why-bundle-for-production), as well as middleware for detecting the user's preferred locale, managing cookies, and handling URL redirection. |
| 47 | +
|
| 48 | +### Step 2: Configuration of your project |
| 49 | +
|
| 50 | +Create a config file to configure the languages of your application: |
| 51 | +
|
| 52 | +```typescript fileName="intlayer.config.ts" codeFormat="typescript" |
| 53 | +import { Locales, type IntlayerConfig } from "intlayer"; |
| 54 | +
|
| 55 | +const config: IntlayerConfig = { |
| 56 | + internationalization: { |
| 57 | + locales: [ |
| 58 | + Locales.ENGLISH, |
| 59 | + Locales.FRENCH, |
| 60 | + Locales.SPANISH, |
| 61 | + // Your other locales |
| 62 | + ], |
| 63 | + defaultLocale: Locales.ENGLISH, |
| 64 | + }, |
| 65 | +}; |
| 66 | +
|
| 67 | +export default config; |
| 68 | +``` |
| 69 | +
|
| 70 | +```javascript fileName="intlayer.config.mjs" codeFormat="esm" |
| 71 | +import { Locales } from "intlayer"; |
| 72 | +
|
| 73 | +/** @type {import('intlayer').IntlayerConfig} */ |
| 74 | +const config = { |
| 75 | + internationalization: { |
| 76 | + locales: [ |
| 77 | + Locales.ENGLISH, |
| 78 | + Locales.FRENCH, |
| 79 | + Locales.SPANISH, |
| 80 | + // Your other locales |
| 81 | + ], |
| 82 | + defaultLocale: Locales.ENGLISH, |
| 83 | + }, |
| 84 | +}; |
| 85 | +
|
| 86 | +export default config; |
| 87 | +``` |
| 88 | +
|
| 89 | +```javascript fileName="intlayer.config.cjs" codeFormat="commonjs" |
| 90 | +const { Locales } = require("intlayer"); |
| 91 | +
|
| 92 | +/** @type {import('intlayer').IntlayerConfig} */ |
| 93 | +const config = { |
| 94 | + internationalization: { |
| 95 | + locales: [ |
| 96 | + Locales.ENGLISH, |
| 97 | + Locales.FRENCH, |
| 98 | + Locales.SPANISH, |
| 99 | + // Your other locales |
| 100 | + ], |
| 101 | + defaultLocale: Locales.ENGLISH, |
| 102 | + }, |
| 103 | +}; |
| 104 | +
|
| 105 | +module.exports = config; |
| 106 | +``` |
| 107 | +
|
| 108 | +> Through this configuration file, you can set up localized URLs, middleware redirection, cookie names, the location and extension of your content declarations, disable Intlayer logs in the console, and more. For a complete list of available parameters, refer to the [configuration documentation](https://github.com/aymericzip/intlayer/blob/main/docs/en/configuration.md). |
| 109 | +
|
| 110 | +### Step 3: Integrate Intlayer in Your Vite Configuration |
| 111 | +
|
| 112 | +Add the intlayer plugin into your configuration. |
| 113 | +
|
| 114 | +```typescript fileName="vite.config.ts" codeFormat="typescript" |
| 115 | +import { defineConfig } from "vite"; |
| 116 | +import react from "@vitejs/plugin-react-swc"; |
| 117 | +import { intlayerPlugin } from "vite-intlayer"; |
| 118 | +
|
| 119 | +// https://vitejs.dev/config/ |
| 120 | +export default defineConfig({ |
| 121 | + plugins: [react(), intlayerPlugin()], |
| 122 | +}); |
| 123 | +``` |
| 124 | +
|
| 125 | +```javascript fileName="vite.config.mjs" codeFormat="esm" |
| 126 | +import { defineConfig } from "vite"; |
| 127 | +import react from "@vitejs/plugin-react-swc"; |
| 128 | +import { intlayerPlugin } from "vite-intlayer"; |
| 129 | +
|
| 130 | +// https://vitejs.dev/config/ |
| 131 | +export default defineConfig({ |
| 132 | + plugins: [react(), intlayerPlugin()], |
| 133 | +}); |
| 134 | +``` |
| 135 | +
|
| 136 | +```javascript fileName="vite.config.cjs" codeFormat="commonjs" |
| 137 | +const { defineConfig } = require("vite"); |
| 138 | +const react = require("@vitejs/plugin-react-swc"); |
| 139 | +const { intlayerPlugin } = require("vite-intlayer"); |
| 140 | +
|
| 141 | +// https://vitejs.dev/config/ |
| 142 | +module.exports = defineConfig({ |
| 143 | + plugins: [react(), intlayerPlugin()], |
| 144 | +}); |
| 145 | +``` |
| 146 | +
|
| 147 | +> The `intlayerPlugin()` Vite plugin is used to integrate Intlayer with Vite. It ensures the building of content declaration files and monitors them in development mode. It defines Intlayer environment variables within the Vite application. Additionally, it provides aliases to optimize performance. |
| 148 | +
|
| 149 | +### Step 4: Declare Your Content |
| 150 | +
|
| 151 | +Create and manage your content declarations to store translations: |
| 152 | +
|
| 153 | +```tsx fileName="src/app.content.tsx" contentDeclarationFormat="typescript" |
| 154 | +import { t, type Dictionary } from "intlayer"; |
| 155 | +
|
| 156 | +const appContent = { |
| 157 | + key: "app", |
| 158 | + content: {}, |
| 159 | +} satisfies Dictionary; |
| 160 | +
|
| 161 | +export default appContent; |
| 162 | +``` |
| 163 | +
|
| 164 | +```javascript fileName="src/app.content.mjs" contentDeclarationFormat="esm" |
| 165 | +import { t } from "intlayer"; |
| 166 | +
|
| 167 | +/** @type {import('intlayer').Dictionary} */ |
| 168 | +const appContent = { |
| 169 | + key: "app", |
| 170 | + content: {}, |
| 171 | +}; |
| 172 | +
|
| 173 | +export default appContent; |
| 174 | +``` |
| 175 | +
|
| 176 | +```javascript fileName="src/app.content.cjs" contentDeclarationFormat="commonjs" |
| 177 | +const { t } = require("intlayer"); |
| 178 | +
|
| 179 | +/** @type {import('intlayer').Dictionary} */ |
| 180 | +const appContent = { |
| 181 | + key: "app", |
| 182 | + content: {}, |
| 183 | +}; |
| 184 | +
|
| 185 | +module.exports = appContent; |
| 186 | +``` |
| 187 | +
|
| 188 | +```json fileName="src/app.content.json" contentDeclarationFormat="json" |
| 189 | +{ |
| 190 | + "$schema": "https://intlayer.org/schema.json", |
| 191 | + "key": "app", |
| 192 | + "content": {} |
| 193 | +} |
| 194 | +``` |
| 195 | +
|
| 196 | +> Your content declarations can be defined anywhere in your application as soon they are included into the `contentDir` directory (by default, `./src`). And match the content declaration file extension (by default, `.content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx}`). |
| 197 | +> For more details, refer to the [content declaration documentation](https://github.com/aymericzip/intlayer/blob/main/docs/en/dictionary/get_started.md). |
| 198 | +
|
| 199 | +### Step 5: Utilize Intlayer in Your Code |
| 200 | +
|
| 201 | +[to complete] |
| 202 | +
|
| 203 | +### (Optional) Step 6: Change the language of your content |
| 204 | +
|
| 205 | +[to complete] |
| 206 | +
|
| 207 | +### (Optional) Step 7: Add localized Routing to your application |
| 208 | +
|
| 209 | +[to complete] |
| 210 | +
|
| 211 | +### (Optional) Step 8: Change the URL when the locale changes |
| 212 | +
|
| 213 | +[to complete] |
| 214 | +
|
| 215 | +### (Optional) Step 9: Switch the HTML Language and Direction Attributes |
| 216 | +
|
| 217 | +[to complete] |
| 218 | +
|
| 219 | +### (Optional) Step 10: Creating a Localized Link Component |
| 220 | +
|
| 221 | +[to complete] |
| 222 | +
|
| 223 | +### Git Configuration |
| 224 | +
|
| 225 | +It is recommended to ignore the files generated by Intlayer. This allows you to avoid committing them to your Git repository. |
| 226 | +
|
| 227 | +To do this, you can add the following instructions to your `.gitignore` file: |
| 228 | +
|
| 229 | +```plaintext |
| 230 | +# Ignore the files generated by Intlayer |
| 231 | +.intlayer |
| 232 | +``` |
| 233 | +
|
| 234 | +### VS Code Extension |
| 235 | +
|
| 236 | +To improve your development experience with Intlayer, you can install the official **Intlayer VS Code Extension**. |
| 237 | +
|
| 238 | +[Install from the VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension) |
| 239 | +
|
| 240 | +This extension provides: |
| 241 | +
|
| 242 | +- **Autocompletion** for translation keys. |
| 243 | +- **Real-time error detection** for missing translations. |
| 244 | +- **Inline previews** of translated content. |
| 245 | +- **Quick actions** to easily create and update translations. |
| 246 | +
|
| 247 | +For more details on how to use the extension, refer to the [Intlayer VS Code Extension documentation](https://intlayer.org/doc/vs-code-extension). |
| 248 | +
|
| 249 | +--- |
| 250 | +
|
| 251 | +### Go Further |
| 252 | +
|
| 253 | +To go further, you can implement the [visual editor](https://github.com/aymericzip/intlayer/blob/main/docs/en/intlayer_visual_editor.md) or externalize your content using the [CMS](https://github.com/aymericzip/intlayer/blob/main/docs/en/intlayer_CMS.md). |
0 commit comments