DEV Community

loading...

Discussion on: Using i18Next with Svelte

Collapse
mweissen profile image
mweissen

I like the amount very much and that's why I wanted to try the program right away. I tried it and spent a few hours in the process. Unfortunately, I could not solve the following problems:

(1) I get:
getContext is not defined (index.ts:31) Where should getContext be defined?

(2) "To wire it up, you need to call the initLocalizationContext function from the root of your application (usually App.svelte)." I put this in the index.svelte file:

<svelte:head>
    <script>
        import { initLocalizationContext } from "../i18n/index.ts";
        initLocalizationContext();      
    </script>
</svelte:head>
Enter fullscreen mode Exit fullscreen mode

Does this fit?

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

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.