mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 18:10:43 +01:00
refactor: 💡 remove deepmerge and dlv dependencies
No need for them anymore. We now flat the dicitonary partials on `addMessages`
This commit is contained in:
parent
e889a41e63
commit
eb3dbb6f74
@ -79,8 +79,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"commander": "^4.0.1",
|
||||
"deepmerge": "^4.2.2",
|
||||
"dlv": "^1.1.3",
|
||||
"estree-walker": "^0.9.0",
|
||||
"fast-memoize": "^2.5.1",
|
||||
"intl-messageformat": "^7.5.2",
|
||||
|
@ -7,7 +7,6 @@ import {
|
||||
Identifier,
|
||||
Literal,
|
||||
} from 'estree'
|
||||
import delve from 'dlv'
|
||||
import { walk } from 'estree-walker'
|
||||
import { Ast } from 'svelte/types/compiler/interfaces'
|
||||
import { parse } from 'svelte/compiler'
|
||||
@ -21,6 +20,9 @@ const DEFINE_MESSAGES_METHOD_NAME = 'defineMessages'
|
||||
const FORMAT_METHOD_NAMES = new Set(['format', '_', 't'])
|
||||
const IGNORED_UTILITIES = new Set(['number', 'date', 'time'])
|
||||
|
||||
const delve = (o: Record<string, any>, id: string) =>
|
||||
id.split('.').reduce((acc, path) => acc[path], o)
|
||||
|
||||
function isFormatCall(node: Node, imports: Set<string>) {
|
||||
if (node.type !== 'CallExpression') return false
|
||||
|
||||
|
@ -16,6 +16,21 @@ export function lower(str: string) {
|
||||
return str.toLocaleLowerCase()
|
||||
}
|
||||
|
||||
// could use a reduce, but a simple for-in has less footprint
|
||||
export const flatObj = (obj: Record<string, any>, prefix = '') => {
|
||||
const flatted: Record<string, string> = {}
|
||||
for (const key in obj) {
|
||||
const flatKey = prefix + key
|
||||
// we want plainobjects
|
||||
if (typeof obj[key] === 'object') {
|
||||
Object.assign(flatted, flatObj(obj[key], `${flatKey}.`))
|
||||
} else {
|
||||
flatted[flatKey] = obj[key]
|
||||
}
|
||||
}
|
||||
return flatted
|
||||
}
|
||||
|
||||
const getFromQueryString = (queryString: string, key: string) => {
|
||||
const keyVal = queryString.split('&').find(i => i.indexOf(`${key}=`) === 0)
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
import delve from 'dlv'
|
||||
import merge from 'deepmerge'
|
||||
import { writable, derived } from 'svelte/store'
|
||||
|
||||
import { Dictionary } from '../types/index'
|
||||
import { LocaleDictionary, DeepDictionary, Dictionary } from '../types/index'
|
||||
import { flatObj } from '../includes/utils'
|
||||
|
||||
import { getFallbackOf } from './locale'
|
||||
|
||||
@ -10,7 +9,7 @@ let dictionary: Dictionary
|
||||
const $dictionary = writable<Dictionary>({})
|
||||
|
||||
export function getLocaleDictionary(locale: string) {
|
||||
return (dictionary[locale] as Dictionary) || null
|
||||
return (dictionary[locale] as LocaleDictionary) || null
|
||||
}
|
||||
|
||||
export function getDictionary() {
|
||||
@ -27,8 +26,6 @@ export function getMessageFromDictionary(locale: string, id: string) {
|
||||
if (id in localeDictionary) {
|
||||
return localeDictionary[id]
|
||||
}
|
||||
const message = delve(localeDictionary, id)
|
||||
if (message) return message
|
||||
}
|
||||
return null
|
||||
}
|
||||
@ -38,11 +35,11 @@ export function getClosestAvailableLocale(locale: string): string | null {
|
||||
return getClosestAvailableLocale(getFallbackOf(locale))
|
||||
}
|
||||
|
||||
export function addMessages(locale: string, ...partials: Dictionary[]) {
|
||||
export function addMessages(locale: string, ...partials: DeepDictionary[]) {
|
||||
const flattedPartials = partials.map(partial => flatObj(partial))
|
||||
|
||||
$dictionary.update(d => {
|
||||
dictionary[locale] = merge.all<Dictionary>(
|
||||
[getLocaleDictionary(locale) || {}].concat(partials)
|
||||
)
|
||||
d[locale] = Object.assign(d[locale] || {}, ...flattedPartials)
|
||||
return d
|
||||
})
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { Formats } from 'intl-messageformat'
|
||||
|
||||
export interface Dictionary {
|
||||
[key: string]: string | string[] | Dictionary | Dictionary[]
|
||||
export interface DeepDictionary {
|
||||
[key: string]: DeepDictionary | string | string[]
|
||||
}
|
||||
export type LocaleDictionary = Record<string, string>
|
||||
export type Dictionary = Record<string, LocaleDictionary>
|
||||
|
||||
export interface MessageObject {
|
||||
id?: string
|
||||
|
2
src/client/types/modules.d.ts
vendored
2
src/client/types/modules.d.ts
vendored
@ -1,2 +0,0 @@
|
||||
declare module 'dlv'
|
||||
declare module 'nano-memoize'
|
@ -40,12 +40,14 @@ test('merges the existing dictionaries with new ones', () => {
|
||||
en: {
|
||||
field_1: 'name',
|
||||
field_2: 'lastname',
|
||||
deep: { prop1: 'foo', prop2: 'foo' },
|
||||
'deep.prop1': 'foo',
|
||||
'deep.prop2': 'foo',
|
||||
},
|
||||
pt: {
|
||||
field_1: 'nome',
|
||||
field_2: 'sobrenome',
|
||||
deep: { prop1: 'foo', prop2: 'foo' },
|
||||
'deep.prop1': 'foo',
|
||||
'deep.prop2': 'foo',
|
||||
},
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user