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

Prevent baseTranslation from flashing #22

Open
vineryap opened this issue May 21, 2022 · 9 comments
Open

Prevent baseTranslation from flashing #22

vineryap opened this issue May 21, 2022 · 9 comments

Comments

@vineryap
Copy link

vineryap commented May 21, 2022

I'm using SvelteKit, and so far it's working fine unless for 1 problem.
When refreshing the page, it always flashes the baseTranslation before the current locale translation.
The locale is persisted on the localStorage using persistentAtom.
I also added await translationsLoading(i18n) but same result.
And i18n.loading.get() always returning false

@ai
Copy link
Member

ai commented May 21, 2022

There will be no way to do not have baseTranslation in some first ms on the app runtime (because of the async nature).

But I agree that it is a bug since, at least, we should have i18n.loading.get() in true.

Can I ask you to debug the source of the problem since you have a reproduction case?

@vineryap
Copy link
Author

vineryap commented May 21, 2022

So I tested it again and I found that i18n.loading.get() and translationsLoading is working when I use it in the components where it is called the translation Message.

If I use i18n.loading.get() and translationsLoading in the <script context="module"> in __layout.svelte file, the i18n.loading.get() will always returning false.

// __layout.svelte

// something like this
<script context="module">
import { i18n } from '$lib/stores/i18n';
import { translationsLoading } from '@nanostores/i18n';

export const load = async () => {
  ...

  // i18n.loading.get() --> false
  if (i18n.loading.get()) {
    await translationsLoading(i18n))
  }

  ...
})
</script>

<main>
	<slot />
</main>

I'm guessing it is because the translation message is not used in the __layout.svelte file.
And it makes sense to me and doesn't seem like a bug, but just to be sure, I prefer to have a confirmation.
Is this the correct behavior?

In the component I can await the translation and put a fallback placeholder when fetching the translation, but is it possible to await all translations from the <script context="module"> inside the __layout.svelte or maybe from the hooks (server-side) before it gets to the client? Because it is very repetitive to do this on every component that needs translation.

@vineryap
Copy link
Author

Something like this would be nice
Screen Shot 2022-05-21 at 22 06 54

@ai
Copy link
Member

ai commented May 21, 2022

Can you try to make PR?

Sorry, I am busy on another open-source project now.

@vineryap
Copy link
Author

Yup, sure, I'll try to make it, but I might need some guidance.
Hopefully, I can solve it by myself though, I don't want to bother you too much.

Anyway, thanks for the great tools! Really appreciate it.

@ai
Copy link
Member

ai commented May 21, 2022

Sure, feel free to ask questions. I will try to explain and guide you.

@nymless
Copy link
Contributor

nymless commented May 21, 2022

I've recreated this issue in React, so it's not SveltKit problem. I think the loading.set(true/false) calls logic is broken in create-i18n. Can't yet figure how to fix.

@nymless
Copy link
Contributor

nymless commented May 22, 2022

Can you try your app with adding this line?

// @nanostores/i18n/create-i18n/index.js -- Line 99

  async function getTranslation(code, components) {
    await new Promise((resolve) => setTimeout(() => resolve(), 0)); // This Line
    loading.set(true);

It worked for React, an async issue. Changing of loading store to true. but component who used it, hasn't passed its callback yet. So it didn't know of loading store change.

@vineryap
Copy link
Author

Hi @nymless, it doesn't work with SvelteKit.
I'm trying to make the loading state changes according to whether the translations for the locale are fetched/imported.
But I still haven't found the way.
Anyway, thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants