-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.js
153 lines (138 loc) · 5.62 KB
/
translate.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This node provides DLP processing. We will assume that msg.payload
* contains the original data that may contain sensitive data.
*
* The configuration for the node includes:
*
*/
/* jshint esversion: 8 */
//@ts-check
module.exports = function (RED) {
"use strict";
const NODE_TYPE = "google-cloud-translate";
const translate = require('@google-cloud/translate');
let translationServiceClient;
function TranslateNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const context = node.context();
const projectId = config.projectId;
const sourceLanguageCodeType = config.sourceLanguageCodeType || "str";
const targetLanguageCodeType = config.targetLanguageCodeType || "str";
const outputFormat = config.outputFormat || "full";
const displayLanguageCode = Intl.DateTimeFormat().resolvedOptions().locale || "en";
let credentials = null;
if (config.account) {
credentials = GetCredentials(config.account);
}
const keyFilename = config.keyFilename;
/**
* Extract JSON service account key from "google-cloud-credentials" config node.
*/
function GetCredentials(node) {
return JSON.parse(RED.nodes.getCredentials(node).account);
}
/**
* Get supported languages from global context. If not found, load from Google Could Translate and store in global context.
*
* @returns List of supported languages for translation
*/
async function getSupportedLanguages() {
let supportedLanguages = context.global.get("google-cloud-translate-languages");
if (!supportedLanguages) {
const request = {
"displayLanguageCode": displayLanguageCode,
"parent": translationServiceClient.locationPath(projectId, "global")
};
supportedLanguages = (await translationServiceClient.getSupportedLanguages(request))[0].languages;
context.global.set("google-cloud-translate-languages", supportedLanguages);
}
return supportedLanguages;
}
/**
* Gets the language codes for the specified languages
*
* @param {string[]} languages
*/
async function getLanguageCodes(...languages) {
let supportedLanguages = await getSupportedLanguages();
return languages.map(language =>
language &&
supportedLanguages
.find(availableLanguage =>
availableLanguage.languageCode === language
|| availableLanguage.displayName === language
|| availableLanguage.displayName.startsWith(language)
)
|| { languageCode: language }
).map(l => l.languageCode);
}
/**
* Called when a message arrives at the node.
*
* @param {{ payload: any; }} msg
*/
async function Input(msg) {
const [sourceLanguageCode, targetLanguageCode] = await getLanguageCodes(
RED.util.evaluateNodeProperty(
config.sourceLanguageCode,
sourceLanguageCodeType,
node,
msg
) || "",
RED.util.evaluateNodeProperty(
config.targetLanguageCode,
targetLanguageCodeType,
node,
msg
),
);
const request = {
"contents": [msg.payload],
"sourceLanguageCode": sourceLanguageCode,
"targetLanguageCode": targetLanguageCode,
"mimeType": "text/plain",
"parent": translationServiceClient.locationPath(projectId, "global")
};
try {
const [response] = await translationServiceClient.translateText(request);
msg.payload = outputFormat === "full"
? response
: response.translations[0].translatedText;
node.send(msg);
}
catch (e) {
node.error(e, msg);
}
} // Input
// We must have EITHER credentials or a keyFilename. If neither are supplied, that
// is an error. If both are supplied, then credentials will be used.
if (credentials) {
translationServiceClient = new translate.v3beta1.TranslationServiceClient({
"credentials": credentials
});
} else if (keyFilename) {
translationServiceClient = new translate.v3beta1.TranslationServiceClient({
"keyFilename": keyFilename
});
} else {
translationServiceClient = new translate.v3beta1.TranslationServiceClient({});
}
node.on("input", Input);
} // TranslateNode
RED.nodes.registerType(NODE_TYPE, TranslateNode);
};