UNPKG

1.19 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('../base/Base');
7
8module.exports = class MessageSource extends Base {
9
10 constructor (config) {
11 super({
12 forceTranslation: false, // translate source language
13 sourceLanguage: config.i18n.sourceLanguage,
14 ...config
15 });
16 this._messages = {};
17 }
18
19 async load () {
20 throw new Error('Load messages from storage');
21 }
22
23 setParent (parent) {
24 this.parent = parent;
25 this.forceTranslationParent = parent
26 ? (parent.forceTranslation ? parent : parent.forceTranslationParent)
27 : null;
28 }
29
30 translate (message, language) {
31 const data = this._messages[language];
32 if (data && Object.prototype.hasOwnProperty.call(data, message)) {
33 return data[message];
34 }
35 return this.parent ? this.parent.translate(message, language) : null;
36 }
37
38 log () {
39 CommonHelper.log(this.i18n, this.constructor.name, ...arguments);
40 }
41};
42
43const CommonHelper = require('../helper/CommonHelper');
\No newline at end of file