feat: 🎸 export a store listing all locales available

This commit is contained in:
Christian Kaisermann 2019-11-19 17:12:15 -03:00
parent db21bb878a
commit f58a20b21e
2 changed files with 48 additions and 32 deletions

View File

@ -1,5 +1,3 @@
// todo maybe locale can be a promise so we can await for it on the template?
import { writable, derived } from 'svelte/store' import { writable, derived } from 'svelte/store'
import resolvePath from 'object-resolve-path' import resolvePath from 'object-resolve-path'
import memoize from 'micro-memoize' import memoize from 'micro-memoize'
@ -36,10 +34,10 @@ function getAvailableLocale(locale: string) {
} }
const lookupMessage = memoize((path: string, locale: string) => { const lookupMessage = memoize((path: string, locale: string) => {
return ( if (path in currentDictionary[locale]) {
(currentDictionary[locale] as any)[path] || return currentDictionary[locale][path]
resolvePath(currentDictionary[locale], path) }
) return resolvePath(currentDictionary[locale], path)
}) })
const formatMessage: Formatter = (id, options = {}) => { const formatMessage: Formatter = (id, options = {}) => {
@ -52,9 +50,7 @@ const formatMessage: Formatter = (id, options = {}) => {
const message = lookupMessage(id, locale) const message = lookupMessage(id, locale)
if (!message) { if (!message) {
console.warn( console.warn(`[svelte-i18n] The message "${id}" was not found in the locale "${locale}".`)
`[svelte-i18n] The message "${id}" was not found in the locale "${locale}".`,
)
if (defaultValue != null) return defaultValue if (defaultValue != null) return defaultValue
return id return id
} }
@ -64,26 +60,20 @@ const formatMessage: Formatter = (id, options = {}) => {
return getMessageFormatter(message, locale).format(values) return getMessageFormatter(message, locale).format(values)
} }
formatMessage.time = (t, options) => formatMessage.time = (t, options) => getTimeFormatter(currentLocale, options).format(t)
getTimeFormatter(currentLocale, options).format(t) formatMessage.date = (d, options) => getDateFormatter(currentLocale, options).format(d)
formatMessage.date = (d, options) => formatMessage.number = (n, options) => getNumberFormatter(currentLocale, options).format(n)
getDateFormatter(currentLocale, options).format(d) formatMessage.capital = (id, options) => capital(formatMessage(id, options))
formatMessage.number = (n, options) => formatMessage.title = (id, options) => title(formatMessage(id, options))
getNumberFormatter(currentLocale, options).format(n) formatMessage.upper = (id, options) => upper(formatMessage(id, options))
formatMessage.lower = (id, options) => lower(formatMessage(id, options))
formatMessage.capital = (path, options) => capital(formatMessage(path, options)) const $dictionary = writable({})
formatMessage.title = (path, options) => title(formatMessage(path, options)) $dictionary.subscribe((newDictionary: any) => (currentDictionary = newDictionary))
formatMessage.upper = (path, options) => upper(formatMessage(path, options))
formatMessage.lower = (path, options) => lower(formatMessage(path, options))
const dictionary = writable({}) const $locale = writable(null)
dictionary.subscribe( const localeSet = $locale.set
(newDictionary: any) => (currentDictionary = newDictionary), $locale.set = (newLocale: string): void | Promise<void> => {
)
const locale = writable(null)
const localeSet = locale.set
locale.set = (newLocale: string) => {
const { locale, loader } = getAvailableLocale(newLocale) const { locale, loader } = getAvailableLocale(newLocale)
if (typeof loader === 'function') { if (typeof loader === 'function') {
return loader() return loader()
@ -100,17 +90,20 @@ locale.set = (newLocale: string) => {
throw Error(`[svelte-i18n] Locale "${newLocale}" not found.`) throw Error(`[svelte-i18n] Locale "${newLocale}" not found.`)
} }
locale.update = (fn: (locale: string) => string) => localeSet(fn(currentLocale)) $locale.update = (fn: (locale: string) => void | Promise<void>) => localeSet(fn(currentLocale))
locale.subscribe((newLocale: string) => (currentLocale = newLocale)) $locale.subscribe((newLocale: string) => (currentLocale = newLocale))
const format = derived([$locale, $dictionary], () => formatMessage)
const locales = derived([$dictionary], ([$dictionary]) => Object.keys($dictionary))
const format = derived([locale, dictionary], () => formatMessage)
// 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 const defineMessages = (i: Record<string, MessageObject>) => i
export { customFormats, addCustomFormats } from './formatters' export { customFormats, addCustomFormats } from './formatters'
export { export {
locale, $locale as locale,
dictionary, $dictionary as dictionary,
locales,
getClientLocale, getClientLocale,
defineMessages, defineMessages,
format as _, format as _,

View File

@ -49,3 +49,26 @@ export const getClientLocale = ({
return locale || defaultLocale || fallback return locale || defaultLocale || fallback
} }
// function mergeDeep(target: any, source: any) {
// const isObject = (obj: any) => obj && typeof obj === 'object'
// if (!isObject(target) || !isObject(source)) {
// return source
// }
// Object.keys(source).forEach(key => {
// const targetValue = target[key]
// const sourceValue = source[key]
// if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
// target[key] = targetValue.concat(sourceValue)
// } else if (isObject(targetValue) && isObject(sourceValue)) {
// target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue)
// } else {
// target[key] = sourceValue
// }
// })
// return target
// }