mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-06-30 18:54:49 +02: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
270aefa199
|
@ -79,8 +79,6 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"commander": "^4.0.1",
|
"commander": "^4.0.1",
|
||||||
"deepmerge": "^4.2.2",
|
|
||||||
"dlv": "^1.1.3",
|
|
||||||
"estree-walker": "^0.9.0",
|
"estree-walker": "^0.9.0",
|
||||||
"fast-memoize": "^2.5.1",
|
"fast-memoize": "^2.5.1",
|
||||||
"intl-messageformat": "^7.5.2",
|
"intl-messageformat": "^7.5.2",
|
||||||
|
|
|
@ -7,7 +7,6 @@ import {
|
||||||
Identifier,
|
Identifier,
|
||||||
Literal,
|
Literal,
|
||||||
} from 'estree'
|
} from 'estree'
|
||||||
import delve from 'dlv'
|
|
||||||
import { walk } from 'estree-walker'
|
import { walk } from 'estree-walker'
|
||||||
import { Ast } from 'svelte/types/compiler/interfaces'
|
import { Ast } from 'svelte/types/compiler/interfaces'
|
||||||
import { parse } from 'svelte/compiler'
|
import { parse } from 'svelte/compiler'
|
||||||
|
@ -21,6 +20,9 @@ const DEFINE_MESSAGES_METHOD_NAME = 'defineMessages'
|
||||||
const FORMAT_METHOD_NAMES = new Set(['format', '_', 't'])
|
const FORMAT_METHOD_NAMES = new Set(['format', '_', 't'])
|
||||||
const IGNORED_UTILITIES = new Set(['number', 'date', 'time'])
|
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>) {
|
function isFormatCall(node: Node, imports: Set<string>) {
|
||||||
if (node.type !== 'CallExpression') return false
|
if (node.type !== 'CallExpression') return false
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,21 @@ export function lower(str: string) {
|
||||||
return str.toLocaleLowerCase()
|
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 getFromQueryString = (queryString: string, key: string) => {
|
||||||
const keyVal = queryString.split('&').find(i => i.indexOf(`${key}=`) === 0)
|
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 { 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'
|
import { getFallbackOf } from './locale'
|
||||||
|
|
||||||
|
@ -10,7 +9,7 @@ let dictionary: Dictionary
|
||||||
const $dictionary = writable<Dictionary>({})
|
const $dictionary = writable<Dictionary>({})
|
||||||
|
|
||||||
export function getLocaleDictionary(locale: string) {
|
export function getLocaleDictionary(locale: string) {
|
||||||
return (dictionary[locale] as Dictionary) || null
|
return (dictionary[locale] as LocaleDictionary) || null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDictionary() {
|
export function getDictionary() {
|
||||||
|
@ -27,8 +26,6 @@ export function getMessageFromDictionary(locale: string, id: string) {
|
||||||
if (id in localeDictionary) {
|
if (id in localeDictionary) {
|
||||||
return localeDictionary[id]
|
return localeDictionary[id]
|
||||||
}
|
}
|
||||||
const message = delve(localeDictionary, id)
|
|
||||||
if (message) return message
|
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -38,11 +35,11 @@ export function getClosestAvailableLocale(locale: string): string | null {
|
||||||
return getClosestAvailableLocale(getFallbackOf(locale))
|
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.update(d => {
|
||||||
dictionary[locale] = merge.all<Dictionary>(
|
d[locale] = Object.assign(d[locale] || {}, ...flattedPartials)
|
||||||
[getLocaleDictionary(locale) || {}].concat(partials)
|
|
||||||
)
|
|
||||||
return d
|
return d
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import { Formats } from 'intl-messageformat'
|
import { Formats } from 'intl-messageformat'
|
||||||
|
|
||||||
export interface Dictionary {
|
export interface DeepDictionary {
|
||||||
[key: string]: string | string[] | Dictionary | Dictionary[]
|
[key: string]: DeepDictionary | string | string[]
|
||||||
}
|
}
|
||||||
|
export type LocaleDictionary = Record<string, string>
|
||||||
|
export type Dictionary = Record<string, LocaleDictionary>
|
||||||
|
|
||||||
export interface MessageObject {
|
export interface MessageObject {
|
||||||
id?: string
|
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: {
|
en: {
|
||||||
field_1: 'name',
|
field_1: 'name',
|
||||||
field_2: 'lastname',
|
field_2: 'lastname',
|
||||||
deep: { prop1: 'foo', prop2: 'foo' },
|
'deep.prop1': 'foo',
|
||||||
|
'deep.prop2': 'foo',
|
||||||
},
|
},
|
||||||
pt: {
|
pt: {
|
||||||
field_1: 'nome',
|
field_1: 'nome',
|
||||||
field_2: 'sobrenome',
|
field_2: 'sobrenome',
|
||||||
deep: { prop1: 'foo', prop2: 'foo' },
|
'deep.prop1': 'foo',
|
||||||
|
'deep.prop2': 'foo',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user