DEV Community

Discussion on: Using i18Next with Svelte

Collapse
sbelzile profile image
Sébastien Belzile Author

(1) Where should getContext be defined?

getContext is provided by Svelte.
Link to documentation: svelte.dev/tutorial/context-api
So, import { getContext } from 'svelte';

(2) Does it fit?

index.svelte is fine, though I don't think it should go inside a <svelte:head> tag.

I would place that in the Svelte script section of your file, not in the markup. (add the script tag directly to the root)

<script>
    import { initLocalizationContext } from "../i18n/index.ts";
    initLocalizationContext();      
</script>

<svelte:head>
 [...]
Enter fullscreen mode Exit fullscreen mode

(3) Is the complete program as described in the post available somewhere to try?

Unfortunately it is not, all the snippets are adapted copies of a non-public codebase I worked on.

I encourage you to go through the tutorial on the Svelte site: svelte.dev/tutorial/basics . It does not take long to complete and shows very well most of Svelte features. I feel like this page svelte.dev/tutorial/adding-data and this page svelte.dev/tutorial/context-api could have provided answers your questions.