Skip to content

Commit

Permalink
Add loadingDelay
Browse files Browse the repository at this point in the history
  • Loading branch information
LorisSigrist committed Oct 3, 2023
1 parent 9ef4bc0 commit d6ca160
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/adapter/svelte/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<readonly [${locales
.map(addQuotes)
Expand Down Expand Up @@ -196,15 +205,21 @@ const loaders = {
${locales.map((locale) => ` "${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;
}
}
Expand Down

0 comments on commit d6ca160

Please sign in to comment.