It’s joyful to work with Svelte. The design is elegant and the robust first-party additions which can be coupled with, make building browser apps a pleasure.
The most famous i18n plugin for the progressive JavaScript framework Svelte is probably svelte-i18n.
Make sure you have Node.js and npm installed. It's best, if you have some experience with simple HTML, JavaScript and basic Svelte, before jumping to svelte-i18n.
addMessages('en', { welcome: 'Welcome to Your Svelte App' }); addMessages('de', { welcome: 'Willkommen zu deiner Svelte-App' });
let initialLocale; const detectedLocale = getLocaleFromNavigator(); // the locale could be region specific, i.e. de-CH if (lngs.indexOf(detectedLocale) > -1) initialLocale = detectedLocale; if (!initialLocale && detectedLocale.indexOf('-') > 0) { const foundLng = lngs.find((l) => detectedLocale.indexOf(l + '-') === 0); if (foundLng) initialLocale = foundLng; } if (!initialLocale) initialLocale = fallbackLocale;
init({ fallbackLocale, initialLocale });
Import the i18n.js file, in your main.js file:
1 2 3 4 5 6 7 8 9 10
import App from'./App.svelte';
import'./i18n';
const app = new App({ target: document.body, props: {} });
exportdefault app;
Now let's try to use our first internationalized text.
In your template import _ from svelte-i18n and use it like this:
addMessages('en', { welcome: 'Welcome to Your Svelte App', descr: 'Visit the {link} to learn how to build Svelte apps.', doc: 'Svelte tutorial' }); addMessages('de', { welcome: 'Willkommen zu deiner Svelte-App', descr: 'Besuchen Sie den {link}, um zu erfahren, wie Sie Svelte-Apps erstellen.', doc: 'Svelte Tutorial' });
Now, depending on your browser language you should see something like this:
Language Switcher
Now we will create a language switcher to make the content change between different languages.
addMessages('en', { welcome: 'Welcome to Your Svelte App', descr: 'Visit the {link} to learn how to build Svelte apps.', doc: 'Svelte tutorial' }); addMessages('de', { welcome: 'Willkommen zu deiner Svelte-App', descr: 'Besuchen Sie den {link}, um zu erfahren, wie Sie Svelte-Apps erstellen.', doc: 'Svelte Tutorial' });
locale.subscribe((lng) => { if (lng) localStorage.setItem('svelte-i18n-locale', lng); });
let initialLocale; const detectedLocale = localStorage.getItem('svelte-i18n-locale') || getLocaleFromNavigator(); // the locale could be region specific, i.e. de-CH if (lngs.indexOf(detectedLocale) > -1) initialLocale = detectedLocale; if (!initialLocale && detectedLocale.indexOf('-') > 0) { const foundLng = lngs.find((l) => detectedLocale.indexOf(l + '-') === 0); if (foundLng) initialLocale = foundLng; } if (!initialLocale) initialLocale = fallbackLocale;
init({ fallbackLocale, initialLocale });
🥳 Awesome, you've just created your first language switcher!
Having the translations in your code file works, but is not that suitable to work with, for translators.
Using locize separates the translations from the code.
Having imported all translations should look like this:
Now your translations are fetched directly from locize CDN.
🙀 This means you can fix translations without having to change your code or redeploy your app. 🤩
save missing translations
I wish newly added keys in the code, would automatically be saved to locize.
Your wish is my command!
Extend the i18n.js file with the locize api-key and the handleMissingMessage function:
const fallbackLocale = 'en'; const lngs = [fallbackLocale, 'de']; const namespace = 'messages'; const apiKey = 'my-api-key'; // do not expose your API-Key in production
Lastly, with the help of the auto-machinetranslation workflow, new keys not only gets added to locize automatically, while developing the app, but are also automatically translated into the target languages using machine translation:
Check out this video to see how the automatic machine translation workflow looks like!
const fallbackLocale = 'en'; const lngs = [fallbackLocale, 'de']; const namespace = 'messages'; const apiKey = 'my-api-key'; // do not expose your API-Key in production