From d6ca160e0de69d68884ffa25e2660618caa2539a Mon Sep 17 00:00:00 2001 From: Loris Sigrist Date: Tue, 3 Oct 2023 14:05:42 +0200 Subject: [PATCH] Add loadingDelay --- README.md | 2 +- src/adapter/svelte/store.js | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8c56e8d..f1dfdd0 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ This package has not yet been published. Please be patient while I get it to MVP - [x] SSR - [ ] Fallback Locale - [ ] Sub Locales (eg EN_US, EN_UK) -- [ ] Loading Delay +- [x] Loading Delay - [ ] Namespaces - [ ] Message Description Intellisense - [ ] More File Formats diff --git a/src/adapter/svelte/store.js b/src/adapter/svelte/store.js index 85cbf42..e20e8e9 100644 --- a/src/adapter/svelte/store.js +++ b/src/adapter/svelte/store.js @@ -100,13 +100,22 @@ function generateDTS(localeDictionaries) { let code = `// FILE AUTOGENERATED BY t18s // You can safely add this to your .gitignore + +/** + * TypeSafe translations for your Svelte app. + */ declare module '${VIRTUAL_MODULE_PREFIX}' { import type { Writable } from 'svelte/store'; + /** + * The known locales + */ export type Locale = ${stringTypeUnion(locales)}; /** * A store containing the available locales. + * + * Note: This store will only ever change during development, it is constant in production. */ export const locales : Writable ` "${locale}": async () => (await import("$t18s/messages/${locale}")).default`)} } + +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + export async function loadLocale(newLocale) { - isLoading.set(true); + let done = false; try { + //To avoid showing the loading state too much, we allow a small delay before showing the loading state. + sleep(200).then(() => {if(!done) isLoading.set(true)}); const newMessages = await loaders[newLocale](); messages[newLocale] = newMessages; } catch(e) { console.error("[t18s] Failed to load locale " + newLocale + ": " + e.message); } finally { isLoading.set(false); + done = true; } }