feat: 🎸 addMessagesTo method

This commit is contained in:
Christian Kaisermann 2019-11-20 22:21:36 -03:00
parent a1dc268654
commit d6b8664009
2 changed files with 18 additions and 1 deletions

View File

@ -9,6 +9,7 @@ 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 { $format as format, $format as _, $format as t } from './stores/format'

View File

@ -1,4 +1,5 @@
import { writable, derived } from 'svelte/store'
import merge from 'deepmerge'
let dictionary: Record<string, Record<string, any>>
@ -15,8 +16,23 @@ function hasLocaleDictionary(locale: string) {
return locale in dictionary
}
function addMessagesTo(locale: string, ...partials: any[]) {
$dictionary.update(d => {
dictionary[locale] = merge.all<any>(
[dictionary[locale] || {}].concat(partials)
)
return d
})
}
const $locales = derived([$dictionary], ([$dictionary]) =>
Object.keys($dictionary)
)
export { $dictionary, $locales, getDictionary, hasLocaleDictionary }
export {
$dictionary,
$locales,
getDictionary,
hasLocaleDictionary,
addMessagesTo,
}