Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error pages not translated when accessed in browser for the first time #149

Open
jitterbux opened this issue Sep 18, 2023 · 2 comments
Open
Labels
bug Something isn't working examples Issue related to examples good first issue Good for newcomers

Comments

@jitterbux
Copy link

jitterbux commented Sep 18, 2023

Browser: Chrome 115.0.5790.170
os: macOS 13.2.1

If you try to visit https://locale-router-advanced.netlify.app/about/asdfasdfasdfasfdasdf

you will get:

error.shit.happens (404)
error.default

only after you reload the page manually again, you will then get the correct text:

Oh, dear... (404)
Page not found.

The same is true for the /cs/ and /de/ version.

UPDATE:
It seems that not only for the first time but also if you leave the page open in the browser for a certain amount of time a few minutes and you refresh it, you get the error.shit.happens instead of Oh, dear... translation again. At least, for me it behaves like that. I noticed it when I tried to refreshed older tab I opened some time ago in Chrome.

@jarda-svoboda jarda-svoboda added bug Something isn't working examples Issue related to examples labels Sep 18, 2023
@jarda-svoboda jarda-svoboda added the good first issue Good for newcomers label Oct 3, 2023
@MarilorV
Copy link

MarilorV commented Jan 9, 2024

Same for me. Did you or anyone found a solution ?

@twoends
Copy link

twoends commented May 12, 2024

I got the same error. I fixed it adding the translations in the error object inside the hooks.server.ts

export const handleError: HandleServerError = async ({ event }) => {
	const errorId = crypto.randomUUID();
	const { locals } = event;
	const { lang } = locals;

	await loadTranslations(lang, 'error');

	return {
		message: 'Whoops!',
		errorId,
		translations: translations.get()  // <--- here
	};
};

and then using it to load the translation inside the error page

<script>
	import '../app.pcss';
	import { page } from '$app/stores';
	import { t, locale, addTranslations } from '$lib/i18n/translations';

	const { status } = $page;
	addTranslations($page.error?.translations);  // <--- here
</script>

<main>
	<h1>{$t('error.shit.happens')} ({status})</h1>
	<p>{$t(`error.${status}`, { default: $t('error.default') })}</p>
	<br />
	<br />
	{$locale} – {$t(`lang.${$locale}`)}
</main>

Remember to add the translations field into the app.d.ts to avoid typescript errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working examples Issue related to examples good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants