-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.model.ts
38 lines (33 loc) · 1023 Bytes
/
label.model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { Languages } from './languages.model';
/**
* Each of the attributes contains the translation in that language of the Label.
*
* Example:
* ```
* name.it: 'ciao';
* name.en: 'hello';
* ```
*/
export class Label {
/**
* It supports only _key -> values_ of type string, representing translations in different languages.
*/
[key: string]: string | any;
constructor(x?: any, languages?: Languages) {
this.load(x, languages);
}
load(x?: any, languages?: Languages): void {
if (x) languages.available.forEach(l => (this[l] = x[l] ? String(x[l]) : null));
else languages.available.forEach(l => (this[l] = null));
}
validate(languages: Languages): string[] {
if (!this[languages.default]) return [languages.default];
else return [];
}
/**
* Translate the label in the desired language; in case there's no translation, get the default one.
*/
translate(language: string, languages: Languages): any {
return this[language] || this[languages.default];
}
}