@empathyco/x-archetype-utils 0.1.0-alpha.0 → 0.1.0-alpha.10
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 +26 -0
- package/dist/cjs/build/webpack/webpack.config.js.map +1 -0
- package/dist/cjs/i18n/i18n.plugin.js +136 -0
- package/dist/cjs/i18n/i18n.plugin.js.map +1 -0
- package/dist/cjs/i18n/i18n.types.js +3 -0
- package/dist/cjs/i18n/i18n.types.js.map +1 -0
- package/dist/cjs/index.js +4 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/build/webpack/webpack.config.js +23 -0
- package/dist/esm/build/webpack/webpack.config.js.map +1 -0
- package/dist/esm/i18n/i18n.plugin.js +131 -0
- package/dist/esm/i18n/i18n.plugin.js.map +1 -0
- package/dist/esm/i18n/i18n.types.js +2 -0
- package/dist/esm/i18n/i18n.types.js.map +1 -0
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/types/build/webpack/webpack.config.d.ts +35 -0
- package/dist/types/i18n/i18n.plugin.d.ts +74 -0
- package/dist/types/i18n/i18n.types.d.ts +53 -0
- package/dist/types/index.d.ts +3 -1
- package/package.json +16 -5
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.webpackConfig = void 0;
|
|
4
|
+
exports.webpackConfig = {
|
|
5
|
+
transpileDependencies: ['@empathyco/x-components'],
|
|
6
|
+
configureWebpack: {
|
|
7
|
+
module: {
|
|
8
|
+
rules: [
|
|
9
|
+
{
|
|
10
|
+
test: /\.ts|js|vue$/,
|
|
11
|
+
enforce: 'pre',
|
|
12
|
+
use: ['source-map-loader']
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
output: {
|
|
17
|
+
devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }) => resourcePath.match(/\.vue$/) &&
|
|
18
|
+
!query.match(/type=script/) &&
|
|
19
|
+
!query.match(/\?rollup-plugin-vue=script.ts/)
|
|
20
|
+
? `webpack-generated:///${resourcePath}?${hash}`
|
|
21
|
+
: `sources://${resourcePath}`,
|
|
22
|
+
devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]'
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=webpack.config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpack.config.js","sourceRoot":"","sources":["../../../../src/build/webpack/webpack.config.ts"],"names":[],"mappings":";;;AAmBa,QAAA,aAAa,GAAG;IAC3B,qBAAqB,EAAE,CAAC,yBAAyB,CAAC;IAClD,gBAAgB,EAAE;QAChB,MAAM,EAAE;YACN,KAAK,EAAE;gBACL;oBACE,IAAI,EAAE,cAAc;oBACpB,OAAO,EAAE,KAAK;oBACd,GAAG,EAAE,CAAC,mBAAmB,CAAC;iBAC3B;aACF;SACF;QACD,MAAM,EAAE;YACN,6BAA6B,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAA+B,EAAE,EAAE,CAC5F,YAAY,CAAC,KAAK,CAAC,QAAQ,CAAC;gBAC5B,CAAC,KAAK,CAAC,KAAK,CAAC,aAAa,CAAC;gBAC3B,CAAC,KAAK,CAAC,KAAK,CAAC,+BAA+B,CAAC;gBAC3C,CAAC,CAAC,wBAAwB,YAAY,IAAI,IAAI,EAAE;gBAChD,CAAC,CAAC,aAAa,YAAY,EAAE;YACjC,qCAAqC,EAAE,mCAAmC;SAC3E;KACF;CACF,CAAC","sourcesContent":["/**.\n * Self-made interface since Webpack does not provide a type for this object.\n * Https://github.com/webpack/webpack/blob/main/declarations/WebpackOptions.d.ts#L463\n *\n * @internal\n */\ninterface webpackFilenameTemplateInfo {\n identifier: string;\n shortIdentifier: string;\n resource: string;\n resourcePath: string;\n absoluteResourcePath: string;\n allLoaders: string;\n query: string;\n moduleId: string;\n hash: string;\n namespace: string;\n}\n\nexport const webpackConfig = {\n transpileDependencies: ['@empathyco/x-components'],\n configureWebpack: {\n module: {\n rules: [\n {\n test: /\\.ts|js|vue$/,\n enforce: 'pre',\n use: ['source-map-loader']\n }\n ]\n },\n output: {\n devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }: webpackFilenameTemplateInfo) =>\n resourcePath.match(/\\.vue$/) &&\n !query.match(/type=script/) &&\n !query.match(/\\?rollup-plugin-vue=script.ts/)\n ? `webpack-generated:///${resourcePath}?${hash}`\n : `sources://${resourcePath}`,\n devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]'\n }\n }\n};\n"]}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.I18n = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const x_utils_1 = require("@empathyco/x-utils");
|
|
6
|
+
const vue_i18n_1 = tslib_1.__importDefault(require("vue-i18n"));
|
|
7
|
+
const x_deep_merge_1 = require("@empathyco/x-deep-merge");
|
|
8
|
+
/**
|
|
9
|
+
* I18n settings manager.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
class I18n {
|
|
14
|
+
/**
|
|
15
|
+
* Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
|
|
16
|
+
*
|
|
17
|
+
* @param options - The new {@link I18nOptions | i18n options}.
|
|
18
|
+
*/
|
|
19
|
+
constructor({ locale, messages, device, fallbackLocale }) {
|
|
20
|
+
this.locale = locale;
|
|
21
|
+
this.device = device;
|
|
22
|
+
this.messages = messages;
|
|
23
|
+
this.fallbackLocale = fallbackLocale;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new {@link I18n} instance.
|
|
27
|
+
*
|
|
28
|
+
* @param options - The new {@link I18nOptions | i18n options}.
|
|
29
|
+
*
|
|
30
|
+
* @returns The new instance.
|
|
31
|
+
*/
|
|
32
|
+
static async create(options) {
|
|
33
|
+
const instance = new I18n(options);
|
|
34
|
+
instance.currentMessages = await instance.getCurrentMessages();
|
|
35
|
+
return instance;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Installs VueI18n plugin.
|
|
39
|
+
*
|
|
40
|
+
* @param vue - The Vue instance.
|
|
41
|
+
*/
|
|
42
|
+
install(vue) {
|
|
43
|
+
vue.use(vue_i18n_1.default);
|
|
44
|
+
this.vueI18n = new vue_i18n_1.default({
|
|
45
|
+
locale: this.locale,
|
|
46
|
+
messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
|
|
47
|
+
silentFallbackWarn: true,
|
|
48
|
+
missing: (locale, key) => {
|
|
49
|
+
var _a;
|
|
50
|
+
return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${locale}'`);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Tries to get a value from the messages object accessing it key by key.
|
|
56
|
+
* When a key fails, takes the remaining key parts and tries to access the message
|
|
57
|
+
* joining them into a single key.
|
|
58
|
+
*
|
|
59
|
+
* @param path - A dot separated key to access the message object.
|
|
60
|
+
* @returns The message corresponding to the provided key, or empty string if it doesn't exist.
|
|
61
|
+
*/
|
|
62
|
+
getMessageWithDotsInKey(path) {
|
|
63
|
+
// TO-DO Support function or array messages
|
|
64
|
+
const message = path
|
|
65
|
+
.split('.')
|
|
66
|
+
.reduce((messages, key, index, pathParts) => {
|
|
67
|
+
var _a;
|
|
68
|
+
return (0, x_utils_1.isObject)(messages)
|
|
69
|
+
? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
|
|
70
|
+
: messages;
|
|
71
|
+
}, this.currentMessages);
|
|
72
|
+
return typeof message === 'string' ? message : null;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Sets the new locale and updates the messages accordingly.
|
|
76
|
+
*
|
|
77
|
+
* @param newLocale - The new locale.
|
|
78
|
+
*
|
|
79
|
+
* @returns The new messages.
|
|
80
|
+
*/
|
|
81
|
+
async setLocale(newLocale) {
|
|
82
|
+
if (this.locale !== newLocale) {
|
|
83
|
+
this.locale = newLocale;
|
|
84
|
+
await this.changeMessages();
|
|
85
|
+
this.vueI18n.locale = this.locale;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Sets the new device and updates the messages accordingly.
|
|
90
|
+
*
|
|
91
|
+
* @param newDevice - The new device.
|
|
92
|
+
*
|
|
93
|
+
* @returns The new messages.
|
|
94
|
+
*/
|
|
95
|
+
async setDevice(newDevice) {
|
|
96
|
+
if (this.device !== newDevice) {
|
|
97
|
+
this.device = newDevice;
|
|
98
|
+
await this.changeMessages();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Updates the current messages based on the new locale and/or device.
|
|
103
|
+
*/
|
|
104
|
+
async changeMessages() {
|
|
105
|
+
this.currentMessages = await this.getCurrentMessages();
|
|
106
|
+
this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Retrieves the corresponding messages for the set locale and device.
|
|
110
|
+
*
|
|
111
|
+
* @remarks By default, it merges `base` messages with the specific `device` ones. If there are
|
|
112
|
+
* no messages for the current locale, the fallback locale is used instead.
|
|
113
|
+
*
|
|
114
|
+
* @returns The messages for the current locale and device.
|
|
115
|
+
*/
|
|
116
|
+
async getCurrentMessages() {
|
|
117
|
+
const rawMessages = this.locale in this.messages
|
|
118
|
+
? this.messages[this.locale]
|
|
119
|
+
: this.messages[this.fallbackLocale];
|
|
120
|
+
const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
122
|
+
return (0, x_deep_merge_1.deepMerge)({}, messages.base, messages[this.device]);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.I18n = I18n;
|
|
126
|
+
/**
|
|
127
|
+
* Type-guard to check if the passed messages should be lazy-loaded.
|
|
128
|
+
*
|
|
129
|
+
* @param messages - The messages to check.
|
|
130
|
+
*
|
|
131
|
+
* @returns True if the messages are lazy or false otherwise.
|
|
132
|
+
*/
|
|
133
|
+
function areLazyMessages(messages) {
|
|
134
|
+
return typeof messages === 'function';
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=i18n.plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.plugin.js","sourceRoot":"","sources":["../../../src/i18n/i18n.plugin.ts"],"names":[],"mappings":";;;;AAAA,gDAA8C;AAE9C,gEAAuE;AACvE,0DAAoD;AAGpD;;;;GAIG;AACH,MAAa,IAAI;IAQf;;;;OAIG;IACH,YAAmB,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,cAAc,EAA6B;QACxF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,CACjB,OAAkC;QAElC,MAAM,QAAQ,GAAG,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC;QACnC,QAAQ,CAAC,eAAe,GAAG,MAAM,QAAQ,CAAC,kBAAkB,EAAE,CAAC;QAC/D,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED;;;;OAIG;IACH,OAAO,CAAC,GAAmB;QACzB,GAAG,CAAC,GAAG,CAAC,kBAAO,CAAC,CAAC;QACjB,IAAI,CAAC,OAAO,GAAG,IAAI,kBAAO,CAA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AM,CAAC;SACnC;IACH,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,SAAS,CAAC,SAAiB;QAC/B,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,EAAE;YAC7B,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;YAExB,MAAM,IAAI,CAAC,cAAc,EAAE,CAAC;SAC7B;IACH,CAAC;IAED;;OAEG;IACO,KAAK,CAAC,cAAc;QAC5B,IAAI,CAAC,eAAe,GAAG,MAAM,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAEvD,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;IACnE,CAAC;IAED;;;;;;;OAOG;IACO,KAAK,CAAC,kBAAkB;QAChC,MAAM,WAAW,GACf,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,QAAQ;YAC1B,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC;YAC5B,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAEzC,MAAM,QAAQ,GAAG,eAAe,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,WAAW,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC;QAC5F,6DAA6D;QAC7D,OAAO,IAAA,wBAAS,EAAC,EAAE,EAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7D,CAAC;CACF;AAvID,oBAuIC;AAED;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,QAAmC;IAEnC,OAAO,OAAO,QAAQ,KAAK,UAAU,CAAC;AACxC,CAAC","sourcesContent":["import { isObject } from '@empathyco/x-utils';\nimport { VueConstructor } from 'vue';\nimport VueI18n, { LocaleMessageObject, LocaleMessage } from 'vue-i18n';\nimport { deepMerge } from '@empathyco/x-deep-merge';\nimport { AnyMessages, Device, I18nOptions, LoadLazyMessagesByDevice, Locale } from './i18n.types';\n\n/**\n * I18n settings manager.\n *\n * @public\n */\nexport class I18n<SomeMessages> {\n public vueI18n!: VueI18n;\n protected locale!: Locale;\n protected device!: Device;\n protected messages!: Record<Locale, AnyMessages<SomeMessages>>;\n protected currentMessages!: LocaleMessageObject;\n protected fallbackLocale!: Locale;\n\n /**\n * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.\n *\n * @param options - The new {@link I18nOptions | i18n options}.\n */\n public constructor({ locale, messages, device, fallbackLocale }: I18nOptions<SomeMessages>) {\n this.locale = locale;\n this.device = device;\n this.messages = messages;\n this.fallbackLocale = fallbackLocale;\n }\n\n /**\n * Creates a new {@link I18n} instance.\n *\n * @param options - The new {@link I18nOptions | i18n options}.\n *\n * @returns The new instance.\n */\n static async create<SomeMessages>(\n options: I18nOptions<SomeMessages>\n ): Promise<I18n<SomeMessages>> {\n const instance = new I18n(options);\n instance.currentMessages = await instance.getCurrentMessages();\n return instance;\n }\n\n /**\n * Installs VueI18n plugin.\n *\n * @param vue - The Vue instance.\n */\n install(vue: VueConstructor): void {\n vue.use(VueI18n);\n this.vueI18n = new VueI18n({\n locale: this.locale,\n messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},\n silentFallbackWarn: true,\n missing: (locale, key) => {\n return (\n this.getMessageWithDotsInKey(key) ??\n `[i18n] Key '${key}' is missing for locale: '${locale}'`\n );\n }\n });\n }\n\n /**\n * Tries to get a value from the messages object accessing it key by key.\n * When a key fails, takes the remaining key parts and tries to access the message\n * joining them into a single key.\n *\n * @param path - A dot separated key to access the message object.\n * @returns The message corresponding to the provided key, or empty string if it doesn't exist.\n */\n protected getMessageWithDotsInKey(path: string): string | null {\n // TO-DO Support function or array messages\n const message = path\n .split('.')\n .reduce<LocaleMessage>(\n (messages, key, index, pathParts) =>\n isObject(messages)\n ? messages[key] ?? messages[pathParts.slice(index).join('.')]\n : messages,\n this.currentMessages\n );\n return typeof message === 'string' ? message : null;\n }\n\n /**\n * Sets the new locale and updates the messages accordingly.\n *\n * @param newLocale - The new locale.\n *\n * @returns The new messages.\n */\n async setLocale(newLocale: Locale): Promise<void> {\n if (this.locale !== newLocale) {\n this.locale = newLocale;\n\n await this.changeMessages();\n this.vueI18n.locale = this.locale;\n }\n }\n\n /**\n * Sets the new device and updates the messages accordingly.\n *\n * @param newDevice - The new device.\n *\n * @returns The new messages.\n */\n async setDevice(newDevice: Device): Promise<void> {\n if (this.device !== newDevice) {\n this.device = newDevice;\n\n await this.changeMessages();\n }\n }\n\n /**\n * Updates the current messages based on the new locale and/or device.\n */\n protected async changeMessages(): Promise<void> {\n this.currentMessages = await this.getCurrentMessages();\n\n this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);\n }\n\n /**\n * Retrieves the corresponding messages for the set locale and device.\n *\n * @remarks By default, it merges `base` messages with the specific `device` ones. If there are\n * no messages for the current locale, the fallback locale is used instead.\n *\n * @returns The messages for the current locale and device.\n */\n protected async getCurrentMessages(): Promise<LocaleMessageObject> {\n const rawMessages =\n this.locale in this.messages\n ? this.messages[this.locale]\n : this.messages[this.fallbackLocale];\n\n const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n return deepMerge({}, messages.base, messages[this.device]);\n }\n}\n\n/**\n * Type-guard to check if the passed messages should be lazy-loaded.\n *\n * @param messages - The messages to check.\n *\n * @returns True if the messages are lazy or false otherwise.\n */\nfunction areLazyMessages<SomeMessages>(\n messages: AnyMessages<SomeMessages>\n): messages is LoadLazyMessagesByDevice<SomeMessages> {\n return typeof messages === 'function';\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.types.js","sourceRoot":"","sources":["../../../src/i18n/i18n.types.ts"],"names":[],"mappings":"","sourcesContent":["import VueI18n from 'vue-i18n';\nimport { DeepPartial } from '@empathyco/x-utils';\n\n/** Supported locales. */\nexport type Locale = string;\n\n/** Supported devices. */\nexport type Device = string;\n\n/** Union type containing both eager and lazy messages. */\nexport type AnyMessages<SomeMessages> =\n | MessagesByDevice<SomeMessages>\n | LoadLazyMessagesByDevice<SomeMessages>;\n\n/** A function that loads on demand the messages for a specific locale. */\nexport type LoadLazyMessagesByDevice<SomeMessages> = () => Promise<{\n default: MessagesByDevice<SomeMessages>;\n}>;\n\n/**\n * An object containing a base property with all the existing messages and the overridden ones for\n * each device.\n */\nexport interface MessagesByDevice<SomeMessages> {\n base: SomeMessages;\n [device: Device]: DeepPartial<SomeMessages>;\n}\n\n/**\n * I18n settings.\n */\nexport interface I18nOptions<SomeMessages> {\n /** The initial messages. */\n messages: Record<Locale, AnyMessages<SomeMessages>>;\n /** The initial device. */\n device: Device;\n /** The locale to fall back if no matching locale is available. */\n fallbackLocale: Locale;\n /** The initial locale. */\n locale: Locale;\n}\n\n/**\n * The VueI18n instance that should be passed to the root Vue instance and some runtime utility\n * functions.\n */\nexport interface I18nAPI {\n /** The Vue I18n instance that should be passed to the root Vue component. */\n readonly vueI18n: Readonly<VueI18n>;\n /**\n * Sets the new locale.\n *\n * @param newLocale - The new locale.\n */\n setLocale: (newLocale: Locale) => Promise<void>;\n /**\n * Sets the new device.\n *\n * @param newDevice - The new device.\n */\n setDevice: (newDevice: Device) => Promise<void>;\n}\n"]}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./i18n/i18n.plugin"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./i18n/i18n.types"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./build/webpack/webpack.config"), exports);
|
|
5
7
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAmC;AACnC,4DAAkC;AAClC,yEAA+C","sourcesContent":["export * from './i18n/i18n.plugin';\nexport * from './i18n/i18n.types';\nexport * from './build/webpack/webpack.config';\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const webpackConfig = {
|
|
2
|
+
transpileDependencies: ['@empathyco/x-components'],
|
|
3
|
+
configureWebpack: {
|
|
4
|
+
module: {
|
|
5
|
+
rules: [
|
|
6
|
+
{
|
|
7
|
+
test: /\.ts|js|vue$/,
|
|
8
|
+
enforce: 'pre',
|
|
9
|
+
use: ['source-map-loader']
|
|
10
|
+
}
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
output: {
|
|
14
|
+
devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }) => resourcePath.match(/\.vue$/) &&
|
|
15
|
+
!query.match(/type=script/) &&
|
|
16
|
+
!query.match(/\?rollup-plugin-vue=script.ts/)
|
|
17
|
+
? `webpack-generated:///${resourcePath}?${hash}`
|
|
18
|
+
: `sources://${resourcePath}`,
|
|
19
|
+
devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]'
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=webpack.config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webpack.config.js","sourceRoot":"","sources":["../../../../src/build/webpack/webpack.config.ts"],"names":[],"mappings":"AAmBA,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,qBAAqB,EAAE,CAAC,yBAAyB,CAAC;IAClD,gBAAgB,EAAE;QAChB,MAAM,EAAE;YACN,KAAK,EAAE;gBACL;oBACE,IAAI,EAAE,cAAc;oBACpB,OAAO,EAAE,KAAK;oBACd,GAAG,EAAE,CAAC,mBAAmB,CAAC;iBAC3B;aACF;SACF;QACD,MAAM,EAAE;YACN,6BAA6B,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAA+B,EAAE,EAAE,CAC5F,YAAY,CAAC,KAAK,CAAC,QAAQ,CAAC;gBAC5B,CAAC,KAAK,CAAC,KAAK,CAAC,aAAa,CAAC;gBAC3B,CAAC,KAAK,CAAC,KAAK,CAAC,+BAA+B,CAAC;gBAC3C,CAAC,CAAC,wBAAwB,YAAY,IAAI,IAAI,EAAE;gBAChD,CAAC,CAAC,aAAa,YAAY,EAAE;YACjC,qCAAqC,EAAE,mCAAmC;SAC3E;KACF;CACF,CAAC","sourcesContent":["/**.\n * Self-made interface since Webpack does not provide a type for this object.\n * Https://github.com/webpack/webpack/blob/main/declarations/WebpackOptions.d.ts#L463\n *\n * @internal\n */\ninterface webpackFilenameTemplateInfo {\n identifier: string;\n shortIdentifier: string;\n resource: string;\n resourcePath: string;\n absoluteResourcePath: string;\n allLoaders: string;\n query: string;\n moduleId: string;\n hash: string;\n namespace: string;\n}\n\nexport const webpackConfig = {\n transpileDependencies: ['@empathyco/x-components'],\n configureWebpack: {\n module: {\n rules: [\n {\n test: /\\.ts|js|vue$/,\n enforce: 'pre',\n use: ['source-map-loader']\n }\n ]\n },\n output: {\n devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }: webpackFilenameTemplateInfo) =>\n resourcePath.match(/\\.vue$/) &&\n !query.match(/type=script/) &&\n !query.match(/\\?rollup-plugin-vue=script.ts/)\n ? `webpack-generated:///${resourcePath}?${hash}`\n : `sources://${resourcePath}`,\n devtoolFallbackModuleFilenameTemplate: 'webpack:///[resource-path]?[hash]'\n }\n }\n};\n"]}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { isObject } from '@empathyco/x-utils';
|
|
2
|
+
import VueI18n from 'vue-i18n';
|
|
3
|
+
import { deepMerge } from '@empathyco/x-deep-merge';
|
|
4
|
+
/**
|
|
5
|
+
* I18n settings manager.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export class I18n {
|
|
10
|
+
/**
|
|
11
|
+
* Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
|
|
12
|
+
*
|
|
13
|
+
* @param options - The new {@link I18nOptions | i18n options}.
|
|
14
|
+
*/
|
|
15
|
+
constructor({ locale, messages, device, fallbackLocale }) {
|
|
16
|
+
this.locale = locale;
|
|
17
|
+
this.device = device;
|
|
18
|
+
this.messages = messages;
|
|
19
|
+
this.fallbackLocale = fallbackLocale;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new {@link I18n} instance.
|
|
23
|
+
*
|
|
24
|
+
* @param options - The new {@link I18nOptions | i18n options}.
|
|
25
|
+
*
|
|
26
|
+
* @returns The new instance.
|
|
27
|
+
*/
|
|
28
|
+
static async create(options) {
|
|
29
|
+
const instance = new I18n(options);
|
|
30
|
+
instance.currentMessages = await instance.getCurrentMessages();
|
|
31
|
+
return instance;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Installs VueI18n plugin.
|
|
35
|
+
*
|
|
36
|
+
* @param vue - The Vue instance.
|
|
37
|
+
*/
|
|
38
|
+
install(vue) {
|
|
39
|
+
vue.use(VueI18n);
|
|
40
|
+
this.vueI18n = new VueI18n({
|
|
41
|
+
locale: this.locale,
|
|
42
|
+
messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},
|
|
43
|
+
silentFallbackWarn: true,
|
|
44
|
+
missing: (locale, key) => {
|
|
45
|
+
var _a;
|
|
46
|
+
return ((_a = this.getMessageWithDotsInKey(key)) !== null && _a !== void 0 ? _a : `[i18n] Key '${key}' is missing for locale: '${locale}'`);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Tries to get a value from the messages object accessing it key by key.
|
|
52
|
+
* When a key fails, takes the remaining key parts and tries to access the message
|
|
53
|
+
* joining them into a single key.
|
|
54
|
+
*
|
|
55
|
+
* @param path - A dot separated key to access the message object.
|
|
56
|
+
* @returns The message corresponding to the provided key, or empty string if it doesn't exist.
|
|
57
|
+
*/
|
|
58
|
+
getMessageWithDotsInKey(path) {
|
|
59
|
+
// TO-DO Support function or array messages
|
|
60
|
+
const message = path
|
|
61
|
+
.split('.')
|
|
62
|
+
.reduce((messages, key, index, pathParts) => {
|
|
63
|
+
var _a;
|
|
64
|
+
return isObject(messages)
|
|
65
|
+
? (_a = messages[key]) !== null && _a !== void 0 ? _a : messages[pathParts.slice(index).join('.')]
|
|
66
|
+
: messages;
|
|
67
|
+
}, this.currentMessages);
|
|
68
|
+
return typeof message === 'string' ? message : null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Sets the new locale and updates the messages accordingly.
|
|
72
|
+
*
|
|
73
|
+
* @param newLocale - The new locale.
|
|
74
|
+
*
|
|
75
|
+
* @returns The new messages.
|
|
76
|
+
*/
|
|
77
|
+
async setLocale(newLocale) {
|
|
78
|
+
if (this.locale !== newLocale) {
|
|
79
|
+
this.locale = newLocale;
|
|
80
|
+
await this.changeMessages();
|
|
81
|
+
this.vueI18n.locale = this.locale;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Sets the new device and updates the messages accordingly.
|
|
86
|
+
*
|
|
87
|
+
* @param newDevice - The new device.
|
|
88
|
+
*
|
|
89
|
+
* @returns The new messages.
|
|
90
|
+
*/
|
|
91
|
+
async setDevice(newDevice) {
|
|
92
|
+
if (this.device !== newDevice) {
|
|
93
|
+
this.device = newDevice;
|
|
94
|
+
await this.changeMessages();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Updates the current messages based on the new locale and/or device.
|
|
99
|
+
*/
|
|
100
|
+
async changeMessages() {
|
|
101
|
+
this.currentMessages = await this.getCurrentMessages();
|
|
102
|
+
this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Retrieves the corresponding messages for the set locale and device.
|
|
106
|
+
*
|
|
107
|
+
* @remarks By default, it merges `base` messages with the specific `device` ones. If there are
|
|
108
|
+
* no messages for the current locale, the fallback locale is used instead.
|
|
109
|
+
*
|
|
110
|
+
* @returns The messages for the current locale and device.
|
|
111
|
+
*/
|
|
112
|
+
async getCurrentMessages() {
|
|
113
|
+
const rawMessages = this.locale in this.messages
|
|
114
|
+
? this.messages[this.locale]
|
|
115
|
+
: this.messages[this.fallbackLocale];
|
|
116
|
+
const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
118
|
+
return deepMerge({}, messages.base, messages[this.device]);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Type-guard to check if the passed messages should be lazy-loaded.
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.plugin.js","sourceRoot":"","sources":["../../../src/i18n/i18n.plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C,OAAO,OAA+C,MAAM,UAAU,CAAC;AACvE,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGpD;;;;GAIG;AACH,MAAM,OAAO,IAAI;IAQf;;;;OAIG;IACH,YAAmB,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,cAAc,EAA6B;QACxF,IAAI,CAAC,MAA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AAM,QAAQ,GAAG,eAAe,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,WAAW,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC;QAC5F,6DAA6D;QAC7D,OAAO,SAAS,CAAC,EAAE,EAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC7D,CAAC;CACF;AAED;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,QAAmC;IAEnC,OAAO,OAAO,QAAQ,KAAK,UAAU,CAAC;AACxC,CAAC","sourcesContent":["import { isObject } from '@empathyco/x-utils';\nimport { VueConstructor } from 'vue';\nimport VueI18n, { LocaleMessageObject, LocaleMessage } from 'vue-i18n';\nimport { deepMerge } from '@empathyco/x-deep-merge';\nimport { AnyMessages, Device, I18nOptions, LoadLazyMessagesByDevice, Locale } from './i18n.types';\n\n/**\n * I18n settings manager.\n *\n * @public\n */\nexport class I18n<SomeMessages> {\n public vueI18n!: VueI18n;\n protected locale!: Locale;\n protected device!: Device;\n protected messages!: Record<Locale, AnyMessages<SomeMessages>>;\n protected currentMessages!: LocaleMessageObject;\n protected fallbackLocale!: Locale;\n\n /**\n * Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.\n *\n * @param options - The new {@link I18nOptions | i18n options}.\n */\n public constructor({ locale, messages, device, fallbackLocale }: I18nOptions<SomeMessages>) {\n this.locale = locale;\n this.device = device;\n this.messages = messages;\n this.fallbackLocale = fallbackLocale;\n }\n\n /**\n * Creates a new {@link I18n} instance.\n *\n * @param options - The new {@link I18nOptions | i18n options}.\n *\n * @returns The new instance.\n */\n static async create<SomeMessages>(\n options: I18nOptions<SomeMessages>\n ): Promise<I18n<SomeMessages>> {\n const instance = new I18n(options);\n instance.currentMessages = await instance.getCurrentMessages();\n return instance;\n }\n\n /**\n * Installs VueI18n plugin.\n *\n * @param vue - The Vue instance.\n */\n install(vue: VueConstructor): void {\n vue.use(VueI18n);\n this.vueI18n = new VueI18n({\n locale: this.locale,\n messages: this.currentMessages ? { [this.locale]: this.currentMessages } : {},\n silentFallbackWarn: true,\n missing: (locale, key) => {\n return (\n this.getMessageWithDotsInKey(key) ??\n `[i18n] Key '${key}' is missing for locale: '${locale}'`\n );\n }\n });\n }\n\n /**\n * Tries to get a value from the messages object accessing it key by key.\n * When a key fails, takes the remaining key parts and tries to access the message\n * joining them into a single key.\n *\n * @param path - A dot separated key to access the message object.\n * @returns The message corresponding to the provided key, or empty string if it doesn't exist.\n */\n protected getMessageWithDotsInKey(path: string): string | null {\n // TO-DO Support function or array messages\n const message = path\n .split('.')\n .reduce<LocaleMessage>(\n (messages, key, index, pathParts) =>\n isObject(messages)\n ? messages[key] ?? messages[pathParts.slice(index).join('.')]\n : messages,\n this.currentMessages\n );\n return typeof message === 'string' ? message : null;\n }\n\n /**\n * Sets the new locale and updates the messages accordingly.\n *\n * @param newLocale - The new locale.\n *\n * @returns The new messages.\n */\n async setLocale(newLocale: Locale): Promise<void> {\n if (this.locale !== newLocale) {\n this.locale = newLocale;\n\n await this.changeMessages();\n this.vueI18n.locale = this.locale;\n }\n }\n\n /**\n * Sets the new device and updates the messages accordingly.\n *\n * @param newDevice - The new device.\n *\n * @returns The new messages.\n */\n async setDevice(newDevice: Device): Promise<void> {\n if (this.device !== newDevice) {\n this.device = newDevice;\n\n await this.changeMessages();\n }\n }\n\n /**\n * Updates the current messages based on the new locale and/or device.\n */\n protected async changeMessages(): Promise<void> {\n this.currentMessages = await this.getCurrentMessages();\n\n this.vueI18n.setLocaleMessage(this.locale, this.currentMessages);\n }\n\n /**\n * Retrieves the corresponding messages for the set locale and device.\n *\n * @remarks By default, it merges `base` messages with the specific `device` ones. If there are\n * no messages for the current locale, the fallback locale is used instead.\n *\n * @returns The messages for the current locale and device.\n */\n protected async getCurrentMessages(): Promise<LocaleMessageObject> {\n const rawMessages =\n this.locale in this.messages\n ? this.messages[this.locale]\n : this.messages[this.fallbackLocale];\n\n const messages = areLazyMessages(rawMessages) ? (await rawMessages()).default : rawMessages;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-call\n return deepMerge({}, messages.base, messages[this.device]);\n }\n}\n\n/**\n * Type-guard to check if the passed messages should be lazy-loaded.\n *\n * @param messages - The messages to check.\n *\n * @returns True if the messages are lazy or false otherwise.\n */\nfunction areLazyMessages<SomeMessages>(\n messages: AnyMessages<SomeMessages>\n): messages is LoadLazyMessagesByDevice<SomeMessages> {\n return typeof messages === 'function';\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.types.js","sourceRoot":"","sources":["../../../src/i18n/i18n.types.ts"],"names":[],"mappings":"","sourcesContent":["import VueI18n from 'vue-i18n';\nimport { DeepPartial } from '@empathyco/x-utils';\n\n/** Supported locales. */\nexport type Locale = string;\n\n/** Supported devices. */\nexport type Device = string;\n\n/** Union type containing both eager and lazy messages. */\nexport type AnyMessages<SomeMessages> =\n | MessagesByDevice<SomeMessages>\n | LoadLazyMessagesByDevice<SomeMessages>;\n\n/** A function that loads on demand the messages for a specific locale. */\nexport type LoadLazyMessagesByDevice<SomeMessages> = () => Promise<{\n default: MessagesByDevice<SomeMessages>;\n}>;\n\n/**\n * An object containing a base property with all the existing messages and the overridden ones for\n * each device.\n */\nexport interface MessagesByDevice<SomeMessages> {\n base: SomeMessages;\n [device: Device]: DeepPartial<SomeMessages>;\n}\n\n/**\n * I18n settings.\n */\nexport interface I18nOptions<SomeMessages> {\n /** The initial messages. */\n messages: Record<Locale, AnyMessages<SomeMessages>>;\n /** The initial device. */\n device: Device;\n /** The locale to fall back if no matching locale is available. */\n fallbackLocale: Locale;\n /** The initial locale. */\n locale: Locale;\n}\n\n/**\n * The VueI18n instance that should be passed to the root Vue instance and some runtime utility\n * functions.\n */\nexport interface I18nAPI {\n /** The Vue I18n instance that should be passed to the root Vue component. */\n readonly vueI18n: Readonly<VueI18n>;\n /**\n * Sets the new locale.\n *\n * @param newLocale - The new locale.\n */\n setLocale: (newLocale: Locale) => Promise<void>;\n /**\n * Sets the new device.\n *\n * @param newDevice - The new device.\n */\n setDevice: (newDevice: Device) => Promise<void>;\n}\n"]}
|
package/dist/esm/index.js
CHANGED
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gCAAgC,CAAC","sourcesContent":["export * from './i18n/i18n.plugin';\nexport * from './i18n/i18n.types';\nexport * from './build/webpack/webpack.config';\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**.
|
|
2
|
+
* Self-made interface since Webpack does not provide a type for this object.
|
|
3
|
+
* Https://github.com/webpack/webpack/blob/main/declarations/WebpackOptions.d.ts#L463
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
interface webpackFilenameTemplateInfo {
|
|
8
|
+
identifier: string;
|
|
9
|
+
shortIdentifier: string;
|
|
10
|
+
resource: string;
|
|
11
|
+
resourcePath: string;
|
|
12
|
+
absoluteResourcePath: string;
|
|
13
|
+
allLoaders: string;
|
|
14
|
+
query: string;
|
|
15
|
+
moduleId: string;
|
|
16
|
+
hash: string;
|
|
17
|
+
namespace: string;
|
|
18
|
+
}
|
|
19
|
+
export declare const webpackConfig: {
|
|
20
|
+
transpileDependencies: string[];
|
|
21
|
+
configureWebpack: {
|
|
22
|
+
module: {
|
|
23
|
+
rules: {
|
|
24
|
+
test: RegExp;
|
|
25
|
+
enforce: string;
|
|
26
|
+
use: string[];
|
|
27
|
+
}[];
|
|
28
|
+
};
|
|
29
|
+
output: {
|
|
30
|
+
devtoolModuleFilenameTemplate: ({ resourcePath, hash, query }: webpackFilenameTemplateInfo) => string;
|
|
31
|
+
devtoolFallbackModuleFilenameTemplate: string;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { VueConstructor } from 'vue';
|
|
2
|
+
import VueI18n, { LocaleMessageObject } from 'vue-i18n';
|
|
3
|
+
import { AnyMessages, Device, I18nOptions, Locale } from './i18n.types';
|
|
4
|
+
/**
|
|
5
|
+
* I18n settings manager.
|
|
6
|
+
*
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare class I18n<SomeMessages> {
|
|
10
|
+
vueI18n: VueI18n;
|
|
11
|
+
protected locale: Locale;
|
|
12
|
+
protected device: Device;
|
|
13
|
+
protected messages: Record<Locale, AnyMessages<SomeMessages>>;
|
|
14
|
+
protected currentMessages: LocaleMessageObject;
|
|
15
|
+
protected fallbackLocale: Locale;
|
|
16
|
+
/**
|
|
17
|
+
* Constructs a new {@link I18n} instance with the passed {@link I18nOptions | i18n options}.
|
|
18
|
+
*
|
|
19
|
+
* @param options - The new {@link I18nOptions | i18n options}.
|
|
20
|
+
*/
|
|
21
|
+
constructor({ locale, messages, device, fallbackLocale }: I18nOptions<SomeMessages>);
|
|
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 create<SomeMessages>(options: I18nOptions<SomeMessages>): Promise<I18n<SomeMessages>>;
|
|
30
|
+
/**
|
|
31
|
+
* Installs VueI18n plugin.
|
|
32
|
+
*
|
|
33
|
+
* @param vue - The Vue instance.
|
|
34
|
+
*/
|
|
35
|
+
install(vue: VueConstructor): void;
|
|
36
|
+
/**
|
|
37
|
+
* Tries to get a value from the messages object accessing it key by key.
|
|
38
|
+
* When a key fails, takes the remaining key parts and tries to access the message
|
|
39
|
+
* joining them into a single key.
|
|
40
|
+
*
|
|
41
|
+
* @param path - A dot separated key to access the message object.
|
|
42
|
+
* @returns The message corresponding to the provided key, or empty string if it doesn't exist.
|
|
43
|
+
*/
|
|
44
|
+
protected getMessageWithDotsInKey(path: string): string | null;
|
|
45
|
+
/**
|
|
46
|
+
* Sets the new locale and updates the messages accordingly.
|
|
47
|
+
*
|
|
48
|
+
* @param newLocale - The new locale.
|
|
49
|
+
*
|
|
50
|
+
* @returns The new messages.
|
|
51
|
+
*/
|
|
52
|
+
setLocale(newLocale: Locale): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Sets the new device and updates the messages accordingly.
|
|
55
|
+
*
|
|
56
|
+
* @param newDevice - The new device.
|
|
57
|
+
*
|
|
58
|
+
* @returns The new messages.
|
|
59
|
+
*/
|
|
60
|
+
setDevice(newDevice: Device): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Updates the current messages based on the new locale and/or device.
|
|
63
|
+
*/
|
|
64
|
+
protected changeMessages(): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves the corresponding messages for the set locale and device.
|
|
67
|
+
*
|
|
68
|
+
* @remarks By default, it merges `base` messages with the specific `device` ones. If there are
|
|
69
|
+
* no messages for the current locale, the fallback locale is used instead.
|
|
70
|
+
*
|
|
71
|
+
* @returns The messages for the current locale and device.
|
|
72
|
+
*/
|
|
73
|
+
protected getCurrentMessages(): Promise<LocaleMessageObject>;
|
|
74
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import VueI18n from 'vue-i18n';
|
|
2
|
+
import { DeepPartial } from '@empathyco/x-utils';
|
|
3
|
+
/** Supported locales. */
|
|
4
|
+
export declare type Locale = string;
|
|
5
|
+
/** Supported devices. */
|
|
6
|
+
export declare type Device = string;
|
|
7
|
+
/** Union type containing both eager and lazy messages. */
|
|
8
|
+
export declare type AnyMessages<SomeMessages> = MessagesByDevice<SomeMessages> | LoadLazyMessagesByDevice<SomeMessages>;
|
|
9
|
+
/** A function that loads on demand the messages for a specific locale. */
|
|
10
|
+
export declare type LoadLazyMessagesByDevice<SomeMessages> = () => Promise<{
|
|
11
|
+
default: MessagesByDevice<SomeMessages>;
|
|
12
|
+
}>;
|
|
13
|
+
/**
|
|
14
|
+
* An object containing a base property with all the existing messages and the overridden ones for
|
|
15
|
+
* each device.
|
|
16
|
+
*/
|
|
17
|
+
export interface MessagesByDevice<SomeMessages> {
|
|
18
|
+
base: SomeMessages;
|
|
19
|
+
[device: Device]: DeepPartial<SomeMessages>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* I18n settings.
|
|
23
|
+
*/
|
|
24
|
+
export interface I18nOptions<SomeMessages> {
|
|
25
|
+
/** The initial messages. */
|
|
26
|
+
messages: Record<Locale, AnyMessages<SomeMessages>>;
|
|
27
|
+
/** The initial device. */
|
|
28
|
+
device: Device;
|
|
29
|
+
/** The locale to fall back if no matching locale is available. */
|
|
30
|
+
fallbackLocale: Locale;
|
|
31
|
+
/** The initial locale. */
|
|
32
|
+
locale: Locale;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The VueI18n instance that should be passed to the root Vue instance and some runtime utility
|
|
36
|
+
* functions.
|
|
37
|
+
*/
|
|
38
|
+
export interface I18nAPI {
|
|
39
|
+
/** The Vue I18n instance that should be passed to the root Vue component. */
|
|
40
|
+
readonly vueI18n: Readonly<VueI18n>;
|
|
41
|
+
/**
|
|
42
|
+
* Sets the new locale.
|
|
43
|
+
*
|
|
44
|
+
* @param newLocale - The new locale.
|
|
45
|
+
*/
|
|
46
|
+
setLocale: (newLocale: Locale) => Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Sets the new device.
|
|
49
|
+
*
|
|
50
|
+
* @param newDevice - The new device.
|
|
51
|
+
*/
|
|
52
|
+
setDevice: (newDevice: Device) => Promise<void>;
|
|
53
|
+
}
|
package/dist/types/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@empathyco/x-archetype-utils",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.10",
|
|
4
4
|
"description": "Utilities for x-archetype",
|
|
5
5
|
"author": "Empathy Systems Corporation S.L.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"directory": "packages/x-archetype-utils"
|
|
23
23
|
},
|
|
24
24
|
"engines": {
|
|
25
|
-
"node": "
|
|
25
|
+
"node": "16"
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
28
|
"prebuild": "rimraf dist ./*.tgz",
|
|
@@ -33,16 +33,27 @@
|
|
|
33
33
|
"test": "jest",
|
|
34
34
|
"prepublishOnly": "npm run build"
|
|
35
35
|
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@empathyco/x-deep-merge": "^1.3.0-alpha.27",
|
|
38
|
+
"vue-i18n": "~8.21.0"
|
|
39
|
+
},
|
|
36
40
|
"devDependencies": {
|
|
41
|
+
"@empathyco/x-utils": "^1.0.0-alpha.13",
|
|
37
42
|
"@types/jest": "~27.0.3",
|
|
38
43
|
"@types/node": "~16.4.12",
|
|
39
|
-
"
|
|
44
|
+
"@vue/test-utils": "~1.0.3",
|
|
45
|
+
"concurrently": "~7.6.0",
|
|
40
46
|
"jest": "~27.3.1",
|
|
41
47
|
"ts-jest": "~27.0.7",
|
|
42
|
-
"typescript": "~4.6.2"
|
|
48
|
+
"typescript": "~4.6.2",
|
|
49
|
+
"vue": "~2.7.14"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"vue": "^2.7.0",
|
|
53
|
+
"vue-i18n": "^8.0.0"
|
|
43
54
|
},
|
|
44
55
|
"publishConfig": {
|
|
45
56
|
"access": "public"
|
|
46
57
|
},
|
|
47
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "9a571e850e671c1227d17db9b574579a8a8464ae"
|
|
48
59
|
}
|