From 1fb5c8c5b6008e5f9cb87e8ef01cef322d7c304f Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 21 Jan 2024 01:58:30 -0800 Subject: feat(locale): typed locale getter --- src/stores/locale.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/stores/locale.ts (limited to 'src/stores') diff --git a/src/stores/locale.ts b/src/stores/locale.ts new file mode 100644 index 00000000..ba657bf5 --- /dev/null +++ b/src/stores/locale.ts @@ -0,0 +1,40 @@ +import { derived, type Readable } from 'svelte/store'; +import { json } from 'svelte-i18n'; +import type { Locale } from '$lib/Locale/layout'; + +// type FormatXMLElementFn = (parts: Array) => R; + +// interface Options { +// id?: string; +// locale?: string; +// format?: string; +// default?: string; +// values?: +// | Record< +// string, +// string | number | boolean | Date | FormatXMLElementFn | null | undefined +// > +// | undefined; +// } + +const createLocale = () => { + return derived(json, ($json) => { + return (locale = undefined) => + new Proxy( + {}, + { + get(_target, key) { + const localisation = $json(key.toString(), locale); + + if (localisation === key.toString()) return undefined; + + return localisation; + } + } + ); + }) as Readable<(locale?: string) => Locale>; +}; + +const locale = createLocale(); + +export default locale; -- cgit v1.2.3