Adding multi-lingual support in SvelteKit, using typesafe-i18n
In this post, we'll cover how to set up typesafe-i18n in a SvelteKit app to support multiple languages.
Create the SvelteKit project
First, let's create the SvelteKit project.
Terminal
For the template, choose Skeleton Project. Then, for type-checking, I'll go
with Typescript (but you can choose what you prefer). And finally, for
additional options, I won't add anything since we won't need them for this
project. Feel free to add anything you'll need.
Enter inside the directory and install the dependencies.
Terminal
Adding typesafe-i18n
Add typesafe-i18n with this command. This will generate a
.typesafe-i18n.json file in the root of the project and a new script will
been added in packaged.json, named typesafe-i18n.
Terminal
Let's add a base locale inside our .typesafe-i18n.json file. I'll go with
en but feel free to choose another one.
.typesafe-i18n.json
In the svelte.config.js file, let's add a new alias. We will use it to access
the i18n folder from wherever we want without worrying about relative paths.
svelte.config.js
Lastly, run the following scripts in two different terminals.
This will create a folder named i18n inside the src folder if it doesn't
already exist. It will contains the translations for our app. It contains our
base locale and de. You can delete it or change it to another locale. I will
change it to fr. Feel free to explore inside.
Terminal
And run the vite dev server to view our website in the browser.
Terminal
Adding some translations
Let's add some translations. Inside your base locale's folder inside i18n,
add a key for the translation and the value of the translation. Then, do the
same with your other locales. Here are mines.
i18n/en/index.ts
i18n/fr/index.ts
Using our translations inside the app
Let's add a utils.ts file in the src folder with the following content.
utils.ts
Let's uncomment the Locals interface inside app.d.ts and add the following.
app.d.ts
Let's handle invalid locales by creating a new file lang.ts inside
src/params.
params/lang.ts
And add this inside hooks.server.ts (create it if needed). This will handle
preferred locale, invalid locales (by redirecting to the preferred locale) and
adding the locale and translation functions inside the request.
hooks.server.ts
Now, let's set up a root +layout.server.ts file, in which we will return the
locale, retrieved from locals.
+layout.server.ts
Then, let's load the translations inside our +layout.ts file. This will allow
us to access the translations in every route.
+layout.ts
Finally, inside your root +layout.svelte file, set the locale. Note that this
mut be happen before accessing any translations.
+layout.svelte
Now, create a folder [lang=lang] inside routes and move in our main page.
Let's remove the default content and show some custom text. LL is a readable
store that contains our translations.
+page.svelte
In the browser, the text will appear correctly, and changing the locale
manually will translate it correctly. However, it's not very practical to
change locales directly in the url. Let's implement a navbar to change it.
Changing locales
First, let's add some translations for the languages.
i18n/en/index.ts
i18n/fr/index.ts
Now, let's adapt our root +layout.svelte file to add a navbar.
+layout.svelte
You should now be able to change locales easily.
Conclusion
Voila, now you can simply add translations inside the corresponding files and
use them inside any +page.svelte file. Just make sure to add them inside the
[lang=lang] folder to access them in the browser.