generated from pot-app/pot-app-translate-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
131 lines (113 loc) · 3.38 KB
/
main.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
// SPDX-FileCopyrightText: 2024 Integral <[email protected]>
//
// SPDX-License-Identifier: GPL-3.0-or-later
async function translate(text, from, to, detect, needs = {}) {
try {
const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/${to}/${text}`);
const data = await response.json();
return formatDictionaryEntries(data);
} catch (error) {
throw new Error("Response Parse Error");
}
}
function formatDictionaryEntries(entries) {
if (!Array.isArray(entries) || entries.length === 0) {
return "No entries found.";
}
let result = `${entries[0].word}\n\n`;
let pronunciations = new Set();
let meanings = [];
entries.forEach(entry => {
// Collect unique pronunciations
if (entry.phonetics) {
entry.phonetics.forEach(phonetic => {
let pronunciationInfo = "";
if (phonetic.text) {
pronunciationInfo += phonetic.text;
}
if (phonetic.audio && phonetic.text) {
const region = getRegionFromAudioUrl(phonetic.audio, entry.word);
if (region) {
pronunciationInfo += ` (${region})`;
}
}
if (pronunciationInfo) {
pronunciations.add(pronunciationInfo);
}
});
}
// Collect all meanings
if (entry.meanings) {
meanings = meanings.concat(entry.meanings);
}
});
// Add pronunciations to result
if (pronunciations.size > 0) {
result += "Pronunciations:\n";
pronunciations.forEach(pronunciation => {
result += ` ${pronunciation}\n`;
});
result += "\n";
}
// Add meanings to result
if (meanings.length > 0) {
let partOfSpeechCounter = {};
meanings.forEach((meaning, index) => {
const partOfSpeech = meaning.partOfSpeech.charAt(0).toUpperCase() + meaning.partOfSpeech.slice(1);
partOfSpeechCounter[partOfSpeech] = (partOfSpeechCounter[partOfSpeech] || 0) + 1;
result += `${partOfSpeechCounter[partOfSpeech]}. ${partOfSpeech}\n`;
meaning.definitions.forEach((def, defIndex) => {
result += ` ${defIndex + 1}. ${def.definition}\n`;
if (def.example) {
result += ` Example: "${def.example}"\n`;
}
});
if (meaning.synonyms && meaning.synonyms.length > 0) {
result += ` Synonyms: ${meaning.synonyms.join(", ")}\n`;
}
if (meaning.antonyms && meaning.antonyms.length > 0) {
result += ` Antonyms: ${meaning.antonyms.join(", ")}\n`;
}
result += "\n";
});
}
return result.trim();
}
function getRegionFromAudioUrl(url, word) {
const regionCodes = {
'us': 'American',
'uk': 'British',
'au': 'Australian',
'ca': 'Canadian',
'ie': 'Irish',
'nz': 'New Zealand',
'sa': 'South African',
'in': 'Indian',
'jm': 'Jamaican',
'ph': 'Philippine',
'sg': 'Singaporean',
'ng': 'Nigerian',
'gh': 'Ghanaian',
'ke': 'Kenyan',
'tz': 'Tanzanian',
'zw': 'Zimbabwean',
'hk': 'Hong Kong',
'my': 'Malaysian'
};
const pattern = new RegExp(`${word}-\\d*-?([a-z]{2})\\.mp3`);
const match = url.match(pattern);
if (match && match[1] in regionCodes) {
return regionCodes[match[1]];
}
return null;
}
// Example usage
async function main() {
try {
const result = await translate("implement", "en", "en", "en");
console.log(result);
} catch (error) {
console.error("Error:", error.message);
}
}
main();