origam 2.11.0 → 2.12.0
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/src/composables/Commons/locale.composable.cjs +6 -8
- package/dist/src/composables/Commons/locale.composable.d.ts +3 -3
- package/dist/src/composables/Commons/locale.composable.js +8 -7
- package/dist/src/interfaces/Commons/locale.interface.d.ts +30 -3
- package/dist/src/utils/Commons/locale.util.cjs +58 -2
- package/dist/src/utils/Commons/locale.util.d.ts +7 -3
- package/dist/src/utils/Commons/locale.util.js +56 -2
- package/package.json +1 -1
|
@@ -10,24 +10,22 @@ exports.provideRtl = provideRtl;
|
|
|
10
10
|
exports.useLocale = useLocale;
|
|
11
11
|
exports.useRtl = useRtl;
|
|
12
12
|
var _vue = require("vue");
|
|
13
|
-
var _vueI18n = require("vue-i18n");
|
|
14
13
|
var origamMessages = _interopRequireWildcard(require("../../assets/locales/index.cjs"));
|
|
15
14
|
var _consts = require("../../consts/index.cjs");
|
|
16
15
|
var _utils = require("../../utils/index.cjs");
|
|
17
16
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
18
17
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
19
18
|
function createLocale(options) {
|
|
20
|
-
const
|
|
19
|
+
const merged = (0, _utils.mergeDeep)({
|
|
21
20
|
locale: "en",
|
|
22
21
|
fallbackLocale: "en",
|
|
23
|
-
messages: JSON.parse(JSON.stringify(origamMessages))
|
|
22
|
+
messages: JSON.parse(JSON.stringify(origamMessages))
|
|
24
23
|
// Fix problems with json file imported
|
|
25
|
-
legacy: false
|
|
26
24
|
}, options);
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
const locale = options?.adapter ?? (0, _utils.createBuiltinAdapter)({
|
|
26
|
+
current: (0, _vue.ref)(merged.locale),
|
|
27
|
+
fallback: (0, _vue.ref)(merged.fallbackLocale),
|
|
28
|
+
messages: (0, _vue.computed)(() => merged.messages)
|
|
31
29
|
});
|
|
32
30
|
const rtl = createRtl(locale, options);
|
|
33
31
|
return {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ILocaleInstance, ILocaleOptions, ILocaleProps, IRtlInstance, IRtlOptions, IRtlProps } from "../../interfaces";
|
|
1
|
+
import type { ILocaleInstance, ILocaleOptions, ILocaleProps, IRtlInstance, IRtlOptions, IRtlProps, ILocaleMessages } from "../../interfaces";
|
|
2
2
|
/*********************************************************
|
|
3
3
|
* createLocale
|
|
4
4
|
********************************************************/
|
|
@@ -6,7 +6,7 @@ export declare function createLocale(options?: ILocaleOptions & IRtlOptions): {
|
|
|
6
6
|
isRtl: import("vue").Ref<boolean>;
|
|
7
7
|
rtl: import("vue").Ref<Record<string, boolean>>;
|
|
8
8
|
name: string;
|
|
9
|
-
messages: import("vue").ComputedRef<
|
|
9
|
+
messages: import("vue").ComputedRef<ILocaleMessages>;
|
|
10
10
|
current: import("vue").Ref<string>;
|
|
11
11
|
fallback: import("vue").Ref<string>;
|
|
12
12
|
t: (key: string, ...params: unknown[]) => string;
|
|
@@ -24,7 +24,7 @@ export declare function provideLocale(props: ILocaleProps & IRtlProps): {
|
|
|
24
24
|
isRtl: import("vue").Ref<boolean>;
|
|
25
25
|
rtl: import("vue").Ref<Record<string, boolean>>;
|
|
26
26
|
name: string;
|
|
27
|
-
messages: import("vue").ComputedRef<
|
|
27
|
+
messages: import("vue").ComputedRef<ILocaleMessages>;
|
|
28
28
|
current: import("vue").Ref<string>;
|
|
29
29
|
fallback: import("vue").Ref<string>;
|
|
30
30
|
t: (key: string, ...params: unknown[]) => string;
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { computed, inject, provide, ref } from "vue";
|
|
2
|
-
import { createI18n, useI18n } from "vue-i18n";
|
|
3
2
|
import * as origamMessages from "../../assets/locales/index.js";
|
|
4
3
|
import { LOCALE_RTL_DEFAULT, ORIGAM_LOCALE_KEY } from "../../consts/index.js";
|
|
5
|
-
import {
|
|
4
|
+
import { createBuiltinAdapter, getCurrentInstanceName, mergeDeep } from "../../utils/index.js";
|
|
6
5
|
export function createLocale(options) {
|
|
7
|
-
const
|
|
6
|
+
const merged = mergeDeep({
|
|
8
7
|
locale: "en",
|
|
9
8
|
fallbackLocale: "en",
|
|
10
|
-
messages: JSON.parse(JSON.stringify(origamMessages))
|
|
9
|
+
messages: JSON.parse(JSON.stringify(origamMessages))
|
|
11
10
|
// Fix problems with json file imported
|
|
12
|
-
legacy: false
|
|
13
11
|
}, options);
|
|
14
|
-
const
|
|
15
|
-
|
|
12
|
+
const locale = options?.adapter ?? createBuiltinAdapter({
|
|
13
|
+
current: ref(merged.locale),
|
|
14
|
+
fallback: ref(merged.fallbackLocale),
|
|
15
|
+
messages: computed(() => merged.messages)
|
|
16
|
+
});
|
|
16
17
|
const rtl = createRtl(locale, options);
|
|
17
18
|
return { ...locale, ...rtl };
|
|
18
19
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ComputedRef, Ref } from "vue";
|
|
2
|
-
import type { I18n, useI18n } from "vue-i18n";
|
|
3
2
|
export interface ILocaleMessages {
|
|
4
3
|
[key: string]: ILocaleMessages | string;
|
|
5
4
|
}
|
|
@@ -17,9 +16,37 @@ export interface ILocaleOptions {
|
|
|
17
16
|
fallbackLocale: string;
|
|
18
17
|
messages: Record<string, unknown>;
|
|
19
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Formes STRUCTURELLES de vue-i18n, declarees localement.
|
|
21
|
+
*
|
|
22
|
+
* `vue-i18n` est un peer OPTIONNEL : le DS ne doit pas en dependre, meme au
|
|
23
|
+
* niveau des types. Un `import type` reste inoffensif a l'execution mais fait
|
|
24
|
+
* echouer le type-check d'un projet qui ne l'installe pas (des lors que
|
|
25
|
+
* `skipLibCheck` est a false) — c'est la meme classe de bug que l'import
|
|
26
|
+
* runtime, un cran plus loin.
|
|
27
|
+
*
|
|
28
|
+
* On decrit donc uniquement ce que le DS consomme reellement. Une vraie
|
|
29
|
+
* instance vue-i18n satisfait ces formes structurellement, sans que le DS ait
|
|
30
|
+
* a connaitre le paquet.
|
|
31
|
+
*/
|
|
32
|
+
export interface IVueI18nGlobalLike {
|
|
33
|
+
locale: Ref<string>;
|
|
34
|
+
fallbackLocale: unknown;
|
|
35
|
+
messages: unknown;
|
|
36
|
+
t: (key: string, ...args: unknown[]) => string;
|
|
37
|
+
n: (value: number, ...args: unknown[]) => string;
|
|
38
|
+
}
|
|
39
|
+
export interface IVueI18nLike {
|
|
40
|
+
global: IVueI18nGlobalLike;
|
|
41
|
+
}
|
|
42
|
+
export type TUseI18nLike = (options: Record<string, unknown>) => {
|
|
43
|
+
locale: Ref<string>;
|
|
44
|
+
t: (key: string, ...args: unknown[]) => string;
|
|
45
|
+
n: (value: number, ...args: unknown[]) => string;
|
|
46
|
+
};
|
|
20
47
|
export interface ILocaleI18n {
|
|
21
|
-
i18n:
|
|
22
|
-
useI18n:
|
|
48
|
+
i18n: IVueI18nLike;
|
|
49
|
+
useI18n: TUseI18nLike;
|
|
23
50
|
}
|
|
24
51
|
export interface ILocaleProps {
|
|
25
52
|
messages?: ILocaleMessages;
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.createBuiltinAdapter = createBuiltinAdapter;
|
|
6
7
|
exports.createProvideFunction = createProvideFunction;
|
|
7
8
|
exports.createVueI18nAdapter = createVueI18nAdapter;
|
|
8
9
|
exports.useProvided = useProvided;
|
|
@@ -16,7 +17,7 @@ function callI18nT(fn, key, params) {
|
|
|
16
17
|
}
|
|
17
18
|
function createVueI18nAdapter({
|
|
18
19
|
i18n,
|
|
19
|
-
useI18n
|
|
20
|
+
useI18n
|
|
20
21
|
}) {
|
|
21
22
|
const current = i18n.global.locale;
|
|
22
23
|
const fallback = i18n.global.fallbackLocale;
|
|
@@ -32,7 +33,7 @@ function createVueI18nAdapter({
|
|
|
32
33
|
current,
|
|
33
34
|
fallback,
|
|
34
35
|
messages,
|
|
35
|
-
useI18n
|
|
36
|
+
useI18n
|
|
36
37
|
})
|
|
37
38
|
};
|
|
38
39
|
}
|
|
@@ -77,4 +78,59 @@ function useProvided(props, prop, provided) {
|
|
|
77
78
|
}
|
|
78
79
|
});
|
|
79
80
|
return internal;
|
|
81
|
+
}
|
|
82
|
+
function resolveKey(messages, key) {
|
|
83
|
+
const value = key.split(".").reduce((acc, segment) => acc && typeof acc === "object" ? acc[segment] : void 0, messages);
|
|
84
|
+
return typeof value === "string" ? value : void 0;
|
|
85
|
+
}
|
|
86
|
+
function stringifyParam(raw) {
|
|
87
|
+
if (typeof raw === "string") return raw;
|
|
88
|
+
if (typeof raw === "number" || typeof raw === "boolean" || typeof raw === "bigint") {
|
|
89
|
+
return raw.toString();
|
|
90
|
+
}
|
|
91
|
+
return "";
|
|
92
|
+
}
|
|
93
|
+
function asNamedParams(params) {
|
|
94
|
+
if (params.length !== 1) return void 0;
|
|
95
|
+
const first = params[0];
|
|
96
|
+
if (first === null || typeof first !== "object" || Array.isArray(first)) return void 0;
|
|
97
|
+
return first;
|
|
98
|
+
}
|
|
99
|
+
function interpolate(template, params) {
|
|
100
|
+
const named = asNamedParams(params);
|
|
101
|
+
const lookup = token => {
|
|
102
|
+
if (named) return named[token];
|
|
103
|
+
if (/^\d+$/.test(token)) return params[Number(token)];
|
|
104
|
+
return void 0;
|
|
105
|
+
};
|
|
106
|
+
return template.replace(/\{([^{}]+)\}/g, (_match, token) => {
|
|
107
|
+
return stringifyParam(lookup(token));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function createBuiltinAdapter(data) {
|
|
111
|
+
const translate = (key, ...params) => {
|
|
112
|
+
if (typeof key !== "string" || !key) return key;
|
|
113
|
+
const all = data.messages.value;
|
|
114
|
+
const template = resolveKey(all?.[data.current.value] ?? {}, key) ?? resolveKey(all?.[data.fallback.value] ?? {}, key);
|
|
115
|
+
if (template === void 0) return key;
|
|
116
|
+
return params.length ? interpolate(template, params) : template;
|
|
117
|
+
};
|
|
118
|
+
return {
|
|
119
|
+
name: "origam-builtin",
|
|
120
|
+
current: data.current,
|
|
121
|
+
fallback: data.fallback,
|
|
122
|
+
messages: data.messages,
|
|
123
|
+
t: translate,
|
|
124
|
+
n: value => new Intl.NumberFormat(data.current.value).format(value),
|
|
125
|
+
provide: props => {
|
|
126
|
+
const current = useProvided(props, "locale", data.current);
|
|
127
|
+
const fallback = useProvided(props, "fallback", data.fallback);
|
|
128
|
+
const messages = useProvided(props, "messages", data.messages);
|
|
129
|
+
return createBuiltinAdapter({
|
|
130
|
+
current,
|
|
131
|
+
fallback,
|
|
132
|
+
messages
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
};
|
|
80
136
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ComputedRef, Ref } from "vue";
|
|
2
|
-
import {
|
|
3
|
-
import type { ILocaleI18n, ILocaleInstance, ILocaleMessages, ILocaleProps } from "../../interfaces";
|
|
2
|
+
import type { ILocaleI18n, ILocaleInstance, ILocaleMessages, ILocaleProps, TUseI18nLike } from "../../interfaces";
|
|
4
3
|
/**
|
|
5
4
|
* Create vue i 18 n adapter.
|
|
6
5
|
*
|
|
@@ -17,7 +16,7 @@ export declare function createProvideFunction(data: {
|
|
|
17
16
|
current: Ref<string>;
|
|
18
17
|
fallback: Ref<string>;
|
|
19
18
|
messages: ComputedRef<ILocaleMessages>;
|
|
20
|
-
useI18n:
|
|
19
|
+
useI18n: TUseI18nLike;
|
|
21
20
|
}): (props: ILocaleProps) => ILocaleInstance;
|
|
22
21
|
/**
|
|
23
22
|
* Use provided.
|
|
@@ -28,3 +27,8 @@ export declare function createProvideFunction(data: {
|
|
|
28
27
|
* @returns …
|
|
29
28
|
*/
|
|
30
29
|
export declare function useProvided<T>(props: any, prop: string, provided: Ref<T>): Ref<T>;
|
|
30
|
+
export declare function createBuiltinAdapter(data: {
|
|
31
|
+
current: Ref<string>;
|
|
32
|
+
fallback: Ref<string>;
|
|
33
|
+
messages: ComputedRef<ILocaleMessages>;
|
|
34
|
+
}): ILocaleInstance;
|
|
@@ -6,7 +6,7 @@ function callI18nT(fn, key, params) {
|
|
|
6
6
|
const list = params.map((p) => p == null ? "" : p);
|
|
7
7
|
return fn(key, list);
|
|
8
8
|
}
|
|
9
|
-
export function createVueI18nAdapter({ i18n, useI18n
|
|
9
|
+
export function createVueI18nAdapter({ i18n, useI18n }) {
|
|
10
10
|
const current = i18n.global.locale;
|
|
11
11
|
const fallback = i18n.global.fallbackLocale;
|
|
12
12
|
const messages = i18n.global.messages;
|
|
@@ -17,7 +17,7 @@ export function createVueI18nAdapter({ i18n, useI18n: useI18n2 }) {
|
|
|
17
17
|
messages,
|
|
18
18
|
t: (key, ...params) => callI18nT(i18n.global.t.bind(i18n.global), key, params),
|
|
19
19
|
n: i18n.global.n,
|
|
20
|
-
provide: createProvideFunction({ current, fallback, messages, useI18n
|
|
20
|
+
provide: createProvideFunction({ current, fallback, messages, useI18n })
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
export function createProvideFunction(data) {
|
|
@@ -57,3 +57,57 @@ export function useProvided(props, prop, provided) {
|
|
|
57
57
|
});
|
|
58
58
|
return internal;
|
|
59
59
|
}
|
|
60
|
+
function resolveKey(messages, key) {
|
|
61
|
+
const value = key.split(".").reduce(
|
|
62
|
+
(acc, segment) => acc && typeof acc === "object" ? acc[segment] : void 0,
|
|
63
|
+
messages
|
|
64
|
+
);
|
|
65
|
+
return typeof value === "string" ? value : void 0;
|
|
66
|
+
}
|
|
67
|
+
function stringifyParam(raw) {
|
|
68
|
+
if (typeof raw === "string") return raw;
|
|
69
|
+
if (typeof raw === "number" || typeof raw === "boolean" || typeof raw === "bigint") {
|
|
70
|
+
return raw.toString();
|
|
71
|
+
}
|
|
72
|
+
return "";
|
|
73
|
+
}
|
|
74
|
+
function asNamedParams(params) {
|
|
75
|
+
if (params.length !== 1) return void 0;
|
|
76
|
+
const first = params[0];
|
|
77
|
+
if (first === null || typeof first !== "object" || Array.isArray(first)) return void 0;
|
|
78
|
+
return first;
|
|
79
|
+
}
|
|
80
|
+
function interpolate(template, params) {
|
|
81
|
+
const named = asNamedParams(params);
|
|
82
|
+
const lookup = (token) => {
|
|
83
|
+
if (named) return named[token];
|
|
84
|
+
if (/^\d+$/.test(token)) return params[Number(token)];
|
|
85
|
+
return void 0;
|
|
86
|
+
};
|
|
87
|
+
return template.replace(/\{([^{}]+)\}/g, (_match, token) => {
|
|
88
|
+
return stringifyParam(lookup(token));
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
export function createBuiltinAdapter(data) {
|
|
92
|
+
const translate = (key, ...params) => {
|
|
93
|
+
if (typeof key !== "string" || !key) return key;
|
|
94
|
+
const all = data.messages.value;
|
|
95
|
+
const template = resolveKey(all?.[data.current.value] ?? {}, key) ?? resolveKey(all?.[data.fallback.value] ?? {}, key);
|
|
96
|
+
if (template === void 0) return key;
|
|
97
|
+
return params.length ? interpolate(template, params) : template;
|
|
98
|
+
};
|
|
99
|
+
return {
|
|
100
|
+
name: "origam-builtin",
|
|
101
|
+
current: data.current,
|
|
102
|
+
fallback: data.fallback,
|
|
103
|
+
messages: data.messages,
|
|
104
|
+
t: translate,
|
|
105
|
+
n: (value) => new Intl.NumberFormat(data.current.value).format(value),
|
|
106
|
+
provide: (props) => {
|
|
107
|
+
const current = useProvided(props, "locale", data.current);
|
|
108
|
+
const fallback = useProvided(props, "fallback", data.fallback);
|
|
109
|
+
const messages = useProvided(props, "messages", data.messages);
|
|
110
|
+
return createBuiltinAdapter({ current, fallback, messages });
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|