@empathyco/x-archetype-utils 0.1.0-alpha.9 → 1.0.0-alpha.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/cjs/build/webpack/webpack.config.js +27 -25
- package/dist/cjs/i18n/i18n.plugin.js +139 -134
- package/dist/cjs/index.js +11 -7
- package/dist/esm/build/webpack/webpack.config.js +23 -22
- package/dist/esm/i18n/i18n.plugin.js +129 -127
- package/dist/esm/index.js +2 -4
- package/dist/home/home-template.html +2272 -0
- package/dist/types/__tests__/i18n.plugin.spec.d.ts +1 -0
- package/dist/types/build/webpack/webpack.config.d.ts +35 -35
- package/dist/types/i18n/i18n.plugin.d.ts +74 -74
- package/dist/types/i18n/i18n.types.d.ts +53 -53
- package/dist/types/index.d.ts +3 -3
- package/package.json +21 -15
- package/dist/cjs/build/webpack/webpack.config.js.map +0 -1
- package/dist/cjs/i18n/i18n.plugin.js.map +0 -1
- package/dist/cjs/i18n/i18n.types.js +0 -3
- package/dist/cjs/i18n/i18n.types.js.map +0 -1
- package/dist/cjs/index.js.map +0 -1
- package/dist/esm/build/webpack/webpack.config.js.map +0 -1
- package/dist/esm/i18n/i18n.plugin.js.map +0 -1
- package/dist/esm/i18n/i18n.types.js +0 -2
- package/dist/esm/i18n/i18n.types.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
|
@@ -1,131 +1,133 @@
|
|
|
1
1
|
import { isObject } from '@empathyco/x-utils';
|
|
2
2
|
import VueI18n from 'vue-i18n';
|
|
3
3
|
import { deepMerge } from '@empathyco/x-deep-merge';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
instance
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* @
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
: messages
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* I18n settings manager.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
class I18n {
|
|
11
|
+
/**
|
|
12
|
+
* Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The new {@link I18nOptions | i18n options}.
|
|
15
|
+
*/
|
|
16
|
+
constructor({ locale, messages, device, fallbackLocale }) {
|
|
17
|
+
this.locale = locale;
|
|
18
|
+
this.device = device;
|
|
19
|
+
this.messages = messages;
|
|
20
|
+
this.fallbackLocale = fallbackLocale;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new {@link I18n} instance.
|
|
24
|
+
*
|
|
25
|
+
* @param options - The new {@link I18nOptions | i18n options}.
|
|
26
|
+
*
|
|
27
|
+
* @returns The new instance.
|
|
28
|
+
*/
|
|
29
|
+
static async create(options) {
|
|
30
|
+
const instance = new I18n(options);
|
|
31
|
+
instance.currentMessages = await instance.getCurrentMessages();
|
|
32
|
+
return instance;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Installs VueI18n plugin.
|
|
36
|
+
*
|
|
37
|
+
* @param vue - The Vue instance.
|
|
38
|
+
*/
|
|
39
|
+
install(vue) {
|
|
40
|
+
vue.use(VueI18n);
|
|
41
|
+
this.vueI18n = new VueI18n({
|
|
42
|
+
locale: this.locale,
|
|
43
|
+
messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
|
|
44
|
+
silentFallbackWarn: true,
|
|
45
|
+
missing: (locale, key) => {
|
|
46
|
+
var _a;
|
|
47
|
+
return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${locale}'`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Tries to get a value from the messages object accessing it key by key.
|
|
53
|
+
* When a key fails, takes the remaining key parts and tries to access the message
|
|
54
|
+
* joining them into a single key.
|
|
55
|
+
*
|
|
56
|
+
* @param path - A dot separated key to access the message object.
|
|
57
|
+
* @returns The message corresponding to the provided key, or empty string if it doesn't exist.
|
|
58
|
+
*/
|
|
59
|
+
getMessageWithDotsInKey(path) {
|
|
60
|
+
// TO-DO Support function or array messages
|
|
61
|
+
const message = path
|
|
62
|
+
.split('.')
|
|
63
|
+
.reduce((messages, key, index, pathParts) => {
|
|
64
|
+
var _a;
|
|
65
|
+
return isObject(messages)
|
|
66
|
+
? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
|
|
67
|
+
: messages;
|
|
68
|
+
}, this.currentMessages);
|
|
69
|
+
return typeof message === 'string' ? message : null;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Sets the new locale and updates the messages accordingly.
|
|
73
|
+
*
|
|
74
|
+
* @param newLocale - The new locale.
|
|
75
|
+
*
|
|
76
|
+
* @returns The new messages.
|
|
77
|
+
*/
|
|
78
|
+
async setLocale(newLocale) {
|
|
79
|
+
if (this.locale !== newLocale) {
|
|
80
|
+
this.locale = newLocale;
|
|
81
|
+
await this.changeMessages();
|
|
82
|
+
this.vueI18n.locale = this.locale;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Sets the new device and updates the messages accordingly.
|
|
87
|
+
*
|
|
88
|
+
* @param newDevice - The new device.
|
|
89
|
+
*
|
|
90
|
+
* @returns The new messages.
|
|
91
|
+
*/
|
|
92
|
+
async setDevice(newDevice) {
|
|
93
|
+
if (this.device !== newDevice) {
|
|
94
|
+
this.device = newDevice;
|
|
95
|
+
await this.changeMessages();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Updates the current messages based on the new locale and/or device.
|
|
100
|
+
*/
|
|
101
|
+
async changeMessages() {
|
|
102
|
+
this.currentMessages = await this.getCurrentMessages();
|
|
103
|
+
this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Retrieves the corresponding messages for the set locale and device.
|
|
107
|
+
*
|
|
108
|
+
* @remarks By default, it merges `base` messages with the specific `device` ones. If there are
|
|
109
|
+
* no messages for the current locale, the fallback locale is used instead.
|
|
110
|
+
*
|
|
111
|
+
* @returns The messages for the current locale and device.
|
|
112
|
+
*/
|
|
113
|
+
async getCurrentMessages() {
|
|
114
|
+
const rawMessages = this.locale in this.messages
|
|
115
|
+
? this.messages[this.locale]
|
|
116
|
+
: this.messages[this.fallbackLocale];
|
|
117
|
+
const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
119
|
+
return deepMerge({}, messages.base, messages[this.device]);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Type-guard to check if the passed messages should be lazy-loaded.
|
|
124
|
+
*
|
|
125
|
+
* @param messages - The messages to check.
|
|
126
|
+
*
|
|
127
|
+
* @returns True if the messages are lazy or false otherwise.
|
|
128
|
+
*/
|
|
129
|
+
function areLazyMessages(messages) {
|
|
130
|
+
return typeof messages === 'function';
|
|
120
131
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
*
|
|
124
|
-
* @param messages - The messages to check.
|
|
125
|
-
*
|
|
126
|
-
* @returns True if the messages are lazy or false otherwise.
|
|
127
|
-
*/
|
|
128
|
-
function areLazyMessages(messages) {
|
|
129
|
-
return typeof messages === 'function';
|
|
130
|
-
}
|
|
131
|
-
//# sourceMappingURL=i18n.plugin.js.map
|
|
132
|
+
|
|
133
|
+
export { I18n };
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export * from './build/webpack/webpack.config';
|
|
4
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export { I18n } from './i18n/i18n.plugin.js';
|
|
2
|
+
export { webpackConfig } from './build/webpack/webpack.config.js';
|