1

I am trying to internationalize my Svelte app.

I am using Svelte with Snowpack. And svelte-i18n. I follow their tutorial on GitHub.

In the tutorial, they say :

"Note: Make sure to call your i18n.js file on your app's entry-point. If you're using Sapper, remember to also call init() on your server-side code (server.js)."

I don't know how to do it? Can anyone please help me?

Any help is very much appreciated.

Ulvi
  • 965
  • 12
  • 31

2 Answers2

2

I'm using Svelte with Svelte Kit, and I can't find any entry point like main.js or App.svelte anywhere, but putting the code in __layout.svelte works for me.

1

The entry point is the file where you initialize your app. If you are using the default Svelte template, the entry point is src/main.js. You can place the i18n code there.

import App from './App.svelte';
// copied from https://github.com/kaisermann/svelte-i18n/blob/main/docs/Getting%20Started.md#4-initializing
import { register, init, getLocaleFromNavigator } from 'svelte-i18n';

register('en', () => import('./en.json'));
register('en-US', () => import('./en-US.json'));
register('pt', () => import('./pt.json'));
// en, en-US and pt are not available yet

init({
    fallbackLocale: 'en',
    initialLocale: getLocaleFromNavigator(),
});
// starts loading 'en-US' and 'en'

// now render your app
const app = new App({
    target: document.body,
    props: {
        name: 'world'
    }
});

export default app;

The package also provides a template for doing this in Sapper.

Geoff Rich
  • 4,726
  • 9
  • 22
  • I did your suggestion but it didn't work for me. I am using svelte with snowpack. Could you please help me? I don't know why it is not working. I'm following the tutorial and no error is showing. Just a blank page. – Ulvi Feb 10 '21 at 21:05
  • 1
    Can you make a GitHub repo demonstrating the issue? It's hard to tell what's going on with the context you've provided. – Geoff Rich Feb 10 '21 at 21:33
  • Thank you! I posted a new question with my GitHub repo: https://stackoverflow.com/questions/66145620/svelte-i18n-with-snowpack-shows-blank-page-without-errors – Ulvi Feb 10 '21 at 21:51