@sveltia/i18n 0.1.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/LICENSE.txt +21 -0
- package/README.md +709 -0
- package/dist/index.svelte.d.ts +202 -0
- package/dist/index.svelte.js +514 -0
- package/package.json +101 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
export type Formats = {
|
|
2
|
+
/**
|
|
3
|
+
* Custom number format presets.
|
|
4
|
+
*/
|
|
5
|
+
number?: Record<string, Intl.NumberFormatOptions> | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* Custom date format presets.
|
|
8
|
+
*/
|
|
9
|
+
date?: Record<string, Intl.DateTimeFormatOptions> | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* Custom time format presets.
|
|
12
|
+
*/
|
|
13
|
+
time?: Record<string, Intl.DateTimeFormatOptions> | undefined;
|
|
14
|
+
};
|
|
15
|
+
export type MissingKeyHandler = (key: string, locale: string, defaultValue: string | undefined) => string | void;
|
|
16
|
+
export type MessageObject = {
|
|
17
|
+
/**
|
|
18
|
+
* Message key.
|
|
19
|
+
*/
|
|
20
|
+
id: string;
|
|
21
|
+
/**
|
|
22
|
+
* Variables to interpolate into the message.
|
|
23
|
+
*/
|
|
24
|
+
values?: Record<string, any> | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Locale override for this call.
|
|
27
|
+
*/
|
|
28
|
+
locale?: string | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Fallback string if the key is not found.
|
|
31
|
+
*/
|
|
32
|
+
default?: string | undefined;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Date/time formatting options, extending `Intl.DateTimeFormatOptions` with `locale` and `format`
|
|
36
|
+
* overrides.
|
|
37
|
+
*/
|
|
38
|
+
export type DateFormatOptions = Intl.DateTimeFormatOptions & {
|
|
39
|
+
locale?: string;
|
|
40
|
+
format?: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Number formatting options, extending `Intl.NumberFormatOptions` with `locale` and `format`
|
|
44
|
+
* overrides.
|
|
45
|
+
*/
|
|
46
|
+
export type NumberFormatOptions = Intl.NumberFormatOptions & {
|
|
47
|
+
locale?: string;
|
|
48
|
+
format?: string;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Format a message by key.
|
|
52
|
+
*
|
|
53
|
+
* Supports two call signatures (matching svelte-i18n):
|
|
54
|
+
* - `format(id, options?)` — key as first argument
|
|
55
|
+
* - `format({ id, values, locale, default })` — options object only.
|
|
56
|
+
* @param {string | MessageObject} key Message key, or an object with `id` and options.
|
|
57
|
+
* @param {{ values?: Record<string, any>, locale?: string, default?: string }} [options] Formatting
|
|
58
|
+
* options when `key` is a string.
|
|
59
|
+
* @returns {string} The formatted message string.
|
|
60
|
+
*/
|
|
61
|
+
export function format(key: string | MessageObject, { values, locale: localeOverride, default: defaultString }?: {
|
|
62
|
+
values?: Record<string, any>;
|
|
63
|
+
locale?: string;
|
|
64
|
+
default?: string;
|
|
65
|
+
}): string;
|
|
66
|
+
/**
|
|
67
|
+
* Add new messages for a locale. Accepts flat or nested maps; nested objects are flattened to
|
|
68
|
+
* dot-separated keys (`field.name`). Multiple dicts can be passed and are merged in order, matching
|
|
69
|
+
* svelte-i18n's `addMessages(locale, ...dicts)` signature.
|
|
70
|
+
* @param {string} localeCode Locale.
|
|
71
|
+
* @param {...Record<string, any>} maps One or more message maps (flat or nested).
|
|
72
|
+
* @see https://messageformat.github.io/messageformat/api/messageformat.messageformat/
|
|
73
|
+
*/
|
|
74
|
+
export function addMessages(localeCode: string, ...maps: Record<string, any>[]): void;
|
|
75
|
+
/**
|
|
76
|
+
* Format a date value as a localized date string.
|
|
77
|
+
* @param {Date} value The date to format.
|
|
78
|
+
* @param {DateFormatOptions} [options] Formatting options.
|
|
79
|
+
* @returns {string} The formatted date string.
|
|
80
|
+
*/
|
|
81
|
+
export function date(value: Date, { locale: loc, format: fmt, ...rest }?: DateFormatOptions): string;
|
|
82
|
+
/**
|
|
83
|
+
* All registered resources.
|
|
84
|
+
* @type {Record<string, Record<string, Intl.MessageFormat>>}
|
|
85
|
+
*/
|
|
86
|
+
export const dictionary: Record<string, Record<string, Intl.MessageFormat>>;
|
|
87
|
+
/**
|
|
88
|
+
* Get the locale from a `key=value` pair in `window.location.hash`.
|
|
89
|
+
* @param {string} hashKey The key to look for in the hash.
|
|
90
|
+
* @returns {string | undefined} The hash parameter value, or `undefined` if not in a browser or not
|
|
91
|
+
* found.
|
|
92
|
+
*/
|
|
93
|
+
export function getLocaleFromHash(hashKey: string): string | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Get the locale from a pattern matched against `window.location.hostname`.
|
|
96
|
+
* @param {RegExp} hostnamePattern Pattern with a capture group for the locale code.
|
|
97
|
+
* @returns {string | undefined} The matched locale code, or `undefined` if not in a browser or no
|
|
98
|
+
* match.
|
|
99
|
+
*/
|
|
100
|
+
export function getLocaleFromHostname(hostnamePattern: RegExp): string | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* Get the user's preferred locale from the browser.
|
|
103
|
+
* @returns {string | undefined} The first navigator language, or `undefined` in non-browser
|
|
104
|
+
* environments.
|
|
105
|
+
*/
|
|
106
|
+
export function getLocaleFromNavigator(): string | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* Get the locale from a pattern matched against `window.location.pathname`.
|
|
109
|
+
* @param {RegExp} pathnamePattern Pattern with a capture group for the locale code.
|
|
110
|
+
* @returns {string | undefined} The matched locale code, or `undefined` if not in a browser or no
|
|
111
|
+
* match.
|
|
112
|
+
*/
|
|
113
|
+
export function getLocaleFromPathname(pathnamePattern: RegExp): string | undefined;
|
|
114
|
+
/**
|
|
115
|
+
* Get the locale from a URL query string parameter.
|
|
116
|
+
* @param {string} queryKey The query string key to read.
|
|
117
|
+
* @returns {string | undefined} The query parameter value, or `undefined` if not in a browser or
|
|
118
|
+
* not found.
|
|
119
|
+
*/
|
|
120
|
+
export function getLocaleFromQueryString(queryKey: string): string | undefined;
|
|
121
|
+
/**
|
|
122
|
+
* Initialize the locales.
|
|
123
|
+
* @param {object} args Arguments.
|
|
124
|
+
* @param {string} args.fallbackLocale Locale to be used for fallback.
|
|
125
|
+
* @param {string} [args.initialLocale] Locale to be used for the initial selection.
|
|
126
|
+
* @param {Formats} [args.formats] Custom named formats.
|
|
127
|
+
* @param {MissingKeyHandler} [args.handleMissingMessage] Called when a message key is not found.
|
|
128
|
+
* May return a string to use as a fallback.
|
|
129
|
+
*/
|
|
130
|
+
export function init(args: {
|
|
131
|
+
fallbackLocale: string;
|
|
132
|
+
initialLocale?: string | undefined;
|
|
133
|
+
formats?: Formats | undefined;
|
|
134
|
+
handleMissingMessage?: MissingKeyHandler | undefined;
|
|
135
|
+
}): void;
|
|
136
|
+
/**
|
|
137
|
+
* Whether locale messages are currently being loaded. Returns `true` after a locale is set but
|
|
138
|
+
* before its messages are available.
|
|
139
|
+
* @returns {boolean} `true` if messages are pending for the current locale, `false` otherwise.
|
|
140
|
+
*/
|
|
141
|
+
export function isLoading(): boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Whether the current locale is written right-to-left. Reactive: re-evaluates whenever the locale
|
|
144
|
+
* changes.
|
|
145
|
+
* @returns {boolean} `true` if the active locale is RTL, `false` otherwise.
|
|
146
|
+
*/
|
|
147
|
+
export function isRTL(): boolean;
|
|
148
|
+
/**
|
|
149
|
+
* Return a nested object of formatted strings for all keys under the given prefix. Equivalent to
|
|
150
|
+
* svelte-i18n's `$json()`. Useful for iterating over a group of messages.
|
|
151
|
+
* @param {string} prefix Key prefix (e.g. `'nav'` matches `nav.home`, `nav.about`, …).
|
|
152
|
+
* @param {{ locale?: string }} [options] Lookup options.
|
|
153
|
+
* @returns {Record<string, string> | undefined} Object mapping suffix keys to formatted strings, or
|
|
154
|
+
* `undefined` if no keys match the prefix.
|
|
155
|
+
*/
|
|
156
|
+
export function json(prefix: string, { locale: localeOverride }?: {
|
|
157
|
+
locale?: string;
|
|
158
|
+
}): Record<string, string> | undefined;
|
|
159
|
+
export namespace locale {
|
|
160
|
+
const current: string;
|
|
161
|
+
/**
|
|
162
|
+
* Set the current locale. Negotiates against registered locales (e.g. En-CA → en-US), updates
|
|
163
|
+
* `<html lang>` / `<html dir>`, and auto-triggers any registered loader.
|
|
164
|
+
* @param {string} value The locale to set.
|
|
165
|
+
* @returns {Promise<void>}
|
|
166
|
+
*/
|
|
167
|
+
function set(value: string): Promise<void>;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* All registered locales.
|
|
171
|
+
* @type {string[]}
|
|
172
|
+
*/
|
|
173
|
+
export const locales: string[];
|
|
174
|
+
/**
|
|
175
|
+
* Format a number as a localized string.
|
|
176
|
+
* @param {number} value The number to format.
|
|
177
|
+
* @param {NumberFormatOptions} [options] Formatting options.
|
|
178
|
+
* @returns {string} The formatted number string.
|
|
179
|
+
*/
|
|
180
|
+
export function number(value: number, { locale: loc, format: fmt, ...rest }?: NumberFormatOptions): string;
|
|
181
|
+
/**
|
|
182
|
+
* Register an async loader for a locale. The loader is called the first time
|
|
183
|
+
* `waitLocale(localeCode)` is invoked for that locale.
|
|
184
|
+
* @param {string} localeCode Locale.
|
|
185
|
+
* @param {() => Promise<Record<string, string>>} loader Function returning a message map.
|
|
186
|
+
*/
|
|
187
|
+
export function register(localeCode: string, loader: () => Promise<Record<string, string>>): void;
|
|
188
|
+
/**
|
|
189
|
+
* Format a date value as a localized time string.
|
|
190
|
+
* @param {Date} value The date to format.
|
|
191
|
+
* @param {DateFormatOptions} [options] Formatting options.
|
|
192
|
+
* @returns {string} The formatted time string.
|
|
193
|
+
*/
|
|
194
|
+
export function time(value: Date, { locale: loc, format: fmt, ...rest }?: DateFormatOptions): string;
|
|
195
|
+
/**
|
|
196
|
+
* Execute the registered loader for the given locale (or the current locale if omitted) and wait
|
|
197
|
+
* until the messages are loaded. Subsequent calls for the same locale return the same promise.
|
|
198
|
+
* @param {string} [localeCode] Defaults to `locale.current`.
|
|
199
|
+
* @returns {Promise<void>}
|
|
200
|
+
*/
|
|
201
|
+
export function waitLocale(localeCode?: string): Promise<void>;
|
|
202
|
+
export { format as _, format as t };
|