Skip to content

Commit

Permalink
feat: 🎸 add waitInitialLocale helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Nov 22, 2019
1 parent 766f196 commit 6ee28e7
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/test/fixtures
dist/
dist/
example/
4 changes: 2 additions & 2 deletions Intl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</script>

<script>
import { locale, locales, loading } from './dist/i18n.mjs'
import { locale, locales, isLoading } from './dist/i18n.mjs'
$: updateLangAttr($locale)
</script>

<slot loading={$loading} locale={$locale} locales={$locales} />
<slot isLoading={$isLoading} locale={$locale} locales={$locales} />
19 changes: 8 additions & 11 deletions example/src/routes/_layout.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<script context="module">
import { locales, locale, waitLocale,getClientLocale } from 'svelte-i18n'
import { locales, locale, waitInitialLocale } from 'svelte-i18n'
import Intl from 'svelte-i18n/Intl.svelte'
export async function preload() {
const initialLocale = getClientLocale({
return waitInitialLocale({
default: 'en-US',
navigator: true
navigator: true,
})
return locale.set(initialLocale)
}
</script>

Expand All @@ -16,7 +15,7 @@
const localeLabels = {
'pt-BR': 'Português',
'en': 'English',
en: 'English',
'en-US': 'English US',
'es-ES': 'Espanõl',
}
Expand All @@ -41,20 +40,18 @@
right: 0;
top: 0;
bottom: 0;
background-color: rgba(0, 0, 0, .5);
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
font-family: monospace;
font-size: 4rem;
display: flex;
justify-content: center;
align-items: center;
}
</style>

<Intl let:loading>
{#if loading}
<Intl let:isLoading>
{#if isLoading}
<div class="loading">Loading...</div>
{/if}

Expand All @@ -69,4 +66,4 @@
</select>
<slot />
</main>
</Intl>
</Intl>
6 changes: 3 additions & 3 deletions src/client/includes/loaderQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
addMessagesTo,
} from '../stores/dictionary'
import { getCurrentLocale } from '../stores/locale'
import { $loading } from '../stores/loading'
import { $isLoading } from '../stores/loading'

import { removeFromLookupCache } from './lookup'
import { getLocalesFrom } from './utils'
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function flushQueue(locale: string = getCurrentLocale()) {
if (queue.length === 0) return

removeLocaleFromQueue(locale)
const loadingDelay = setTimeout(() => $loading.set(true), 200)
const loadingDelay = setTimeout(() => $isLoading.set(true), 200)

// todo what happens if some loader fails?
return Promise.all(queue.map(loader => loader()))
Expand All @@ -66,7 +66,7 @@ export async function flushQueue(locale: string = getCurrentLocale()) {
})
.then(() => {
clearTimeout(loadingDelay)
$loading.set(false)
$isLoading.set(false)
})
}

Expand Down
12 changes: 3 additions & 9 deletions src/client/includes/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GetClientLocaleOptions } from '../types'

export function capital(str: string) {
return str.replace(/(^|\s)\S/, l => l.toLocaleUpperCase())
}
Expand Down Expand Up @@ -47,15 +49,7 @@ export const getClientLocale = ({
pathname,
hostname,
default: defaultLocale,
}: {
navigator?: boolean
hash?: string | RegExp
search?: string | RegExp
fallback?: string
default?: string
pathname?: RegExp
hostname?: RegExp
}) => {
}: GetClientLocaleOptions) => {
let locale

if (typeof window === 'undefined') {
Expand Down
20 changes: 15 additions & 5 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import merge from 'deepmerge'

import { MessageObject } from './types'
import { GetClientLocaleOptions, MessageObject } from './types'
import { getClientLocale } from './includes/utils'
import { $locale } from './stores/locale'

// defineMessages allow us to define and extract dynamic message ids
const defineMessages = (i: Record<string, MessageObject>) => i
export function defineMessages(i: Record<string, MessageObject>) {
return i
}

export function waitInitialLocale(options: GetClientLocaleOptions | string) {
if (typeof options === 'string') {
return $locale.set(options)
}
return $locale.set(getClientLocale(options))
}

export { $locale as locale, loadLocale as preloadLocale } from './stores/locale'
export {
$dictionary as dictionary,
$locales as locales,
addMessagesTo,
} from './stores/dictionary'
export { $loading as loading } from './stores/loading'
export { $isLoading as isLoading } from './stores/loading'
export { $format as format, $format as _, $format as t } from './stores/format'

// utilities
export { defineMessages, merge }
export { getClientLocale, merge }
export { customFormats, addCustomFormats } from './includes/formats'
export { getClientLocale } from './includes/utils'
export {
flushQueue as waitLocale,
registerLocaleLoader as register,
Expand Down
2 changes: 1 addition & 1 deletion src/client/stores/loading.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { writable } from 'svelte/store'

export const $loading = writable(false)
export const $isLoading = writable(false)
10 changes: 10 additions & 0 deletions src/client/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ export interface Formatter extends FormatterFn {
export interface MessagesLoader {
(): Promise<any>
}

export interface GetClientLocaleOptions {
navigator?: boolean
hash?: string | RegExp
search?: string | RegExp
fallback?: string
default?: string
pathname?: RegExp
hostname?: RegExp
}

0 comments on commit 6ee28e7

Please sign in to comment.