aboutsummaryrefslogtreecommitdiff
path: root/src/stores/locale.ts
blob: 2105eb51e8cc02341bab13767bcdfa5dca641204 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { derived, type Readable } from 'svelte/store';
import { json } from 'svelte-i18n';
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;

type InterpolationValues = Record<string, InterpolationValue> | undefined;

interface Options {
	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);

						if (localisation === key.toString()) return undefined;

						const replaceValues = (
							localisation: InterpolationValues,
							values: InterpolationValues
						) => {
							if (typeof localisation !== 'object' || localisation === null) return localisation;

							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;
								}
							}

							return updatedLocalisation;
						};

						if (options.values)
							return replaceValues(localisation as unknown as InterpolationValues, options.values);

						return localisation;
					}
				}
			);
	}) as Readable<(options?: Options) => Locale>;
};

const locale = createLocale();

export default locale;