-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.js
338 lines (313 loc) · 9.96 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* translate
*
* This shared library for translating any event's properties to other languages.
* The google function uses the google translation api to translate from
* the source language to other languages.
* The pirate function may be used as a example of creating your own translation
* library if you have custom translations that are needed. Simply update the dictionary.
*
* Exposed methods:
* google(payload, languages) - translates the properties provided in a standard event payload (https://help.xmatters.com/xmapi/index.html#trigger-an-event)
* to multiple languages.
* payload - object - xMatters event payload with a 'properties' property to be updated with translated properties.
* property names are the original appended with -language id
* languages - array - array of language identifiers as strings as defined by google: https://cloud.google.com/translate/docs/languages
*
* NOTE: Requires defined "Google Translation" endpoint. Example:
* Name: Google Translate
* Base URL: https://translation.googleapis.com/language/translate/v2?key=YOUR_KEY_HERE
* Auth Type: None
*
* pirate(payload) - Utilizes a custom library to translate all properties into 'pirate'
* parameter1 - string - The name of stuff to get
*
* Usage:
Example Inbound Service Transform Script -->
//pull in shared library (make sure the shared library is named translate)
var translate = require('translate');
var payload = JSON.parse(request.body); //Use this if the payload is a JSON object in the request body
var languages = ["en", "fr", "zh-cn", "is", "es"];
//calling both functions will result in extraneous translated properties.
payload = translate.google(payload, languages);
//payload = translate.pirate(payload);
form.post(payload);
*/
/*
* This function will take a normal xMatters event payload and converts it to multiple languages.
* Languages supported: https://cloud.google.com/translate/docs/languages
* languages should be an array of google language codes: ["en", "es", "is"]
*
* Requires "Google Translation" endpoint as defined here https://cloud.google.com/translate/docs/translating-text#translate_translate_text-protocol
*/
exports.google = function(payload, languages) {
if (languages === undefined || languages.length === 0) {
// languages not set, returning receieved payload.
return payload;
}
//extract properties from payload in source language
var properties = payload.properties;
//for each language
for (i = 0; i < languages.length; i++) {
var language = languages[i];
//Build language translation request body
var requestPayload = {
target: language,
q: []
};
//add properties to translate
for (var property in properties) {
if (properties.hasOwnProperty(property)) {
var element = properties[property];
requestPayload.q.push(element);
}
}
//build and make translation request to google
var getGoogleTranslationRequest = http.request({
endpoint: 'Google Translation',
method: 'POST'
});
var googleResponse = getGoogleTranslationRequest.write(requestPayload);
//write translated properties back to payload.properties, use order to identify property
var translations = JSON.parse(googleResponse.body).data.translations;
var translationCounter = 0;
for (var _property in properties) {
if (properties.hasOwnProperty(_property)) {
if (googleResponse.statusCode != 200) {
payload.properties[_property + '-' + language] = properties[_property];
} else {
payload.properties[_property + '-' + language] =
translations[translationCounter].translatedText;
}
translationCounter += 1;
}
}
}
return payload;
};
/*
* returns event payload with new properties translated to pirate.
* new properties are name the same as name amended with -pr
*
*/
exports.pirate = function(payload) {
//extract properties from payload
var properties = payload.properties;
//translate properties
for (var property in properties) {
if (properties.hasOwnProperty(property)) {
var element = properties[property];
payload.properties[property + '-pr'] = translate(element);
}
}
return payload;
//pirate translation code sourced from: https://github.com/mikewesthad/pirate-speak/blob/master/lib/pirate-speak.js
//modified by xMatters 2018-Oct-23
//license: MIT
function translateWord(word) {
var dictionary = {
address: "port o' call",
admin: 'helm',
am: 'be',
an: 'a',
and: "n'",
are: 'be',
award: 'prize',
bathroom: 'head',
beer: 'grog',
before: 'afore',
belief: 'creed',
between: 'betwixt',
big: 'vast',
bill: 'coin',
bills: 'coins',
boss: 'admiral',
bourbon: 'rum',
box: 'barrel',
boy: 'lad',
buddy: 'mate',
business: 'company',
businesses: 'companies',
calling: "callin'",
canada: 'Great North',
cash: 'coin',
cat: 'parrot',
cheat: 'hornswaggle',
comes: 'hails',
comments: 'yer words',
cool: 'shipshape',
country: 'land',
dashboard: 'shanty',
dead: "in Davy Jones's Locker",
disconnect: 'keelhaul',
do: "d'",
dog: 'parrot',
dollar: 'doubloon',
dollars: 'doubloons',
dude: 'pirate',
employee: 'crew',
everyone: 'all hands',
eye: 'eye-patch',
family: 'kin',
fee: 'debt',
female: 'wench',
females: 'wenches',
food: 'grub',
for: 'fer',
friend: 'shipmate',
friends: 'crew',
gin: 'rum',
girl: 'lass',
girls: 'lassies',
go: 'sail',
good: 'jolly good',
grave: "Davy Jones's Locker",
group: 'maties',
gun: 'bluderbuss',
haha: 'yo ho',
hahaha: 'yo ho ho',
hahahaha: 'yo ho ho ho',
hand: 'hook',
happy: 'grog-filled',
hello: 'ahoy',
hey: 'ahoy',
hi: 'ahoy',
hotel: 'fleebag inn',
i: 'me',
"i'm": 'i be',
internet: "series o' tubes",
invalid: 'sunk',
is: 'be',
island: 'isle',
"isn't": 'be not',
"it's": "'tis",
jail: 'brig',
kill: 'keelhaul',
king: 'king',
ladies: 'lasses',
lady: 'lass',
lawyer: 'scurvy land lubber',
left: 'port',
leg: 'peg',
logout: 'walk the plank',
lol: 'blimey',
male: 'pirate',
man: 'pirate',
manager: 'admiral',
money: 'doubloons',
month: 'moon',
my: 'me',
never: 'nary',
no: 'nay',
not: 'nay',
of: "o'",
old: 'barnacle-covered',
omg: 'shiver me timbers',
over: "o'er",
page: 'parchment',
people: 'scallywags',
person: 'scurvy dog',
posted: 'tacked to the yardarm',
president: 'king',
prison: 'brig',
quickly: 'smartly',
really: 'verily',
relative: 'kin',
relatives: 'kin',
religion: 'creed',
restaurant: 'galley',
right: 'starboard',
rotf: "rollin' on the decks",
say: 'cry',
seconds: "ticks o' tha clock",
shipping: 'cargo',
small: 'puny',
snack: 'grub',
soldier: 'sailor',
sorry: 'yarr',
spouse: "ball 'n' chain",
state: 'land',
supervisor: "Cap'n",
"that's": 'that be',
the: 'thar',
theif: 'swoggler',
them: "'em",
this: 'dis',
to: "t'",
together: "t'gether",
treasure: 'booty',
vodka: 'rum',
was: 'be',
water: 'grog',
we: 'our jolly crew',
"we're": "we's",
whiskey: 'rum',
whisky: 'rum',
wine: 'grog',
with: "wit'",
woman: 'comely wench',
women: 'beauties',
work: 'duty',
yah: 'aye',
yeah: 'aye',
yes: 'aye',
you: 'ye',
"you're": 'you be',
"you've": 'ye',
your: 'yer',
yup: 'aye'
};
var pirateWord = dictionary[word.toLowerCase()];
if (pirateWord === undefined) return word;
else return applyCase(word, pirateWord);
}
// Take the case from wordA and apply it to wordB
function applyCase(wordA, wordB) {
// Exception to avoid words like "I" being converted to "ME"
if (wordA.length === 1 && wordB.length !== 1) return wordB;
// Uppercase
if (wordA === wordA.toUpperCase()) return wordB.toUpperCase();
// Lowercase
if (wordA === wordA.toLowerCase()) return wordB.toLowerCase();
// Capitialized
var firstChar = wordA.slice(0, 1);
var otherChars = wordA.slice(1);
if (firstChar === firstChar.toUpperCase() && otherChars === otherChars.toLowerCase()) {
return wordB.slice(0, 1).toUpperCase() + wordB.slice(1).toLowerCase();
}
// Other cases
return wordB;
}
function isLetter(character) {
if (character.search(/[a-zA-Z'-]/) === -1) return false;
return true;
}
function translate(text) {
var translatedText = '';
// Loop through the text, one character at a time.
var word = '';
for (var i = 0; i < text.length; i += 1) {
var character = text[i];
// If the char is a letter, then we are in the middle of a word, so we
// should accumulate the letter into the word variable
if (isLetter(character)) {
word += character;
}
// If the char is not a letter, then we hit the end of a word, so we
// should translate the current word and add it to the translation
else {
if (word !== '') {
// If we've just finished a word, translate it
var pirateWord = translateWord(word);
translatedText += pirateWord;
word = '';
}
translatedText += character; // Add the non-letter character
}
}
// If we ended the loop before translating a word, then translate the final
// word and add it to the translation.
if (word !== '') translatedText += translateWord(word);
return translatedText;
}
};