aboutsummaryrefslogtreecommitdiff
path: root/src/stores/locale.ts
blob: 1b94ea969d64d49be5144c0d5600fc1c6b318a2b (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 FALLBACK_LOCALE = "en";

const isPlainObject = (value: unknown): value is Record<string, unknown> =>
	typeof value === "object" && value !== null && !Array.isArray(value);

const deepMerge = (fallback: unknown, current: unknown): unknown => {
	if (current === undefined || current === null) return fallback;
	if (!isPlainObject(current) || !isPlainObject(fallback)) return current;

	const merged: Record<string, unknown> = { ...fallback };
	for (const [key, value] of Object.entries(current)) {
		merged[key] = deepMerge(fallback[key], value);
	}
	return merged;
};

const createLocale = () => {
	return derived(json, ($json) => {
		return (options: Options = {}) =>
			new Proxy(
				{},
				{
					get(_target, key) {
						const keyStr = key.toString();
						const current = $json(keyStr, options.locale);
						const currentMissing = current === keyStr || current == null;

						let resolved: unknown;
						if (options.locale === FALLBACK_LOCALE) {
							if (currentMissing) return undefined;
							resolved = current;
						} else {
							const fallback = $json(keyStr, FALLBACK_LOCALE);
							const fallbackMissing =
								fallback === keyStr || fallback == null;

							if (currentMissing && fallbackMissing) return undefined;
							if (currentMissing) resolved = fallback;
							else if (fallbackMissing) resolved = current;
							else resolved = deepMerge(fallback, current);
						}

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

							const updatedLocalisation: InterpolationValues = {};

							for (const [k, value] of Object.entries(localisation)) {
								if (typeof value === "string") {
									updatedLocalisation[k] = value.replace(
										/\{(\w+)\}/g,
										(match, name) => (values ? values[name] : match) as string,
									);
								} else {
									updatedLocalisation[k] = replaceValues(
										value as InterpolationValues,
										values,
									) as InterpolationValue;
								}
							}

							return updatedLocalisation;
						};

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

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

const locale = createLocale();

export default locale;