diff options
Diffstat (limited to 'src/stores/locale.ts')
| -rw-r--r-- | src/stores/locale.ts | 96 |
1 files changed, 48 insertions, 48 deletions
diff --git a/src/stores/locale.ts b/src/stores/locale.ts index 2105eb51..ae4a5107 100644 --- a/src/stores/locale.ts +++ b/src/stores/locale.ts @@ -5,68 +5,68 @@ import type { Locale } from '$lib/Locale/layout'; type FormatXMLElementFn<T, R = string | T | (string | T)[]> = (parts: Array<string | T>) => R; type InterpolationValue = - | string - | number - | boolean - | Date - | FormatXMLElementFn<unknown> - | null - | undefined; + | string + | number + | boolean + | Date + | FormatXMLElementFn<unknown> + | null + | undefined; type InterpolationValues = Record<string, InterpolationValue> | undefined; interface Options { - id?: string; - locale?: string; - format?: string; - default?: string; - values?: InterpolationValues; + id?: string; + locale?: string; + format?: string; + default?: string; + values?: InterpolationValues; } const createLocale = () => { - return derived(json, ($json) => { - return (options: Options = {}) => - new Proxy( - {}, - { - get(_target, key) { - const localisation = $json(key.toString(), options.locale); + return derived(json, ($json) => { + return (options: Options = {}) => + new Proxy( + {}, + { + get(_target, key) { + const localisation = $json(key.toString(), options.locale); - if (localisation === key.toString()) return undefined; + if (localisation === key.toString()) return undefined; - const replaceValues = ( - localisation: InterpolationValues, - values: InterpolationValues - ) => { - if (typeof localisation !== 'object' || localisation === null) return localisation; + const replaceValues = ( + localisation: InterpolationValues, + values: InterpolationValues + ) => { + if (typeof localisation !== 'object' || localisation === null) return localisation; - const updatedLocalisation: InterpolationValues = {}; + const updatedLocalisation: InterpolationValues = {}; - for (const [key, value] of Object.entries(localisation)) { - if (typeof value === 'string') { - updatedLocalisation[key] = value.replace( - /\{(\w+)\}/g, - (match, name) => (values ? values[name] : match) as string - ); - } else { - updatedLocalisation[key] = replaceValues( - value as InterpolationValues, - values - ) as InterpolationValue; - } - } + for (const [key, value] of Object.entries(localisation)) { + if (typeof value === 'string') { + updatedLocalisation[key] = value.replace( + /\{(\w+)\}/g, + (match, name) => (values ? values[name] : match) as string + ); + } else { + updatedLocalisation[key] = replaceValues( + value as InterpolationValues, + values + ) as InterpolationValue; + } + } - return updatedLocalisation; - }; + return updatedLocalisation; + }; - if (options.values) - return replaceValues(localisation as unknown as InterpolationValues, options.values); + if (options.values) + return replaceValues(localisation as unknown as InterpolationValues, options.values); - return localisation; - } - } - ); - }) as Readable<(options?: Options) => Locale>; + return localisation; + } + } + ); + }) as Readable<(options?: Options) => Locale>; }; const locale = createLocale(); |