mi-intl 0.6.3

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.
@@ -0,0 +1,120 @@
1
+ export function reduceSupportedLangs(lng: string, supportedLngs: string[], browserLngs?: string[]): string[];
2
+ export class I18n {
3
+ /**
4
+ * @param {I18nOptions} options
5
+ */
6
+ constructor(options: I18nOptions);
7
+ options: {
8
+ version: string;
9
+ ns: string[];
10
+ localesPath: string;
11
+ useLabel: boolean;
12
+ log?: {
13
+ debug?: Function;
14
+ error?: Function;
15
+ };
16
+ cookie: {
17
+ name?: string;
18
+ path?: string;
19
+ sameSite?: string;
20
+ maxAge?: number;
21
+ domain?: string;
22
+ expires?: string | Date;
23
+ secure?: boolean;
24
+ } | {
25
+ name: string;
26
+ path: string;
27
+ };
28
+ };
29
+ /** default namespace */
30
+ defaultNs: string;
31
+ /** fallback language */
32
+ fallbackLng: string;
33
+ /** selected language for translation, must be part of supportedLngs */
34
+ lng: string;
35
+ /** supported languages with translations */
36
+ supportedLngs: string[];
37
+ /** user assigned language with variant */
38
+ userLng: string | undefined;
39
+ /** available resources (if any) */
40
+ resources: {
41
+ [lng: string]: {
42
+ [ns: string]: {
43
+ [label: string]: string;
44
+ };
45
+ };
46
+ };
47
+ /**
48
+ * translation function
49
+ * @param {string} label
50
+ * @param {object} values
51
+ * @returns {string}
52
+ */
53
+ t(label: string, values?: object): string;
54
+ /**
55
+ * get all supported languages
56
+ * @returns {string[]}
57
+ */
58
+ getLanguages(): string[];
59
+ /**
60
+ * get the user selected (cookie) or browser language
61
+ * @returns {string}
62
+ */
63
+ getUserLanguage(): string;
64
+ /**
65
+ * reset user selected language by deleting language cookie
66
+ */
67
+ resetUserLanguage(): void;
68
+ /**
69
+ * @protected
70
+ * @returns {[cookieLng:string, browserLng:string]}
71
+ */
72
+ protected _getSettings(): [cookieLng: string, browserLng: string];
73
+ /**
74
+ * @protected
75
+ * set language cookie using cookie options
76
+ */
77
+ protected _setCookie(): void;
78
+ /**
79
+ * @protected
80
+ * @param {string} [lng]
81
+ * @returns {string[]}
82
+ */
83
+ protected _setLanguage(lng?: string): string[];
84
+ /**
85
+ * changes the language
86
+ * @param {string} [lng]
87
+ * @return {Promise<void>}
88
+ */
89
+ changeLanguage(lng?: string): Promise<void>;
90
+ /**
91
+ * @param {string} ns
92
+ * @return {Promise<void>}
93
+ */
94
+ changeNamespace(ns: string): Promise<void>;
95
+ /**
96
+ * @param {string[]} lngs
97
+ * @param {string[]} [ns]
98
+ * @return {Promise<(LoadResponse|undefined|void)[]>}
99
+ */
100
+ loadLanguages(lngs?: string[], ns?: string[]): Promise<(LoadResponse | undefined | void)[]>;
101
+ /**
102
+ * @protected
103
+ * @param {{
104
+ * lng: string
105
+ * ns: string
106
+ * }} param0
107
+ * @return {Promise<undefined|LoadResponse>}
108
+ */
109
+ protected _load({ lng, ns }: {
110
+ lng: string;
111
+ ns: string;
112
+ }): Promise<undefined | LoadResponse>;
113
+ }
114
+ export type I18nOptions = import("./types.js").I18nOptions;
115
+ export type LoadResponse = {
116
+ ok: boolean;
117
+ status: number;
118
+ lng: string;
119
+ ns: string;
120
+ };
@@ -0,0 +1,7 @@
1
+ export { I18n } from "./i18n.js";
2
+ export { MiIntlMessage } from "./intl-message.js";
3
+ export { IntlConsumer } from "./intl-consumer.js";
4
+ export type I18nOptions = import("./types.js").I18nOptions;
5
+ export type IntlContext = import("./types.js").IntlContext;
6
+ export { cookieParse, cookieSerialize } from "./cookie.js";
7
+ export { MiIntlProvider, INTL_CONTEXT } from "./intl-provider.js";
@@ -0,0 +1,13 @@
1
+ /** @typedef {import('mi-element').MiElement} MiElement */
2
+ /**
3
+ * ContextConsumer subscribing to MiIntlProvider context
4
+ */
5
+ export class IntlConsumer extends ContextConsumer<any> {
6
+ /**
7
+ * @param {MiElement} miElement
8
+ * @param {boolean} [subscribe=true]
9
+ */
10
+ constructor(miElement: MiElement, subscribe?: boolean);
11
+ }
12
+ export type MiElement = import("mi-element").MiElement;
13
+ import { ContextConsumer } from 'mi-element';
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Connects IntlConsumer to MiIntlProvider context
3
+ * @example
4
+ * ```js
5
+ * import { define } from 'mi-element'
6
+ * import { MiIntlMessage } from 'mi-intl'
7
+ *
8
+ * define('mi-message',
9
+ * class extends MiIntlMessage {
10
+ * static get attributes() {
11
+ * return { label: String, value: String }
12
+ * }
13
+ * update() {
14
+ * this.renderRoot.textContent = this.t(this.label, this.value)
15
+ * }
16
+ * }
17
+ * )
18
+ * ```
19
+ */
20
+ export class MiIntlMessage extends MiElement {
21
+ t(label: any, value: any): any;
22
+ #private;
23
+ }
24
+ import { MiElement } from 'mi-element';
@@ -0,0 +1,50 @@
1
+ /** @typedef {import('./types.js').I18nOptions} I18nOptions */
2
+ /** @typedef {import('./types.js').IntlContext} IntlContext */
3
+ export const INTL_CONTEXT: "mi-intl";
4
+ export class MiIntlProvider extends MiElement {
5
+ static get attributes(): {
6
+ /** translation version */
7
+ version: string;
8
+ /** pre-selected language */
9
+ lng: StringConstructor;
10
+ /** default namespace */
11
+ defaultNs: string;
12
+ /** used namespaces, comma separated */
13
+ ns: string;
14
+ /** supported languages, comma separated */
15
+ supportedLngs: string;
16
+ /** path for loading resources */
17
+ localesPath: string;
18
+ /** use translation label */
19
+ useLabel: BooleanConstructor;
20
+ /** debugging support */
21
+ debug: boolean;
22
+ };
23
+ static get properties(): {
24
+ loading: boolean;
25
+ };
26
+ static template: string;
27
+ /**
28
+ * @param {I18nOptions} options
29
+ */
30
+ set options(options: I18nOptions);
31
+ i18n: I18n | undefined;
32
+ /**
33
+ * @protected
34
+ * @returns {IntlContext}
35
+ */
36
+ protected _contextValue(): IntlContext;
37
+ /**
38
+ * @param {string} [lng]
39
+ * @returns {Promise<void>}
40
+ */
41
+ changeLanguage(lng?: string): Promise<void>;
42
+ loading: boolean | undefined;
43
+ provider: ContextProvider<import("./types.js").IntlContext> | undefined;
44
+ update(): void;
45
+ }
46
+ export type I18nOptions = import("./types.js").I18nOptions;
47
+ export type IntlContext = import("./types.js").IntlContext;
48
+ import { MiElement } from 'mi-element';
49
+ import { I18n } from './i18n.js';
50
+ import { ContextProvider } from 'mi-element';
@@ -0,0 +1,90 @@
1
+ export interface I18nOptions {
2
+ /**
3
+ * list of supported languages with translations
4
+ */
5
+ supportedLngs: string[];
6
+ /**
7
+ * version info
8
+ */
9
+ version?: string;
10
+ /**
11
+ * selected language
12
+ */
13
+ lng?: string;
14
+ /**
15
+ * fallback language; Must be part of supportedLngs;
16
+ * Default is first supported language from `supportedLngs`
17
+ */
18
+ fallbackLng?: string;
19
+ /**
20
+ * supported namespaces; all required namespaces for required translations
21
+ * must be named to load all namespaces when changing languages
22
+ * @default ['translations']
23
+ */
24
+ ns?: string[];
25
+ /**
26
+ * default namespace
27
+ * @default 'translations'
28
+ */
29
+ defaultNs?: string;
30
+ /**
31
+ * path for resources e.g. '/locales/{lng}/{ns}.json'
32
+ * @default '/locales/{lng}/{ns}.json?v={version}'
33
+ */
34
+ localesPath?: string;
35
+ /**
36
+ * use t(label) for translation
37
+ * @default false
38
+ */
39
+ useLabel?: boolean;
40
+ /**
41
+ * debugging support
42
+ * @default false
43
+ */
44
+ debug?: boolean;
45
+ /**
46
+ * logger e.g. `{ debug: console.debug, error: console.error }`
47
+ */
48
+ log?: {
49
+ debug?: Function;
50
+ error?: Function;
51
+ };
52
+ /**
53
+ * resources object
54
+ */
55
+ resources?: {
56
+ [lng: string]: {
57
+ [ns: string]: {
58
+ [label: string]: string;
59
+ };
60
+ };
61
+ };
62
+ /**
63
+ * language cookie
64
+ */
65
+ cookie?: {
66
+ /** @default "lc" */
67
+ name?: string;
68
+ /** @default "/" */
69
+ path?: string;
70
+ /** @default "Strict" */
71
+ sameSite?: string;
72
+ /** */
73
+ maxAge?: number;
74
+ domain?: string;
75
+ expires?: string | Date;
76
+ secure?: boolean;
77
+ };
78
+ }
79
+ export interface IntlContext {
80
+ /** translation function */
81
+ t: (label: string, values?: any) => string;
82
+ /** current selected language */
83
+ lng: string;
84
+ /** list all available translation languages */
85
+ getLanguages: () => string[];
86
+ /** change the current language */
87
+ changeLanguage: (lng: string) => Promise<void>;
88
+ /** language is loading */
89
+ loading: boolean;
90
+ }