piral-translate 1.4.0-beta.6276 → 1.4.0-beta.6287
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/esm/create.d.ts +2 -2
- package/esm/create.js +6 -11
- package/esm/create.js.map +1 -1
- package/esm/flatten-translations.d.ts +1 -1
- package/esm/flatten-translations.js +7 -16
- package/esm/flatten-translations.js.map +1 -1
- package/esm/types.d.ts +3 -2
- package/lib/create.d.ts +2 -2
- package/lib/create.js +6 -11
- package/lib/create.js.map +1 -1
- package/lib/flatten-translations.d.ts +1 -1
- package/lib/flatten-translations.js +8 -17
- package/lib/flatten-translations.js.map +1 -1
- package/lib/types.d.ts +3 -2
- package/package.json +3 -3
- package/piral-translate.min.js +1 -1
- package/src/create.ts +16 -15
- package/src/flatten-translations.ts +12 -27
- package/src/types.ts +4 -2
package/esm/create.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PiralPlugin } from 'piral-core';
|
|
2
|
-
import { PiletLocaleApi,
|
|
2
|
+
import type { PiletLocaleApi, Localizable, AnyLocalizationMessages } from './types';
|
|
3
3
|
export interface TranslationFallback {
|
|
4
4
|
(key: string, language: string): string;
|
|
5
5
|
}
|
|
@@ -14,7 +14,7 @@ export interface LocaleConfig {
|
|
|
14
14
|
* Sets the default (global) localization messages.
|
|
15
15
|
* @default {}
|
|
16
16
|
*/
|
|
17
|
-
messages?:
|
|
17
|
+
messages?: AnyLocalizationMessages;
|
|
18
18
|
/**
|
|
19
19
|
* Sets the default language to use.
|
|
20
20
|
*/
|
package/esm/create.js
CHANGED
|
@@ -37,16 +37,13 @@ export function createLocaleApi(localizer = setupLocalizer()) {
|
|
|
37
37
|
}));
|
|
38
38
|
return (api) => {
|
|
39
39
|
let localTranslations = {};
|
|
40
|
+
const setTranslations = (messages) => {
|
|
41
|
+
localTranslations = flattenTranslations(messages);
|
|
42
|
+
};
|
|
40
43
|
return {
|
|
41
44
|
addTranslations(messages, isOverriding = true) {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
messagesToMerge.unshift(localizer.messages);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
messagesToMerge.push(localizer.messages);
|
|
48
|
-
}
|
|
49
|
-
this.setTranslations(deepmerge.all(messagesToMerge));
|
|
45
|
+
const current = localizer.messages;
|
|
46
|
+
setTranslations(deepmerge.all(isOverriding ? [current, ...messages] : [...messages, current]));
|
|
50
47
|
},
|
|
51
48
|
getCurrentLanguage(cb) {
|
|
52
49
|
const selected = context.readState((s) => s.language.selected);
|
|
@@ -60,9 +57,7 @@ export function createLocaleApi(localizer = setupLocalizer()) {
|
|
|
60
57
|
}
|
|
61
58
|
return selected;
|
|
62
59
|
},
|
|
63
|
-
setTranslations
|
|
64
|
-
localTranslations = flattenTranslations(messages);
|
|
65
|
-
},
|
|
60
|
+
setTranslations,
|
|
66
61
|
getTranslations() {
|
|
67
62
|
return localTranslations;
|
|
68
63
|
},
|
package/esm/create.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,WAAW,CAAC;AAGvC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,WAAW,CAAC;AAGvC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAoC7D;;;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,2 @@
|
|
|
1
|
-
import { LocalizationMessages, NestedLocalizationMessages } from './types';
|
|
1
|
+
import type { LocalizationMessages, NestedLocalizationMessages } from './types';
|
|
2
2
|
export declare function flattenTranslations(messages: LocalizationMessages | NestedLocalizationMessages): LocalizationMessages;
|
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
export function flattenTranslations(messages) {
|
|
2
|
-
return Object.fromEntries(Object
|
|
3
|
-
.entries(messages)
|
|
4
|
-
.map(([language, translations]) => {
|
|
5
|
-
return [
|
|
6
|
-
language,
|
|
7
|
-
flat(translations)
|
|
8
|
-
];
|
|
9
|
-
}));
|
|
10
|
-
}
|
|
11
1
|
function flat(source) {
|
|
12
2
|
const target = {};
|
|
13
3
|
flatten(source, target);
|
|
@@ -19,14 +9,15 @@ function flatten(source, target, prop = '') {
|
|
|
19
9
|
return;
|
|
20
10
|
}
|
|
21
11
|
if (typeof source === 'object' && source !== null) {
|
|
22
|
-
Object
|
|
23
|
-
|
|
24
|
-
.forEach((key) => {
|
|
25
|
-
flatten(source[key], target, prop
|
|
26
|
-
? `${prop}.${key}`
|
|
27
|
-
: key);
|
|
12
|
+
Object.keys(source).forEach((key) => {
|
|
13
|
+
flatten(source[key], target, prop ? `${prop}.${key}` : key);
|
|
28
14
|
});
|
|
29
15
|
return;
|
|
30
16
|
}
|
|
31
17
|
}
|
|
18
|
+
export function flattenTranslations(messages) {
|
|
19
|
+
return Object.fromEntries(Object.entries(messages).map(([language, translations]) => {
|
|
20
|
+
return [language, flat(translations)];
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
32
23
|
//# sourceMappingURL=flatten-translations.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flatten-translations.js","sourceRoot":"","sources":["../src/flatten-translations.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"flatten-translations.js","sourceRoot":"","sources":["../src/flatten-translations.ts"],"names":[],"mappings":"AAEA,SAAS,IAAI,CAAC,MAA+B;IAC3C,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,OAAO,CAAC,MAAW,EAAE,MAA8B,EAAE,IAAI,GAAG,EAAE;IACrE,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;QAC9B,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;QAEtB,OAAO;KACR;IAED,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,EAAE;QACjD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAClC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAC9D,CAAC,CAAC,CAAC;QAEH,OAAO;KACR;AACH,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,QAA2D;IAC7F,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,YAAY,CAAC,EAAE,EAAE;QACxD,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;IACxC,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
|
package/esm/types.d.ts
CHANGED
|
@@ -119,6 +119,7 @@ export interface NestedLocalizationMessages {
|
|
|
119
119
|
*/
|
|
120
120
|
[lang: string]: NestedTranslations;
|
|
121
121
|
}
|
|
122
|
+
export type AnyLocalizationMessages = LocalizationMessages | NestedLocalizationMessages;
|
|
122
123
|
export interface PiletLocaleApi {
|
|
123
124
|
/**
|
|
124
125
|
* Adds a list of translations to the existing translations.
|
|
@@ -129,7 +130,7 @@ export interface PiletLocaleApi {
|
|
|
129
130
|
* @param messagesList The list of messages that extend the existing translations
|
|
130
131
|
* @param [isOverriding=true] Indicates whether the new translations overwrite the existing translations
|
|
131
132
|
*/
|
|
132
|
-
addTranslations(messagesList:
|
|
133
|
+
addTranslations(messagesList: Array<AnyLocalizationMessages>, isOverriding?: boolean): void;
|
|
133
134
|
/**
|
|
134
135
|
* Gets the currently selected language directly.
|
|
135
136
|
*/
|
|
@@ -151,7 +152,7 @@ export interface PiletLocaleApi {
|
|
|
151
152
|
* The translations will be exclusively used for retrieving translations for the pilet.
|
|
152
153
|
* @param messages The messages to use as translation basis.
|
|
153
154
|
*/
|
|
154
|
-
setTranslations(messages:
|
|
155
|
+
setTranslations(messages: AnyLocalizationMessages): void;
|
|
155
156
|
/**
|
|
156
157
|
* Gets the currently provided translations by the pilet.
|
|
157
158
|
*/
|
package/lib/create.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PiralPlugin } from 'piral-core';
|
|
2
|
-
import { PiletLocaleApi,
|
|
2
|
+
import type { PiletLocaleApi, Localizable, AnyLocalizationMessages } from './types';
|
|
3
3
|
export interface TranslationFallback {
|
|
4
4
|
(key: string, language: string): string;
|
|
5
5
|
}
|
|
@@ -14,7 +14,7 @@ export interface LocaleConfig {
|
|
|
14
14
|
* Sets the default (global) localization messages.
|
|
15
15
|
* @default {}
|
|
16
16
|
*/
|
|
17
|
-
messages?:
|
|
17
|
+
messages?: AnyLocalizationMessages;
|
|
18
18
|
/**
|
|
19
19
|
* Sets the default language to use.
|
|
20
20
|
*/
|
package/lib/create.js
CHANGED
|
@@ -41,16 +41,13 @@ function createLocaleApi(localizer = setupLocalizer()) {
|
|
|
41
41
|
}));
|
|
42
42
|
return (api) => {
|
|
43
43
|
let localTranslations = {};
|
|
44
|
+
const setTranslations = (messages) => {
|
|
45
|
+
localTranslations = (0, flatten_translations_1.flattenTranslations)(messages);
|
|
46
|
+
};
|
|
44
47
|
return {
|
|
45
48
|
addTranslations(messages, isOverriding = true) {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
messagesToMerge.unshift(localizer.messages);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
messagesToMerge.push(localizer.messages);
|
|
52
|
-
}
|
|
53
|
-
this.setTranslations(deepmerge.all(messagesToMerge));
|
|
49
|
+
const current = localizer.messages;
|
|
50
|
+
setTranslations(deepmerge.all(isOverriding ? [current, ...messages] : [...messages, current]));
|
|
54
51
|
},
|
|
55
52
|
getCurrentLanguage(cb) {
|
|
56
53
|
const selected = context.readState((s) => s.language.selected);
|
|
@@ -64,9 +61,7 @@ function createLocaleApi(localizer = setupLocalizer()) {
|
|
|
64
61
|
}
|
|
65
62
|
return selected;
|
|
66
63
|
},
|
|
67
|
-
setTranslations
|
|
68
|
-
localTranslations = (0, flatten_translations_1.flattenTranslations)(messages);
|
|
69
|
-
},
|
|
64
|
+
setTranslations,
|
|
70
65
|
getTranslations() {
|
|
71
66
|
return localTranslations;
|
|
72
67
|
},
|
package/lib/create.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AAAA,uCAAuC;AAGvC,uCAA0C;AAC1C,yCAAuC;AACvC,uCAA0C;
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AAAA,uCAAuC;AAGvC,uCAA0C;AAC1C,yCAAuC;AACvC,uCAA0C;AAC1C,iEAA6D;AAoC7D;;;GAGG;AACH,SAAgB,cAAc,CAAC,SAAuB,EAAE;IACtD,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC;IACnC,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpC,MAAM,WAAW,GAAG,SAAS,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;IACzC,MAAM,WAAW,GAAG,MAAM,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ACb,SAAS,CAAC,GAAG,CAA0B,YAAY,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,EAAE,OAAO,CAAC,CAAC,CACvG,CAAC;gBACJ,CAAC;gBACD,kBAAkB,CAAC,EAAwB;oBACzC,MAAM,QAAQ,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;oBAE/D,IAAI,EAAE,EAAE;wBACN,EAAE,CAAC,QAAQ,CAAC,CAAC;wBACb,MAAM,OAAO,GAAG,CAAC,EAA4B,EAAE,EAAE;4BAC/C,EAAE,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC;wBACzB,CAAC,CAAC;wBACF,GAAG,CAAC,EAAE,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC;wBACnC,OAAO,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC;qBAClD;oBAED,OAAO,QAAQ,CAAC;gBAClB,CAAC;gBACD,eAAe;gBACf,eAAe;oBACb,OAAO,iBAAiB,CAAC;gBAC3B,CAAC;gBACD,SAAS,CAAC,GAAG,EAAE,SAAS;oBACtB,OAAO,SAAS,CAAC,aAAa,CAAC,iBAAiB,EAAE,GAAG,EAAE,SAAS,CAAC,CAAC;gBACpE,CAAC;aACF,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAvDD,0CAuDC"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { LocalizationMessages, NestedLocalizationMessages } from './types';
|
|
1
|
+
import type { LocalizationMessages, NestedLocalizationMessages } from './types';
|
|
2
2
|
export declare function flattenTranslations(messages: LocalizationMessages | NestedLocalizationMessages): LocalizationMessages;
|
|
@@ -1,17 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.flattenTranslations = void 0;
|
|
4
|
-
function flattenTranslations(messages) {
|
|
5
|
-
return Object.fromEntries(Object
|
|
6
|
-
.entries(messages)
|
|
7
|
-
.map(([language, translations]) => {
|
|
8
|
-
return [
|
|
9
|
-
language,
|
|
10
|
-
flat(translations)
|
|
11
|
-
];
|
|
12
|
-
}));
|
|
13
|
-
}
|
|
14
|
-
exports.flattenTranslations = flattenTranslations;
|
|
15
4
|
function flat(source) {
|
|
16
5
|
const target = {};
|
|
17
6
|
flatten(source, target);
|
|
@@ -23,14 +12,16 @@ function flatten(source, target, prop = '') {
|
|
|
23
12
|
return;
|
|
24
13
|
}
|
|
25
14
|
if (typeof source === 'object' && source !== null) {
|
|
26
|
-
Object
|
|
27
|
-
|
|
28
|
-
.forEach((key) => {
|
|
29
|
-
flatten(source[key], target, prop
|
|
30
|
-
? `${prop}.${key}`
|
|
31
|
-
: key);
|
|
15
|
+
Object.keys(source).forEach((key) => {
|
|
16
|
+
flatten(source[key], target, prop ? `${prop}.${key}` : key);
|
|
32
17
|
});
|
|
33
18
|
return;
|
|
34
19
|
}
|
|
35
20
|
}
|
|
21
|
+
function flattenTranslations(messages) {
|
|
22
|
+
return Object.fromEntries(Object.entries(messages).map(([language, translations]) => {
|
|
23
|
+
return [language, flat(translations)];
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
exports.flattenTranslations = flattenTranslations;
|
|
36
27
|
//# sourceMappingURL=flatten-translations.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flatten-translations.js","sourceRoot":"","sources":["../src/flatten-translations.ts"],"names":[],"mappings":";;;AAEA,
|
|
1
|
+
{"version":3,"file":"flatten-translations.js","sourceRoot":"","sources":["../src/flatten-translations.ts"],"names":[],"mappings":";;;AAEA,SAAS,IAAI,CAAC,MAA+B;IAC3C,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACxB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,OAAO,CAAC,MAAW,EAAE,MAA8B,EAAE,IAAI,GAAG,EAAE;IACrE,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;QAC9B,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC;QAEtB,OAAO;KACR;IAED,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,EAAE;QACjD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAClC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAC9D,CAAC,CAAC,CAAC;QAEH,OAAO;KACR;AACH,CAAC;AAED,SAAgB,mBAAmB,CAAC,QAA2D;IAC7F,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,YAAY,CAAC,EAAE,EAAE;QACxD,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC;IACxC,CAAC,CAAC,CACH,CAAC;AACJ,CAAC;AAND,kDAMC"}
|
package/lib/types.d.ts
CHANGED
|
@@ -119,6 +119,7 @@ export interface NestedLocalizationMessages {
|
|
|
119
119
|
*/
|
|
120
120
|
[lang: string]: NestedTranslations;
|
|
121
121
|
}
|
|
122
|
+
export type AnyLocalizationMessages = LocalizationMessages | NestedLocalizationMessages;
|
|
122
123
|
export interface PiletLocaleApi {
|
|
123
124
|
/**
|
|
124
125
|
* Adds a list of translations to the existing translations.
|
|
@@ -129,7 +130,7 @@ export interface PiletLocaleApi {
|
|
|
129
130
|
* @param messagesList The list of messages that extend the existing translations
|
|
130
131
|
* @param [isOverriding=true] Indicates whether the new translations overwrite the existing translations
|
|
131
132
|
*/
|
|
132
|
-
addTranslations(messagesList:
|
|
133
|
+
addTranslations(messagesList: Array<AnyLocalizationMessages>, isOverriding?: boolean): void;
|
|
133
134
|
/**
|
|
134
135
|
* Gets the currently selected language directly.
|
|
135
136
|
*/
|
|
@@ -151,7 +152,7 @@ export interface PiletLocaleApi {
|
|
|
151
152
|
* The translations will be exclusively used for retrieving translations for the pilet.
|
|
152
153
|
* @param messages The messages to use as translation basis.
|
|
153
154
|
*/
|
|
154
|
-
setTranslations(messages:
|
|
155
|
+
setTranslations(messages: AnyLocalizationMessages): void;
|
|
155
156
|
/**
|
|
156
157
|
* Gets the currently provided translations by the pilet.
|
|
157
158
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-translate",
|
|
3
|
-
"version": "1.4.0-beta.
|
|
3
|
+
"version": "1.4.0-beta.6287",
|
|
4
4
|
"description": "Plugin for providing translated messages in Piral.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"piral",
|
|
@@ -68,8 +68,8 @@
|
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@types/deepmerge": "^2.2.0",
|
|
70
70
|
"@types/react": "^18.0.0",
|
|
71
|
-
"piral-core": "1.4.0-beta.
|
|
71
|
+
"piral-core": "1.4.0-beta.6287",
|
|
72
72
|
"react": "^18.0.0"
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "7aa3284b55970365eae54d79191975b6d0a05607"
|
|
75
75
|
}
|
package/piral-translate.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var piralTranslate=(()=>{var R=Object.create;var
|
|
1
|
+
var piralTranslate=(()=>{var R=Object.create;var p=Object.defineProperty;var G=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var $=Object.getPrototypeOf,I=Object.prototype.hasOwnProperty;var j=e=>p(e,"__esModule",{value:!0});var c=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(e,{get:(t,r)=>(typeof require!="undefined"?require:t)[r]}):e)(function(e){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+e+'" is not supported')});var F=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),U=(e,t)=>{j(e);for(var r in t)p(e,r,{get:t[r],enumerable:!0})},D=(e,t,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of N(t))!I.call(e,n)&&n!=="default"&&p(e,n,{get:()=>t[n],enumerable:!(r=G(t,n))||r.enumerable});return e},l=e=>D(j(p(e!=null?R($(e)):{},"default",e&&e.__esModule&&"default"in e?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e);var h=F((me,z)=>{"use strict";var B=function(t){return V(t)&&!K(t)};function V(e){return!!e&&typeof e=="object"}function K(e){var t=Object.prototype.toString.call(e);return t==="[object RegExp]"||t==="[object Date]"||q(e)}var Y=typeof Symbol=="function"&&Symbol.for,Z=Y?Symbol.for("react.element"):60103;function q(e){return e.$$typeof===Z}function H(e){return Array.isArray(e)?[]:{}}function u(e,t){return t.clone!==!1&&t.isMergeableObject(e)?g(H(e),e,t):e}function J(e,t,r){return e.concat(t).map(function(n){return u(n,r)})}function Q(e,t){if(!t.customMerge)return g;var r=t.customMerge(e);return typeof r=="function"?r:g}function W(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(function(t){return Object.propertyIsEnumerable.call(e,t)}):[]}function x(e){return Object.keys(e).concat(W(e))}function A(e,t){try{return t in e}catch{return!1}}function X(e,t){return A(e,t)&&!(Object.hasOwnProperty.call(e,t)&&Object.propertyIsEnumerable.call(e,t))}function ee(e,t,r){var n={};return r.isMergeableObject(e)&&x(e).forEach(function(a){n[a]=u(e[a],r)}),x(t).forEach(function(a){X(e,a)||(A(e,a)&&r.isMergeableObject(t[a])?n[a]=Q(a,r)(e[a],t[a],r):n[a]=u(t[a],r))}),n}function g(e,t,r){r=r||{},r.arrayMerge=r.arrayMerge||J,r.isMergeableObject=r.isMergeableObject||B,r.cloneUnlessOtherwiseSpecified=u;var n=Array.isArray(t),a=Array.isArray(e),s=n===a;return s?n?r.arrayMerge(e,t,r):ee(e,t,r):u(t,r)}g.all=function(t,r){if(!Array.isArray(t))throw new Error("first argument should be an array");return t.reduce(function(n,a){return g(n,a,r)},{})};var te=g;z.exports=te});var ue={};U(ue,{Languages:()=>ge,PiralLanguagesPicker:()=>M,createLocaleApi:()=>se,getUserLocale:()=>ie,setupLocalizer:()=>_,useDynamicLanguage:()=>le,useTranslate:()=>ce});var O=l(c("piral-core")),M=(0,O.getPiralComponent)("LanguagesPicker");var C=l(h());function P(e){return{selectLanguage(t,r){t.dispatch(n=>{e.language=r;let a=n.language.selected,s=r;return setTimeout(()=>{t.emit("select-language",{previousLanguage:a,currentLanguage:s})},0),{...n,language:{...n.language,loading:r===void 0,selected:r}}})},translate(t,r,n){return e&&e.localizeGlobal(r,n)},setTranslations(t,r,n){e.messages[r]=n.global;for(let a of n.locals){let s=t.apis[a.name];if(s){let o=s.getTranslations();o[r]=a.value,s.setTranslations(o)}}},getTranslations(t,r){return{global:e.messages[r],locals:Object.keys(t.apis).map(n=>({name:n,value:t.apis[n].getTranslations()[r]}))}}}}function re(e){let t={};return S(e,t),t}function S(e,t,r=""){if(typeof e=="string"){t[r]=e;return}if(typeof e=="object"&&e!==null){Object.keys(e).forEach(n=>{S(e[n],t,r?`${r}.${n}`:n)});return}}function m(e){return Object.fromEntries(Object.entries(e).map(([t,r])=>[t,re(r)]))}function ne(e,t){return t?"...":""}function ae(e,t){return e.replace(/{{\s*([A-Za-z0-9_.]+)\s*}}/g,(r,n)=>n in t?t[n]||"":`{{${n}}}`)}var T=class{constructor(t,r,n,a=ne){this.language=r;this.languages=n;this.fallback=a;this.messages=m(t)}localizeGlobal(t,r){return this.localizeBase(t,r)}localizeLocal(t,r,n){let a=this.translateMessage(t,r,n);return a===void 0?this.localizeBase(r,n):a}localizeBase(t,r){let n=this.translateMessage(this.messages,t,r);return n===void 0?this.fallback(t,this.language):n}translateMessage(t,r,n){let a=this.language,s=a&&t[a],o=s&&s[r];return o&&(n?ae(o,n):o)}};var v=l(c("piral-core")),E=e=>(0,v.defaultRender)(void 0);function _(e={}){let t=e.messages||{},r=Object.keys(t),n=r[0]||"en",a=e.language,o=(typeof a=="function"?a(r,n,"en"):a)||n;return new T(t,o,r.length?r:[o],e.fallback)}function se(e=_()){return t=>(t.defineActions(P(e)),t.dispatch(r=>({...r,components:{LanguagesPicker:E,...r.components},language:{loading:!1,available:e.languages,selected:e.language}})),r=>{let n={},a=s=>{n=m(s)};return{addTranslations(s,o=!0){let i=e.messages;a(C.all(o?[i,...s]:[...s,i]))},getCurrentLanguage(s){let o=t.readState(i=>i.language.selected);if(s){s(o);let i=L=>{s(L.currentLanguage)};return r.on("select-language",i),()=>r.off("select-language",i)}return o},setTranslations:a,getTranslations(){return n},translate(s,o){return e.localizeLocal(n,s,o)}}})}var b=l(c("piral-core"));function oe(e,t){let r=b.cookie.getItem("_culture")||b.storage.getItem("locale");return r||(t?t.toLowerCase().substring(0,2):navigator.language?navigator.language.substring(0,2):e)}function ie(e,t,r){let n=oe(t,r||"");return e.indexOf(n)!==-1?n:t}var d=l(c("react")),f=l(c("piral-core"));function le(e,t){let[r,n]=(0,d.useState)(e),{selectLanguage:a,setTranslations:s,getTranslations:o}=(0,f.useActions)();return(0,d.useEffect)(()=>{let i=!0,L=o(r);return a(void 0),t(r,L).then(y=>{i&&(s(r,y),a(r))},y=>console.error(y)),()=>{i=!1}},[r]),[r,n]}function ce(){let{translate:e}=(0,f.useActions)();return(0,f.useGlobalState)(t=>t.language.selected),e}var k=l(c("react")),w=l(c("piral-core"));var ge=()=>{let{available:e,selected:t}=(0,w.useGlobalState)(r=>r.language);return k.createElement(M,{selected:t,available:e})};return ue;})();
|
package/src/create.ts
CHANGED
|
@@ -4,8 +4,14 @@ import type { PiralPlugin } from 'piral-core';
|
|
|
4
4
|
import { createActions } from './actions';
|
|
5
5
|
import { Localizer } from './localize';
|
|
6
6
|
import { DefaultPicker } from './default';
|
|
7
|
-
import { PiletLocaleApi, LocalizationMessages, Localizable, PiralSelectLanguageEvent, NestedLocalizationMessages } from './types';
|
|
8
7
|
import { flattenTranslations } from './flatten-translations';
|
|
8
|
+
import type {
|
|
9
|
+
PiletLocaleApi,
|
|
10
|
+
LocalizationMessages,
|
|
11
|
+
Localizable,
|
|
12
|
+
PiralSelectLanguageEvent,
|
|
13
|
+
AnyLocalizationMessages,
|
|
14
|
+
} from './types';
|
|
9
15
|
|
|
10
16
|
export interface TranslationFallback {
|
|
11
17
|
(key: string, language: string): string;
|
|
@@ -23,7 +29,7 @@ export interface LocaleConfig {
|
|
|
23
29
|
* Sets the default (global) localization messages.
|
|
24
30
|
* @default {}
|
|
25
31
|
*/
|
|
26
|
-
messages?:
|
|
32
|
+
messages?: AnyLocalizationMessages;
|
|
27
33
|
/**
|
|
28
34
|
* Sets the default language to use.
|
|
29
35
|
*/
|
|
@@ -72,18 +78,15 @@ export function createLocaleApi(localizer: Localizable = setupLocalizer()): Pira
|
|
|
72
78
|
return (api) => {
|
|
73
79
|
let localTranslations: LocalizationMessages = {};
|
|
74
80
|
|
|
81
|
+
const setTranslations = (messages: AnyLocalizationMessages) => {
|
|
82
|
+
localTranslations = flattenTranslations(messages);
|
|
83
|
+
};
|
|
84
|
+
|
|
75
85
|
return {
|
|
76
86
|
addTranslations(messages, isOverriding = true) {
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
messagesToMerge.unshift(localizer.messages);
|
|
81
|
-
} else {
|
|
82
|
-
messagesToMerge.push(localizer.messages);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this.setTranslations(
|
|
86
|
-
deepmerge.all(messagesToMerge),
|
|
87
|
+
const current = localizer.messages;
|
|
88
|
+
setTranslations(
|
|
89
|
+
deepmerge.all<AnyLocalizationMessages>(isOverriding ? [current, ...messages] : [...messages, current]),
|
|
87
90
|
);
|
|
88
91
|
},
|
|
89
92
|
getCurrentLanguage(cb?: (l: string) => void): any {
|
|
@@ -100,9 +103,7 @@ export function createLocaleApi(localizer: Localizable = setupLocalizer()): Pira
|
|
|
100
103
|
|
|
101
104
|
return selected;
|
|
102
105
|
},
|
|
103
|
-
setTranslations
|
|
104
|
-
localTranslations = flattenTranslations(messages);
|
|
105
|
-
},
|
|
106
|
+
setTranslations,
|
|
106
107
|
getTranslations() {
|
|
107
108
|
return localTranslations;
|
|
108
109
|
},
|
|
@@ -1,23 +1,8 @@
|
|
|
1
|
-
import { LocalizationMessages, NestedLocalizationMessages } from './types';
|
|
2
|
-
|
|
3
|
-
export function flattenTranslations(messages: LocalizationMessages | NestedLocalizationMessages): LocalizationMessages {
|
|
4
|
-
return Object.fromEntries(
|
|
5
|
-
Object
|
|
6
|
-
.entries(messages)
|
|
7
|
-
.map(([ language, translations ]) => {
|
|
8
|
-
return [
|
|
9
|
-
language,
|
|
10
|
-
flat(translations)
|
|
11
|
-
]
|
|
12
|
-
})
|
|
13
|
-
);
|
|
14
|
-
}
|
|
1
|
+
import type { LocalizationMessages, NestedLocalizationMessages } from './types';
|
|
15
2
|
|
|
16
3
|
function flat(source: Record<string, unknown>): Record<string, string> {
|
|
17
4
|
const target: Record<string, string> = {};
|
|
18
|
-
|
|
19
5
|
flatten(source, target);
|
|
20
|
-
|
|
21
6
|
return target;
|
|
22
7
|
}
|
|
23
8
|
|
|
@@ -29,18 +14,18 @@ function flatten(source: any, target: Record<string, string>, prop = '') {
|
|
|
29
14
|
}
|
|
30
15
|
|
|
31
16
|
if (typeof source === 'object' && source !== null) {
|
|
32
|
-
Object
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
flatten(
|
|
36
|
-
source[key],
|
|
37
|
-
target,
|
|
38
|
-
prop
|
|
39
|
-
? `${prop}.${key}`
|
|
40
|
-
: key
|
|
41
|
-
);
|
|
42
|
-
});
|
|
17
|
+
Object.keys(source).forEach((key) => {
|
|
18
|
+
flatten(source[key], target, prop ? `${prop}.${key}` : key);
|
|
19
|
+
});
|
|
43
20
|
|
|
44
21
|
return;
|
|
45
22
|
}
|
|
46
23
|
}
|
|
24
|
+
|
|
25
|
+
export function flattenTranslations(messages: LocalizationMessages | NestedLocalizationMessages): LocalizationMessages {
|
|
26
|
+
return Object.fromEntries(
|
|
27
|
+
Object.entries(messages).map(([language, translations]) => {
|
|
28
|
+
return [language, flat(translations)];
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -133,6 +133,8 @@ export interface NestedLocalizationMessages {
|
|
|
133
133
|
[lang: string]: NestedTranslations;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
export type AnyLocalizationMessages = LocalizationMessages | NestedLocalizationMessages;
|
|
137
|
+
|
|
136
138
|
export interface PiletLocaleApi {
|
|
137
139
|
/**
|
|
138
140
|
* Adds a list of translations to the existing translations.
|
|
@@ -143,7 +145,7 @@ export interface PiletLocaleApi {
|
|
|
143
145
|
* @param messagesList The list of messages that extend the existing translations
|
|
144
146
|
* @param [isOverriding=true] Indicates whether the new translations overwrite the existing translations
|
|
145
147
|
*/
|
|
146
|
-
addTranslations(messagesList:
|
|
148
|
+
addTranslations(messagesList: Array<AnyLocalizationMessages>, isOverriding?: boolean): void;
|
|
147
149
|
/**
|
|
148
150
|
* Gets the currently selected language directly.
|
|
149
151
|
*/
|
|
@@ -165,7 +167,7 @@ export interface PiletLocaleApi {
|
|
|
165
167
|
* The translations will be exclusively used for retrieving translations for the pilet.
|
|
166
168
|
* @param messages The messages to use as translation basis.
|
|
167
169
|
*/
|
|
168
|
-
setTranslations(messages:
|
|
170
|
+
setTranslations(messages: AnyLocalizationMessages): void;
|
|
169
171
|
/**
|
|
170
172
|
* Gets the currently provided translations by the pilet.
|
|
171
173
|
*/
|