@axium/client 0.40.0 → 0.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/locales.d.ts +1 -28
- package/dist/locales.js +3 -81
- package/dist/web/locales.js +5 -6
- package/lib/ZodForm.svelte +2 -2
- package/lib/ZodInput.svelte +4 -4
- package/package.json +2 -1
package/dist/locales.d.ts
CHANGED
|
@@ -1,28 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Current locale
|
|
4
|
-
*/
|
|
5
|
-
export declare let currentLocale: string;
|
|
6
|
-
export declare function countryName(code: string): string | undefined;
|
|
7
|
-
export declare function dateField(name: string): string | undefined;
|
|
8
|
-
export declare function conjoin(list: Iterable<string>): string;
|
|
9
|
-
export declare function disjoin(list: Iterable<string>): string;
|
|
10
|
-
export declare let currentMonthNames: string[];
|
|
11
|
-
export interface ReplacementOptions {
|
|
12
|
-
$default?: string;
|
|
13
|
-
/** Whether to treat the replacement as HTML */
|
|
14
|
-
$html?: boolean;
|
|
15
|
-
}
|
|
16
|
-
type Replacements<K extends string> = ReplacementOptions & (K extends LocaleKey ? LocaleKeys[K & LocaleKey] : Record<string, LocaleValue>);
|
|
17
|
-
type ReplacementsArgs<K extends string> = {} extends Replacements<K> ? [replacements?: Replacements<K>] : [replacements: Replacements<K>];
|
|
18
|
-
export declare function useLocale(newLocale: string): void;
|
|
19
|
-
export declare function escape(text: string): string;
|
|
20
|
-
/**
|
|
21
|
-
* Get localized text for a given translation key
|
|
22
|
-
* @example
|
|
23
|
-
* ```ts
|
|
24
|
-
* text(`example.translation.key.${dynamicPart}`, { a: 1, b: 2 });
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export declare function text<const K extends string = LocaleKey>(key: K, ...args: ReplacementsArgs<K>): string;
|
|
28
|
-
export {};
|
|
1
|
+
export { conjoin, countryName, dateField, disjoin, escape, text, type ReplacementOptions } from '@james-pre/locales';
|
package/dist/locales.js
CHANGED
|
@@ -1,82 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { error, warn } from 'ioium';
|
|
1
|
+
import * as locales from '@james-pre/locales';
|
|
3
2
|
import en from '../locales/en.json' with { type: 'json' };
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
let currentLoaded;
|
|
7
|
-
/**
|
|
8
|
-
* Current locale
|
|
9
|
-
*/
|
|
10
|
-
export let currentLocale = 'en';
|
|
11
|
-
let currentRegionNames;
|
|
12
|
-
export function countryName(code) {
|
|
13
|
-
return currentRegionNames.of(code);
|
|
14
|
-
}
|
|
15
|
-
let currentDateFields;
|
|
16
|
-
export function dateField(name) {
|
|
17
|
-
return currentDateFields.of(name);
|
|
18
|
-
}
|
|
19
|
-
let currentConjunction;
|
|
20
|
-
export function conjoin(list) {
|
|
21
|
-
return currentConjunction.format(list);
|
|
22
|
-
}
|
|
23
|
-
let currentDisjunction;
|
|
24
|
-
export function disjoin(list) {
|
|
25
|
-
return currentDisjunction.format(list);
|
|
26
|
-
}
|
|
27
|
-
export let currentMonthNames;
|
|
28
|
-
export function useLocale(newLocale) {
|
|
29
|
-
if (!loadedLocales[newLocale])
|
|
30
|
-
throw new Error('Locale is not available: ' + newLocale);
|
|
31
|
-
currentLocale = newLocale;
|
|
32
|
-
currentLoaded = loadedLocales[newLocale];
|
|
33
|
-
currentRegionNames = new Intl.DisplayNames(newLocale, { type: 'region' });
|
|
34
|
-
currentDateFields = new Intl.DisplayNames(newLocale, { type: 'dateTimeField' });
|
|
35
|
-
currentConjunction = new Intl.ListFormat(newLocale, { style: 'long', type: 'conjunction' });
|
|
36
|
-
currentDisjunction = new Intl.ListFormat(newLocale, { style: 'long', type: 'disjunction' });
|
|
37
|
-
const formatter = new Intl.DateTimeFormat(newLocale, { month: 'long' });
|
|
38
|
-
currentMonthNames = Array.from({ length: 12 }, (_, monthIndex) => formatter.format(new Date(Date.UTC(2000, monthIndex + 1, 1))));
|
|
39
|
-
}
|
|
40
|
-
useLocale('en');
|
|
41
|
-
const localeReplacement = /\{(\w+)\}/g;
|
|
42
|
-
const escapePattern = /[&<>"']/g;
|
|
43
|
-
const escapes = {
|
|
44
|
-
'&': '&',
|
|
45
|
-
'<': '<',
|
|
46
|
-
'>': '>',
|
|
47
|
-
'"': '"',
|
|
48
|
-
"'": ''',
|
|
49
|
-
};
|
|
50
|
-
export function escape(text) {
|
|
51
|
-
return text.replaceAll(escapePattern, ch => escapes[ch]);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Get localized text for a given translation key
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* text(`example.translation.key.${dynamicPart}`, { a: 1, b: 2 });
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
export function text(key, ...args) {
|
|
61
|
-
const values = Object.assign(Object.create(null), args[0]);
|
|
62
|
-
let text = getByString(currentLoaded, key) || values.$default;
|
|
63
|
-
if (!text && currentLocale != 'en') {
|
|
64
|
-
warn(`Missing translation in ${currentLocale}: ` + key);
|
|
65
|
-
text = getByString(en, key);
|
|
66
|
-
}
|
|
67
|
-
if (!text) {
|
|
68
|
-
error('Missing translation for key: ' + key);
|
|
69
|
-
text = `?${key}?`;
|
|
70
|
-
}
|
|
71
|
-
if (typeof text == 'object') {
|
|
72
|
-
error('Invalid translation key: ' + key);
|
|
73
|
-
text = values.$default || `!${key}!`;
|
|
74
|
-
}
|
|
75
|
-
return text.replaceAll(localeReplacement, (_, name) => {
|
|
76
|
-
if (!Object.hasOwn(values, name)) {
|
|
77
|
-
console.error(new Error(`Missing replacement value for ${key}: ${name}`));
|
|
78
|
-
values[name] = `<missing: ${name}>`;
|
|
79
|
-
}
|
|
80
|
-
return values.$html ? escape(String(values[name])) : String(values[name]);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
3
|
+
locales.extend('en', en);
|
|
4
|
+
export { conjoin, countryName, dateField, disjoin, escape, text } from '@james-pre/locales';
|
package/dist/web/locales.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as locales from '@james-pre/locales';
|
|
2
2
|
import { debug, warn } from 'ioium';
|
|
3
|
-
import { currentLocale, useLocale } from '../locales.js';
|
|
4
3
|
import { origin } from '../requests.js';
|
|
5
4
|
/**
|
|
6
5
|
* Fetch the translations the server has for a language and start using them.
|
|
7
6
|
* The server resolves the language, so for example asking for `en-US` may give you `en`.
|
|
8
7
|
* @returns the locale that is now in use
|
|
9
8
|
*/
|
|
10
|
-
export async function loadLocale(language = globalThis.navigator?.language ||
|
|
9
|
+
export async function loadLocale(language = globalThis.navigator?.language || locales.current) {
|
|
11
10
|
const response = await fetch(new URL('/locales/' + encodeURIComponent(language), origin));
|
|
12
11
|
if (!response.ok) {
|
|
13
12
|
warn(`No translations for ${language}:`, response.headers.get('content-type') == 'application/json' ? (await response.json()).message : response.statusText);
|
|
14
|
-
return
|
|
13
|
+
return locales.current;
|
|
15
14
|
}
|
|
16
15
|
const resolved = response.headers.get('Content-Language') || language;
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
locales.extend(resolved, await response.json());
|
|
17
|
+
locales.use(resolved);
|
|
19
18
|
debug('Using locale: ' + resolved);
|
|
20
19
|
return resolved;
|
|
21
20
|
}
|
package/lib/ZodForm.svelte
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { type ReplacementOptions, text } from '@axium/client/locales';
|
|
3
|
-
import
|
|
3
|
+
import * as locales from '@james-pre/locales';
|
|
4
4
|
import type { ZodObject } from 'zod';
|
|
5
5
|
import ZodInput from './ZodInput.svelte';
|
|
6
6
|
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
let { rootValue = $bindable(), schema, labels, updateValue, idPrefix }: Props = $props();
|
|
16
16
|
|
|
17
|
-
const localeInfo =
|
|
17
|
+
const localeInfo = locales.zod.get(schema);
|
|
18
18
|
|
|
19
19
|
function subText(name: string, replacements?: ReplacementOptions & Record<string, any>): string | undefined {
|
|
20
20
|
if (!localeInfo?.prefix) return replacements?.$default || labels?.[name] || name;
|
package/lib/ZodInput.svelte
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { text, type ReplacementOptions } from '@axium/client';
|
|
3
3
|
import type { ZodPref } from '@axium/core';
|
|
4
4
|
import feature from '@axium/core/features';
|
|
5
|
-
import
|
|
5
|
+
import * as locales from '@james-pre/locales';
|
|
6
6
|
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
7
7
|
import { getByString, pick, setByString } from 'utilium';
|
|
8
8
|
import { prettifyError, type z } from 'zod';
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
interface Props {
|
|
13
13
|
idPrefix?: string;
|
|
14
|
-
localeInfo?:
|
|
14
|
+
localeInfo?: locales.ZodInfo;
|
|
15
15
|
rootValue: any;
|
|
16
16
|
path: string;
|
|
17
17
|
label?: string;
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
idPrefix,
|
|
38
38
|
updateValue,
|
|
39
39
|
noLabel,
|
|
40
|
-
localeInfo =
|
|
40
|
+
localeInfo = locales.zod.get(schema),
|
|
41
41
|
} = props;
|
|
42
42
|
|
|
43
43
|
const id = (idPrefix ? idPrefix + ':' : '') + path.replaceAll(' ', '_');
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
function partText(part: z.core.$ZodType, literal: string): string {
|
|
141
|
-
const prefix =
|
|
141
|
+
const prefix = locales.zod.get(part)?.prefix;
|
|
142
142
|
return prefix ? text(`${prefix}.${literal}`, { $default: literal }) : literal;
|
|
143
143
|
}
|
|
144
144
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axium/client",
|
|
3
|
-
"version": "0.40.
|
|
3
|
+
"version": "0.40.1",
|
|
4
4
|
"author": "James Prevett <jp@jamespre.dev>",
|
|
5
5
|
"funding": {
|
|
6
6
|
"type": "individual",
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
53
|
"@axium/core": ">=0.41.3",
|
|
54
|
+
"@james-pre/locales": "^1.0.0",
|
|
54
55
|
"ioium": "^1.7.0",
|
|
55
56
|
"semver": "^7.7.4",
|
|
56
57
|
"svelte": "^5.36.0",
|