mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 18:10:43 +01:00
feat: 🎸 add waitInitialLocale helper
This commit is contained in:
parent
766f196bba
commit
6ee28e7d27
@ -1,2 +1,3 @@
|
|||||||
/test/fixtures
|
/test/fixtures
|
||||||
dist/
|
dist/
|
||||||
|
example/
|
@ -7,9 +7,9 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { locale, locales, loading } from './dist/i18n.mjs'
|
import { locale, locales, isLoading } from './dist/i18n.mjs'
|
||||||
|
|
||||||
$: updateLangAttr($locale)
|
$: updateLangAttr($locale)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<slot loading={$loading} locale={$locale} locales={$locales} />
|
<slot isLoading={$isLoading} locale={$locale} locales={$locales} />
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
<script context="module">
|
<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'
|
import Intl from 'svelte-i18n/Intl.svelte'
|
||||||
|
|
||||||
export async function preload() {
|
export async function preload() {
|
||||||
const initialLocale = getClientLocale({
|
return waitInitialLocale({
|
||||||
default: 'en-US',
|
default: 'en-US',
|
||||||
navigator: true
|
navigator: true,
|
||||||
})
|
})
|
||||||
return locale.set(initialLocale)
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@ -16,7 +15,7 @@
|
|||||||
|
|
||||||
const localeLabels = {
|
const localeLabels = {
|
||||||
'pt-BR': 'Português',
|
'pt-BR': 'Português',
|
||||||
'en': 'English',
|
en: 'English',
|
||||||
'en-US': 'English US',
|
'en-US': 'English US',
|
||||||
'es-ES': 'Espanõl',
|
'es-ES': 'Espanõl',
|
||||||
}
|
}
|
||||||
@ -41,7 +40,7 @@
|
|||||||
right: 0;
|
right: 0;
|
||||||
top: 0;
|
top: 0;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
background-color: rgba(0, 0, 0, .5);
|
background-color: rgba(0, 0, 0, 0.5);
|
||||||
color: #fff;
|
color: #fff;
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
font-size: 4rem;
|
font-size: 4rem;
|
||||||
@ -49,12 +48,10 @@
|
|||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<Intl let:loading>
|
<Intl let:isLoading>
|
||||||
{#if loading}
|
{#if isLoading}
|
||||||
<div class="loading">Loading...</div>
|
<div class="loading">Loading...</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
@ -69,4 +66,4 @@
|
|||||||
</select>
|
</select>
|
||||||
<slot />
|
<slot />
|
||||||
</main>
|
</main>
|
||||||
</Intl>
|
</Intl>
|
||||||
|
@ -5,7 +5,7 @@ import {
|
|||||||
addMessagesTo,
|
addMessagesTo,
|
||||||
} from '../stores/dictionary'
|
} from '../stores/dictionary'
|
||||||
import { getCurrentLocale } from '../stores/locale'
|
import { getCurrentLocale } from '../stores/locale'
|
||||||
import { $loading } from '../stores/loading'
|
import { $isLoading } from '../stores/loading'
|
||||||
|
|
||||||
import { removeFromLookupCache } from './lookup'
|
import { removeFromLookupCache } from './lookup'
|
||||||
import { getLocalesFrom } from './utils'
|
import { getLocalesFrom } from './utils'
|
||||||
@ -54,7 +54,7 @@ export async function flushQueue(locale: string = getCurrentLocale()) {
|
|||||||
if (queue.length === 0) return
|
if (queue.length === 0) return
|
||||||
|
|
||||||
removeLocaleFromQueue(locale)
|
removeLocaleFromQueue(locale)
|
||||||
const loadingDelay = setTimeout(() => $loading.set(true), 200)
|
const loadingDelay = setTimeout(() => $isLoading.set(true), 200)
|
||||||
|
|
||||||
// todo what happens if some loader fails?
|
// todo what happens if some loader fails?
|
||||||
return Promise.all(queue.map(loader => loader()))
|
return Promise.all(queue.map(loader => loader()))
|
||||||
@ -66,7 +66,7 @@ export async function flushQueue(locale: string = getCurrentLocale()) {
|
|||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
clearTimeout(loadingDelay)
|
clearTimeout(loadingDelay)
|
||||||
$loading.set(false)
|
$isLoading.set(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { GetClientLocaleOptions } from '../types'
|
||||||
|
|
||||||
export function capital(str: string) {
|
export function capital(str: string) {
|
||||||
return str.replace(/(^|\s)\S/, l => l.toLocaleUpperCase())
|
return str.replace(/(^|\s)\S/, l => l.toLocaleUpperCase())
|
||||||
}
|
}
|
||||||
@ -47,15 +49,7 @@ export const getClientLocale = ({
|
|||||||
pathname,
|
pathname,
|
||||||
hostname,
|
hostname,
|
||||||
default: defaultLocale,
|
default: defaultLocale,
|
||||||
}: {
|
}: GetClientLocaleOptions) => {
|
||||||
navigator?: boolean
|
|
||||||
hash?: string | RegExp
|
|
||||||
search?: string | RegExp
|
|
||||||
fallback?: string
|
|
||||||
default?: string
|
|
||||||
pathname?: RegExp
|
|
||||||
hostname?: RegExp
|
|
||||||
}) => {
|
|
||||||
let locale
|
let locale
|
||||||
|
|
||||||
if (typeof window === 'undefined') {
|
if (typeof window === 'undefined') {
|
||||||
|
@ -1,9 +1,20 @@
|
|||||||
import merge from 'deepmerge'
|
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
|
// 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 { $locale as locale, loadLocale as preloadLocale } from './stores/locale'
|
||||||
export {
|
export {
|
||||||
@ -11,13 +22,12 @@ export {
|
|||||||
$locales as locales,
|
$locales as locales,
|
||||||
addMessagesTo,
|
addMessagesTo,
|
||||||
} from './stores/dictionary'
|
} 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'
|
export { $format as format, $format as _, $format as t } from './stores/format'
|
||||||
|
|
||||||
// utilities
|
// utilities
|
||||||
export { defineMessages, merge }
|
export { getClientLocale, merge }
|
||||||
export { customFormats, addCustomFormats } from './includes/formats'
|
export { customFormats, addCustomFormats } from './includes/formats'
|
||||||
export { getClientLocale } from './includes/utils'
|
|
||||||
export {
|
export {
|
||||||
flushQueue as waitLocale,
|
flushQueue as waitLocale,
|
||||||
registerLocaleLoader as register,
|
registerLocaleLoader as register,
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
import { writable } from 'svelte/store'
|
import { writable } from 'svelte/store'
|
||||||
|
|
||||||
export const $loading = writable(false)
|
export const $isLoading = writable(false)
|
||||||
|
@ -45,3 +45,13 @@ export interface Formatter extends FormatterFn {
|
|||||||
export interface MessagesLoader {
|
export interface MessagesLoader {
|
||||||
(): Promise<any>
|
(): Promise<any>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface GetClientLocaleOptions {
|
||||||
|
navigator?: boolean
|
||||||
|
hash?: string | RegExp
|
||||||
|
search?: string | RegExp
|
||||||
|
fallback?: string
|
||||||
|
default?: string
|
||||||
|
pathname?: RegExp
|
||||||
|
hostname?: RegExp
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user