@worldware/msg 0.5.3 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
2
+ export type MsgNote = {
3
+ type: NoteTypes;
4
+ content: string;
5
+ };
6
+ export type MsgAttributes = {
7
+ lang?: string;
8
+ dir?: string;
9
+ dnt?: boolean;
10
+ };
11
+ export declare const DEFAULT_ATTRIBUTES: MsgAttributes;
12
+ export interface MsgInterface {
13
+ attributes: MsgAttributes;
14
+ notes: MsgNote[];
15
+ }
16
+ export {};
17
+ //# sourceMappingURL=MsgInterface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MsgInterface.d.ts","sourceRoot":"","sources":["../../../src/classes/MsgInterface/MsgInterface.ts"],"names":[],"mappings":"AAAA,KAAK,SAAS,GAAI,aAAa,GAAG,YAAY,GAAG,YAAY,GAAG,SAAS,GAAG,SAAS,CAAC;AAEtF,MAAM,MAAM,OAAO,GAAG;IACpB,IAAI,EAAE,SAAS,CAAA;IACf,OAAO,EAAE,MAAM,CAAC;CACjB,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,OAAO,CAAA;CACd,CAAA;AAED,eAAO,MAAM,kBAAkB,EAAE,aAIhC,CAAA;AAID,MAAM,WAAW,YAAY;IAC3B,UAAU,EAAE,aAAa,CAAA;IACzB,KAAK,EAAE,OAAO,EAAE,CAAA;CACjB"}
@@ -0,0 +1,6 @@
1
+ export const DEFAULT_ATTRIBUTES = {
2
+ lang: '',
3
+ dir: '',
4
+ dnt: false
5
+ };
6
+ //# sourceMappingURL=MsgInterface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MsgInterface.js","sourceRoot":"","sources":["../../../src/classes/MsgInterface/MsgInterface.ts"],"names":[],"mappings":"AAaA,MAAM,CAAC,MAAM,kBAAkB,GAAkB;IAC/C,IAAI,EAAE,EAAE;IACR,GAAG,EAAE,EAAE;IACP,GAAG,EAAE,KAAK;CACX,CAAA"}
@@ -0,0 +1,36 @@
1
+ import { type MessageFormatOptions } from "messageformat";
2
+ import { MsgInterface, type MsgAttributes, type MsgNote } from "../MsgInterface/MsgInterface.js";
3
+ export type MsgMessageData = {
4
+ key: string;
5
+ value: string;
6
+ attributes?: MsgAttributes;
7
+ notes?: MsgNote[];
8
+ };
9
+ export declare class MsgMessage implements MsgInterface {
10
+ private _key;
11
+ private _value;
12
+ private _mf?;
13
+ private _attributes;
14
+ private _notes;
15
+ private constructor();
16
+ static create(data: MsgMessageData): MsgMessage;
17
+ get key(): string;
18
+ get value(): string;
19
+ get attributes(): MsgAttributes;
20
+ get lang(): string | undefined;
21
+ get dir(): string | undefined;
22
+ get dnt(): boolean | undefined;
23
+ get notes(): MsgNote[];
24
+ addNote(note: MsgNote): void;
25
+ format(data: Record<string, any>, options?: MessageFormatOptions): string;
26
+ formatToParts(data: Record<string, any>, options?: MessageFormatOptions): import("messageformat").MessagePart<never>[];
27
+ getData(stripNotes?: boolean): {
28
+ key: string;
29
+ value: string;
30
+ attributes: MsgAttributes;
31
+ notes: MsgNote[] | undefined;
32
+ };
33
+ toString(): string;
34
+ toJSON(stripNotes?: boolean): string;
35
+ }
36
+ //# sourceMappingURL=MsgMessage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MsgMessage.d.ts","sourceRoot":"","sources":["../../../src/classes/MsgMessage/MsgMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,KAAK,aAAa,EAAE,KAAK,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAEjG,MAAM,MAAM,cAAc,GAAG;IAC3B,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,UAAU,CAAC,EAAE,aAAa,CAAC;IAC3B,KAAK,CAAC,EAAE,OAAO,EAAE,CAAA;CAClB,CAAA;AAQD,qBAAa,UAAW,YAAW,YAAY;IAC7C,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,GAAG,CAAC,CAAgB;IAC5B,OAAO,CAAC,WAAW,CAAqC;IACxD,OAAO,CAAC,MAAM,CAAiB;IAE/B,OAAO;IAcP,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,cAAc;IAMlC,IAAW,GAAG,WAEb;IAED,IAAW,KAAK,WAEf;IAED,IAAW,UAAU,kBAEpB;IAED,IAAW,IAAI,uBAEd;IAED,IAAW,GAAG,uBAEb;IAED,IAAW,GAAG,wBAEb;IAED,IAAW,KAAK,cAEf;IAEM,OAAO,CAAC,IAAI,EAAE,OAAO;IAIrB,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,oBAAoB;IAOhE,aAAa,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,oBAAoB;IAOvE,OAAO,CAAC,UAAU,GAAE,OAAe;;;;;;IASnC,QAAQ;IAIR,MAAM,CAAC,UAAU,GAAE,OAAe;CAI1C"}
@@ -0,0 +1,76 @@
1
+ import { MessageFormat } from "messageformat";
2
+ const DEFAULT_ATTRIBUTES = {
3
+ lang: "",
4
+ dir: "",
5
+ dnt: false
6
+ };
7
+ export class MsgMessage {
8
+ constructor(key, value, attributes, notes) {
9
+ this._attributes = DEFAULT_ATTRIBUTES;
10
+ this._notes = [];
11
+ this._key = key;
12
+ this._value = value;
13
+ // merge in any attributes
14
+ this._attributes = attributes ? { ...this._attributes, ...attributes } : this._attributes;
15
+ // add any notes
16
+ if (notes) {
17
+ notes.forEach(note => this.addNote(note));
18
+ }
19
+ }
20
+ static create(data) {
21
+ const { key, value, attributes, notes } = data;
22
+ const message = new MsgMessage(key, value, attributes, notes);
23
+ return message;
24
+ }
25
+ get key() {
26
+ return this._key;
27
+ }
28
+ get value() {
29
+ return this._value;
30
+ }
31
+ get attributes() {
32
+ return this._attributes;
33
+ }
34
+ get lang() {
35
+ return this._attributes['lang'];
36
+ }
37
+ get dir() {
38
+ return this._attributes['dir'];
39
+ }
40
+ get dnt() {
41
+ return this._attributes['dnt'];
42
+ }
43
+ get notes() {
44
+ return this._notes;
45
+ }
46
+ addNote(note) {
47
+ this.notes.push(note);
48
+ }
49
+ format(data, options) {
50
+ if (!this._mf) {
51
+ this._mf = new MessageFormat(this.lang, this.value, options);
52
+ }
53
+ return this._mf.format(data);
54
+ }
55
+ formatToParts(data, options) {
56
+ if (!this._mf) {
57
+ this._mf = new MessageFormat(this.lang, this.value, options);
58
+ }
59
+ return this._mf?.formatToParts(data);
60
+ }
61
+ getData(stripNotes = false) {
62
+ return {
63
+ key: this.key,
64
+ value: this.value,
65
+ attributes: this.attributes,
66
+ notes: !stripNotes && this.notes.length > 0 ? this.notes : undefined
67
+ };
68
+ }
69
+ toString() {
70
+ return this.value;
71
+ }
72
+ toJSON(stripNotes = false) {
73
+ return JSON.stringify(this.getData(stripNotes), null, 2);
74
+ }
75
+ }
76
+ //# sourceMappingURL=MsgMessage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MsgMessage.js","sourceRoot":"","sources":["../../../src/classes/MsgMessage/MsgMessage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAA6B,MAAM,eAAe,CAAC;AAUzE,MAAM,kBAAkB,GAAkB;IACxC,IAAI,EAAE,EAAE;IACR,GAAG,EAAE,EAAE;IACP,GAAG,EAAE,KAAK;CACX,CAAA;AAED,MAAM,OAAO,UAAU;IAOrB,YAAoB,GAAW,EAAE,KAAa,EAAE,UAA0B,EAAE,KAAiB;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 -0,0 +1,32 @@
1
+ import { type MsgResourceData } from "../MsgResource/MsgResource.js";
2
+ type MsgProjectSettings = {
3
+ name: string;
4
+ version?: number;
5
+ };
6
+ type MsgTargetLocalesSettings = {
7
+ [key: string]: string[];
8
+ };
9
+ type MsgLocalesSettings = {
10
+ sourceLocale: string;
11
+ pseudoLocale: string;
12
+ targetLocales: MsgTargetLocalesSettings;
13
+ };
14
+ export type MsgTranslationLoader = (project: string, title: string, lang: string) => Promise<MsgResourceData>;
15
+ export type MsgProjectData = {
16
+ project: MsgProjectSettings;
17
+ locales: MsgLocalesSettings;
18
+ loader: MsgTranslationLoader;
19
+ };
20
+ export declare class MsgProject {
21
+ _project: MsgProjectSettings;
22
+ _locales: MsgLocalesSettings;
23
+ _loader: MsgTranslationLoader;
24
+ static create(data: MsgProjectData): MsgProject;
25
+ private constructor();
26
+ get project(): MsgProjectSettings;
27
+ get locales(): MsgLocalesSettings;
28
+ get loader(): MsgTranslationLoader;
29
+ getTargetLocale(locale: string): string[] | undefined;
30
+ }
31
+ export {};
32
+ //# sourceMappingURL=MsgProject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MsgProject.d.ts","sourceRoot":"","sources":["../../../src/classes/MsgProject/MsgProject.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAErE,KAAK,kBAAkB,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB,CAAC;AAEF,KAAK,wBAAwB,GAAG;IAC9B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CACxB,CAAA;AAED,KAAK,kBAAkB,GAAG;IACxB,YAAY,EAAE,MAAM,CAAA;IACpB,YAAY,EAAE,MAAM,CAAA;IACpB,aAAa,EAAE,wBAAwB,CAAA;CACxC,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,eAAe,CAAC,CAAC;AAE9G,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,EAAE,kBAAkB,CAAA;IAC3B,OAAO,EAAE,kBAAkB,CAAA;IAC3B,MAAM,EAAE,oBAAoB,CAAA;CAC7B,CAAC;AAeF,qBAAa,UAAU;IACrB,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,QAAQ,EAAE,kBAAkB,CAAC;IAC7B,OAAO,EAAE,oBAAoB,CAAC;IAE9B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,cAAc;IAKlC,OAAO;IAUP,IAAW,OAAO,uBAEjB;IAED,IAAW,OAAO,uBAEjB;IAED,IAAW,MAAM,yBAEhB;IAEM,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS;CAI7D"}
@@ -0,0 +1,35 @@
1
+ const defaultProjectSettings = {
2
+ name: 'messages',
3
+ version: 1
4
+ };
5
+ const defaultLocalesSettings = {
6
+ sourceLocale: 'en',
7
+ pseudoLocale: 'en-XA',
8
+ targetLocales: {
9
+ en: ['en']
10
+ }
11
+ };
12
+ export class MsgProject {
13
+ static create(data) {
14
+ const { project, locales, loader } = data;
15
+ return new MsgProject(project, locales, loader);
16
+ }
17
+ constructor(projectSettings, localesSettings, loader) {
18
+ this._project = { ...defaultProjectSettings, ...projectSettings };
19
+ this._locales = { ...defaultLocalesSettings, ...localesSettings };
20
+ this._loader = loader;
21
+ }
22
+ get project() {
23
+ return this._project;
24
+ }
25
+ get locales() {
26
+ return this._locales;
27
+ }
28
+ get loader() {
29
+ return this._loader;
30
+ }
31
+ getTargetLocale(locale) {
32
+ return this._locales.targetLocales[locale];
33
+ }
34
+ }
35
+ //# sourceMappingURL=MsgProject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MsgProject.js","sourceRoot":"","sources":["../../../src/classes/MsgProject/MsgProject.ts"],"names":[],"mappings":"AAyBA,MAAM,sBAAsB,GAAuB;IACjD,IAAI,EAAE,UAAU;IAChB,OAAO,EAAE,CAAC;CACX,CAAC;AAEF,MAAM,sBAAsB,GAAuB;IACjD,YAAY,EAAE,IAAI;IAClB,YAAY,EAAE,OAAO;IACrB,aAAa,EAAE;QACb,EAAE,EAAE,CAAC,IAAI,CAAC;KACX;CACF,CAAC;AAEF,MAAM,OAAO,UAAU;IAKrB,MAAM,CAAC,MAAM,CAAC,IAAoB;QAChC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QAC1C,OAAO,IAAI,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAClD,CAAC;IAED,YACE,eAAmC,EACnC,eAAmC,EACnC,MAA4B;QAE5B,IAAI,CAAC,QAAQ,GAAG,EAAC,GAAG,sBAAsB,EAAE,GAAG,eAAe,EAAC,CAAC;QAChE,IAAI,CAAC,QAAQ,GAAG,EAAC,GAAG,sBAAsB,EAAE,GAAG,eAAe,EAAC,CAAC;QAChE,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED,IAAW,MAAM;QACf,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAEM,eAAe,CAAC,MAAc;QACnC,OAAO,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;CAEF"}
@@ -0,0 +1,30 @@
1
+ import { type MsgMessageData, MsgMessage } from "../MsgMessage/MsgMessage.js";
2
+ import { MsgAttributes, MsgInterface, MsgNote } from "../MsgInterface/MsgInterface.js";
3
+ import { MsgProject } from "../MsgProject/MsgProject.js";
4
+ export type MsgResourceData = {
5
+ title: string;
6
+ attributes: MsgAttributes;
7
+ notes?: MsgNote[];
8
+ messages?: MsgMessageData[];
9
+ };
10
+ export declare class MsgResource extends Map<string, MsgMessage> implements MsgInterface {
11
+ private _attributes;
12
+ private _notes;
13
+ private _title;
14
+ private _project;
15
+ static create(data: MsgResourceData, project: MsgProject): MsgResource;
16
+ private constructor();
17
+ get attributes(): MsgAttributes;
18
+ set attributes(attributes: MsgAttributes);
19
+ get notes(): MsgNote[];
20
+ set notes(notes: MsgNote[]);
21
+ addNote(note: MsgNote): void;
22
+ get title(): string;
23
+ set title(title: string);
24
+ add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): this;
25
+ translate(data: MsgResourceData): MsgResource;
26
+ getTranslation(lang: string): Promise<MsgResource>;
27
+ getData(stripNotes?: boolean): MsgResourceData;
28
+ toJSON(stripNotes?: boolean): string;
29
+ }
30
+ //# sourceMappingURL=MsgResource.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MsgResource.d.ts","sourceRoot":"","sources":["../../../src/classes/MsgResource/MsgResource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,cAAc,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAsB,aAAa,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAC3G,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAEzD,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,UAAU,EAAE,aAAa,CAAA;IACzB,KAAK,CAAC,EAAE,OAAO,EAAE,CAAA;IACjB,QAAQ,CAAC,EAAE,cAAc,EAAE,CAAA;CAC5B,CAAA;AAED,qBAAa,WAAY,SAAQ,GAAG,CAAC,MAAM,EAAE,UAAU,CAAE,YAAW,YAAY;IAE9E,OAAO,CAAC,WAAW,CAAqC;IACxD,OAAO,CAAC,MAAM,CAAiB;IAC/B,OAAO,CAAC,MAAM,CAAS;IAEvB,OAAO,CAAC,QAAQ,CAAa;IAE7B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,UAAU;IAgBxD,OAAO;IAaP,IAAW,UAAU,IAIa,aAAa,CAF9C;IAED,IAAW,UAAU,CAAC,UAAU,EAAE,aAAa,EAE9C;IAED,IAAW,KAAK,IAIQ,OAAO,EAAE,CAFhC;IAED,IAAW,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,EAEhC;IAEM,OAAO,CAAC,IAAI,EAAE,OAAO;IAI5B,IAAW,KAAK,IAIQ,MAAM,CAF7B;IAED,IAAW,KAAK,CAAC,KAAK,EAAE,MAAM,EAE7B;IAEM,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,aAAa,EAAE,KAAK,CAAC,EAAE,OAAO,EAAE;IAc7E,SAAS,CAAC,IAAI,EAAE,eAAe;IAmCzB,cAAc,CAAC,IAAI,EAAE,MAAM;IA0BjC,OAAO,CAAC,UAAU,GAAE,OAAe,GAAG,eAAe;IAarD,MAAM,CAAC,UAAU,GAAE,OAAe;CAI1C"}
@@ -0,0 +1,123 @@
1
+ import { MsgMessage } from "../MsgMessage/MsgMessage.js";
2
+ import { DEFAULT_ATTRIBUTES } from "../MsgInterface/MsgInterface.js";
3
+ export class MsgResource extends Map {
4
+ static create(data, project) {
5
+ const { title, attributes, notes, messages } = data;
6
+ const res = new MsgResource(title, attributes, project, notes);
7
+ if (messages) {
8
+ messages.forEach(messageData => {
9
+ const { key, value, attributes, notes } = messageData;
10
+ res.add(key, value, attributes, notes);
11
+ });
12
+ }
13
+ else {
14
+ res.clear();
15
+ }
16
+ return res;
17
+ }
18
+ constructor(title, attributes, project, notes) {
19
+ super();
20
+ this._attributes = DEFAULT_ATTRIBUTES;
21
+ this._notes = [];
22
+ this._title = title;
23
+ this._attributes = { ...this._attributes, ...attributes };
24
+ this._project = project;
25
+ if (notes) {
26
+ notes.forEach(note => this.addNote(note));
27
+ }
28
+ }
29
+ get attributes() {
30
+ return this._attributes;
31
+ }
32
+ set attributes(attributes) {
33
+ this._attributes = attributes;
34
+ }
35
+ get notes() {
36
+ return this._notes;
37
+ }
38
+ set notes(notes) {
39
+ this._notes = notes;
40
+ }
41
+ addNote(note) {
42
+ this.notes.push(note);
43
+ }
44
+ get title() {
45
+ return this._title;
46
+ }
47
+ set title(title) {
48
+ this._title = title;
49
+ }
50
+ add(key, value, attributes, notes) {
51
+ const merged = { ...this.attributes, ...attributes };
52
+ const msg = MsgMessage.create({
53
+ key,
54
+ value,
55
+ attributes: merged,
56
+ notes
57
+ });
58
+ this.set(key, msg);
59
+ return this;
60
+ }
61
+ translate(data) {
62
+ const { title, attributes, messages } = data;
63
+ if (title !== this.title) {
64
+ throw new TypeError('Title of resource and translations do not match.');
65
+ }
66
+ const translated = MsgResource.create({
67
+ title,
68
+ attributes,
69
+ notes: this.notes, // transfer the notes
70
+ }, this._project);
71
+ // use messages from the resource as defaults
72
+ this.forEach(msg => {
73
+ translated.set(msg.key, msg);
74
+ });
75
+ messages?.forEach(messageData => {
76
+ const { key, value, attributes } = messageData;
77
+ const msg = MsgMessage.create({
78
+ key,
79
+ value,
80
+ attributes,
81
+ });
82
+ const notes = this.get(key)?.notes || []; // transfer the notes
83
+ notes.forEach(note => {
84
+ msg.addNote(note);
85
+ });
86
+ translated.set(key, msg);
87
+ });
88
+ return translated;
89
+ }
90
+ async getTranslation(lang) {
91
+ const project = this._project;
92
+ const languageChain = project.getTargetLocale(lang);
93
+ if (!languageChain) {
94
+ throw new Error("Unsupported locale for resource.");
95
+ }
96
+ if (languageChain.length == 0) {
97
+ throw new Error(`Empty language chain for locale: ${lang}`);
98
+ }
99
+ let translated = this;
100
+ for (let i = 0; i < languageChain.length; i++) {
101
+ const lang = languageChain[i];
102
+ if (lang && project._locales.targetLocales[lang]) {
103
+ translated = await translated._project._loader(project.project.name, translated.title, lang)
104
+ .then(data => translated.translate(data));
105
+ }
106
+ }
107
+ return translated;
108
+ }
109
+ getData(stripNotes = false) {
110
+ const messages = [];
111
+ this.forEach(msg => messages.push(msg.getData(stripNotes)));
112
+ return {
113
+ title: this.title,
114
+ attributes: this.attributes,
115
+ notes: !stripNotes && this.notes.length > 0 ? this.notes : undefined,
116
+ messages
117
+ };
118
+ }
119
+ toJSON(stripNotes = false) {
120
+ return JSON.stringify(this.getData(stripNotes), null, 2);
121
+ }
122
+ }
123
+ //# sourceMappingURL=MsgResource.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MsgResource.js","sourceRoot":"","sources":["../../../src/classes/MsgResource/MsgResource.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAAwC,MAAM,iCAAiC,CAAC;AAU3G,MAAM,OAAO,WAAY,SAAQ,GAAuB;IAQtD,MAAM,CAAC,MAAM,CAAC,IAAqB,EAAE,OAAmB;QACtD,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAC,GAAI,IAAI,CAAC;QACpD,MAAM,GAAG,GAAG,IAAI,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;QAE/D,IAAI,QAAQ,EAAE,CAAC;YACb,QAAQ,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE;gBAC7B,MAAM,EAAC,GAAG,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,EAAC,GAAG,WAAW,CAAC;gBACpD,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;YACzC,CAAC,CAAC,CAAA;QACJ,CAAC;aAAM,CAAC;YACN,GAAG,CAAC,KAAK,EAAE,CAAC;QACd,CAAC;QAED,OAAO,GAAG,CAAC;IACb,CAAC;IAED,YAAqB,KAAa,EAAE,UAAyB,EAAE,OAAmB,EAAE,KAAiB;QACnG,KAAK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E,CAAC,UAAU,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS;YACpE,QAAQ;SACT,CAAA;IACH,CAAC;IAEM,MAAM,CAAC,aAAsB,KAAK;QACvC,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IAC3D,CAAC;CAEF"}
@@ -0,0 +1,4 @@
1
+ export * from './MsgMessage/MsgMessage.js';
2
+ export * from './MsgResource/MsgResource.js';
3
+ export * from './MsgProject/MsgProject.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/classes/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,4BAA4B,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from './MsgMessage/MsgMessage.js';
2
+ export * from './MsgResource/MsgResource.js';
3
+ export * from './MsgProject/MsgProject.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/classes/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,4BAA4B,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './classes/index.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './classes/index.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@worldware/msg",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "description": "Message localization tooling",
5
5
  "license": "MIT",
6
6
  "author": "Joel Sahleen",