aboutsummaryrefslogtreecommitdiff
path: root/src/stores/locale.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores/locale.ts')
-rw-r--r--src/stores/locale.ts110
1 files changed, 58 insertions, 52 deletions
diff --git a/src/stores/locale.ts b/src/stores/locale.ts
index ae4a5107..826d717c 100644
--- a/src/stores/locale.ts
+++ b/src/stores/locale.ts
@@ -1,72 +1,78 @@
-import { derived, type Readable } from 'svelte/store';
-import { json } from 'svelte-i18n';
-import type { Locale } from '$lib/Locale/layout';
+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 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();