-
Version5.4.2 Describe the bugI'm trying to get a minimal working example in Sapper (can't use Sveltekit yet..) and the SSR output works but client then renders an empty translation. There was actually a similar bug for me when I tried to make sveltekit-i18n work with sapper (quick flash of SSR content before displaying empty translation). Any ideas? :) ReproductionStart a new sapper project and install/setup typesafe-i18n
Now edit the <script lang="ts" context="module">
import { loadLocaleAsync } from '../i18n/i18n-util.async'
export async function preload(page, session) {
const locale = 'en'
await loadLocaleAsync(locale)
// NOTE: Comment out this `return` and it works... hmm
return { props: { locale } }
}
</script>
<script lang="ts">
import Nav from '../components/Nav.svelte';
import { setLocale } from '../i18n/i18n-svelte'
export let segment: string;
export let locale: string;
setLocale(locale || 'en')
$: console.log("HI:", $LL.HI({ name: "You" }) )
</script> The server logs LogsNo response Config{
"adapter": "svelte",
"$schema": "https://unpkg.com/typesafe-i18n@5.4.2/schema/typesafe-i18n.json"
} Additional informationNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Strange, sounds like a Sapper bug if it runs the preload function only on the client if it doesn't return anything. Sou you could leave the return statement out. But you probably need the information on what locale is used. You could create language specific urls like Or you can hack it together like in this example: <script lang="ts" context="module">
import { loadFormatters, loadLocaleAsync } from '../i18n/i18n-util.async'
import { loadedLocales } from '../i18n/i18n-util'
export async function preload(page, session) {
const locale = 'en'
await loadLocaleAsync(locale)
// access the loaded translations and send it as a prop to the layout
const translations = loadedLocales[locale]
return { locale, translations }
}
</script>
<script lang="ts">
import LL, { setLocale } from '../i18n/i18n-svelte'
import type { Locales, Translation } from '../i18n/i18n-types'
export let locale: Locales
export let translations: Translation
// restore the translations from the received props
loadedLocales[locale] = translations
loadFormatters(locale)
setLocale(locale || ('en' as Locales))
$: console.log('HI:', $LL.HI({ name: 'You' }))
</script> The |
Beta Was this translation helpful? Give feedback.
Strange, sounds like a Sapper bug if it runs the preload function only on the client if it doesn't return anything.
Sou you could leave the return statement out. But you probably need the information on what locale is used. You could create language specific urls like
'/en/...'
and'/de/...'
and read the information from the page store.Or you can hack it together like in this example: