Currently delving into Svelte 3, I embarked on a project intended to be shared on GitHub in English. However, I realized that some of my friends do not speak English. To accommodate different language preferences, I decided to create a language switcher. Here's what I came up with:
<script lang="typescript">
var selectedLang = "en_us";
const _lang_filename = "./lang/%.json".replace("%", selectedLang);
import lang from _lang_filename;
</script>
<div id="_app_">
<h1>{lang.welcome}</h1>
<button
on:click={() => {
selectedLang = "pt_br";
}}>Português (Brasil)</button
>
<button
on:click={() => {
selectedLang = "en_us";
}}>English (United States)</button
>
</div>
Encountering an issue with the import of the lang file, I encountered an Unexpected Token error when trying to use a variable. I attempted incorporating string.replace() within the import itself, but to no avail.