Skip to content

Commit

Permalink
feat: 🎸 export a store listing all locales available
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Nov 22, 2019
1 parent db21bb8 commit f58a20b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
61 changes: 27 additions & 34 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -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 resolvePath from 'object-resolve-path'
import memoize from 'micro-memoize'
Expand Down Expand Up @@ -36,10 +34,10 @@ function getAvailableLocale(locale: string) {
}

const lookupMessage = memoize((path: string, locale: string) => {
return (
(currentDictionary[locale] as any)[path] ||
resolvePath(currentDictionary[locale], path)
)
if (path in currentDictionary[locale]) {
return currentDictionary[locale][path]
}
return resolvePath(currentDictionary[locale], path)
})

const formatMessage: Formatter = (id, options = {}) => {
Expand All @@ -52,9 +50,7 @@ const formatMessage: Formatter = (id, options = {}) => {
const message = lookupMessage(id, locale)

if (!message) {
console.warn(
`[svelte-i18n] The message "${id}" was not found in the locale "${locale}".`,
)
console.warn(`[svelte-i18n] The message "${id}" was not found in the locale "${locale}".`)
if (defaultValue != null) return defaultValue
return id
}
Expand All @@ -64,26 +60,20 @@ const formatMessage: Formatter = (id, options = {}) => {
return getMessageFormatter(message, locale).format(values)
}

formatMessage.time = (t, options) =>
getTimeFormatter(currentLocale, options).format(t)
formatMessage.date = (d, options) =>
getDateFormatter(currentLocale, options).format(d)
formatMessage.number = (n, options) =>
getNumberFormatter(currentLocale, options).format(n)

formatMessage.capital = (path, options) => capital(formatMessage(path, options))
formatMessage.title = (path, options) => title(formatMessage(path, options))
formatMessage.upper = (path, options) => upper(formatMessage(path, options))
formatMessage.lower = (path, options) => lower(formatMessage(path, options))

const dictionary = writable({})
dictionary.subscribe(
(newDictionary: any) => (currentDictionary = newDictionary),
)

const locale = writable(null)
const localeSet = locale.set
locale.set = (newLocale: string) => {
formatMessage.time = (t, options) => getTimeFormatter(currentLocale, options).format(t)
formatMessage.date = (d, options) => getDateFormatter(currentLocale, options).format(d)
formatMessage.number = (n, options) => getNumberFormatter(currentLocale, options).format(n)
formatMessage.capital = (id, options) => capital(formatMessage(id, options))
formatMessage.title = (id, options) => title(formatMessage(id, options))
formatMessage.upper = (id, options) => upper(formatMessage(id, options))
formatMessage.lower = (id, options) => lower(formatMessage(id, options))

const $dictionary = writable({})
$dictionary.subscribe((newDictionary: any) => (currentDictionary = newDictionary))

const $locale = writable(null)
const localeSet = $locale.set
$locale.set = (newLocale: string): void | Promise<void> => {
const { locale, loader } = getAvailableLocale(newLocale)
if (typeof loader === 'function') {
return loader()
Expand All @@ -100,17 +90,20 @@ locale.set = (newLocale: string) => {

throw Error(`[svelte-i18n] Locale "${newLocale}" not found.`)
}
locale.update = (fn: (locale: string) => string) => localeSet(fn(currentLocale))
locale.subscribe((newLocale: string) => (currentLocale = newLocale))
$locale.update = (fn: (locale: string) => void | Promise<void>) => localeSet(fn(currentLocale))
$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
const defineMessages = (i: Record<string, MessageObject>) => i

export { customFormats, addCustomFormats } from './formatters'
export {
locale,
dictionary,
$locale as locale,
$dictionary as dictionary,
locales,
getClientLocale,
defineMessages,
format as _,
Expand Down
23 changes: 23 additions & 0 deletions src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,26 @@ export const getClientLocale = ({

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
// }

0 comments on commit f58a20b

Please sign in to comment.