svelte-i18n: Cannot format a message without first setting the initial locale when open _error route
Describe the bug
When I try open _error route with some translate in it or in _layout, library throw error
Logs

To Reproduce
- Clone i18n example repo
- Add some translate in
_errorOR_layout({$_('title.index', { default: 'Sapper project template!' })}, for example) - Open dont exist page (http://localhost:3000/ds)
Information about your project:
-
Browser: Chrome 82.0.4067.0
-
OS: Windows 10
-
svelte-i18nversion: latest -
Bundler: Rollup
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 9
- Comments: 15 (3 by maintainers)
For everyone coming across this issue using svelte@next. Here is my solution for now. Note that this is not prerendered at build-time.
routes/$layout.svelte
routes/index.svelte
static/lang/en.json.js
For me it was a problem that I use i18n-keys before they where actually initialized, so I needed to await the result:
As @jayk09 stated, the current solution is to use the
isLoadingstore to check if the library is still loading the messages while we don’t havepreloadsupport in error pages. You can also wrap your_layout.sveltecontent with a loading state.Hey @aksenovdev and @AlexanderBelkevich 👋 It seems that when going to an error page, the
_layout.sveltepreloadmethod is not accounted for. This is happening because the locale lazy loading is ending AFTER the error page is rendered. Will try to get back to you if I find some solution to this. I’m not sure if this is a known sapper behavior or a bug.I had the same issue, adding this worked for me:
Note that I have only one locale for now.
i was getting the same error until i used https://github.com/kaisermann/svelte-i18n/blob/master/docs/Locale.md#isloading
The best solution for me. Otherwise it will have too much duplicated code.
Can you explain why can’t we load it async instead of waiting for all the translations to be ready at this point?
I’m seeing the same issue, with sapper, on first load of a page. The first time I open the app in my browser I get this error, when reloading or even closing the browser & reoping the page everything works as expected.
Can’t reproduce in dev on a local machine, only when running as a google App Engine.