/
messages-translator.js
105 lines (85 loc) · 4.27 KB
/
messages-translator.js
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
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
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
103
104
105
/*
This file is part of Talkie -- text-to-speech browser extension button.
<https://joelpurra.com/projects/talkie/>
Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021 Joel Purra <https://joelpurra.com/>
Talkie is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Talkie is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Talkie. If not, see <https://www.gnu.org/licenses/>.
*/
const assert = require("assert");
const Promise = require("bluebird");
export default class MessagesTranslator {
constructor(translatorService, base, locale) {
assert(typeof translatorService === "object");
assert(typeof base === "object");
assert(typeof base.messages === "object");
assert(typeof base.language === "string");
assert(typeof locale === "object");
assert(typeof locale.messages === "object");
assert(typeof locale.language === "string");
// NOTE: ensure the base language isn't translated onto itself.
assert(typeof locale.language !== base.language);
this._translatorService = translatorService;
this._base = base;
this._locale = locale;
}
_keepOnlyKeys(keys, obj) {
assert(Array.isArray(keys));
keys.forEach((key) => {
assert(typeof key === "string" || (typeof key === "number" && Math.floor(key) === Math.ceil(key)));
});
assert(typeof obj === "object");
return Promise.try(() => keys.reduce((newObj, key) => {
newObj[key] = obj[key];
return newObj;
},
{}));
}
translate() {
return Promise.try(() => {
const baseKeys = Object.keys(this._base.messages);
return this._keepOnlyKeys(baseKeys, this._locale.messages)
.then((localeMessages) => {
// TODO: function filterKeys.
const alreadyTranslatedLocale = baseKeys.reduce((alreadyTranslated, baseKey) => {
if (localeMessages[baseKey] && localeMessages[baseKey].original === this._base.messages[baseKey].message) {
alreadyTranslated[baseKey] = localeMessages[baseKey];
}
return alreadyTranslated;
},
{});
// TODO: function filterKeys.
const untranslatedLocale = baseKeys.reduce((untranslated, baseKey) => {
if (!localeMessages[baseKey] || localeMessages[baseKey].original !== this._base.messages[baseKey].message) {
untranslated[baseKey] = this._base.messages[baseKey];
}
return untranslated;
},
{});
if (Object.keys(untranslatedLocale).length === 0) {
const translated = alreadyTranslatedLocale;
return translated;
}
return this._translatorService.translate(this._base.language, this._locale.language, untranslatedLocale)
.tap((automaticallyTranslated) => {
assert(typeof automaticallyTranslated === "object");
Object.keys(automaticallyTranslated).forEach((automaticallyTranslatedKey) => {
assert(typeof automaticallyTranslated[automaticallyTranslatedKey].original === "string");
assert(typeof automaticallyTranslated[automaticallyTranslatedKey].message === "string");
});
})
.then((automaticallyTranslated) => {
const translated = Object.assign({}, alreadyTranslatedLocale, automaticallyTranslated);
return translated;
});
});
});
}
}