JimmyK300 commited on
Commit
a8c0e96
·
verified ·
1 Parent(s): 070aca1

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +141 -26
script.js CHANGED
@@ -1,29 +1,144 @@
1
- document.addEventListener("DOMContentLoaded", () => {
2
- const translateButton = document.querySelector("#translate");
3
- const textInput = document.querySelector("#text");
4
- const fromInput = document.querySelector("#from");
5
- const toInput = document.querySelector("#to");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  const resultDiv = document.querySelector("#result");
7
 
8
- translateButton.addEventListener("click", () => {
9
- const text = textInput.value;
10
- const from = fromInput.value || "auto";
11
- const to = toInput.value || "vi";
12
-
13
- if (!text) {
14
- resultDiv.textContent = "Please enter text to translate.";
15
- return;
16
- }
17
-
18
- const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${from}&tl=${to}&dt=t&q=${encodeURIComponent(text)}`;
19
-
20
- fetch(url)
21
- .then(response => response.json())
22
- .then(data => {
23
- resultDiv.textContent = data[0].map(item => item[0]).join(" ");
24
- })
25
- .catch(error => {
26
- resultDiv.textContent = "Error: " + error.message;
27
- });
28
- });
29
  });
 
1
+ const languages = {
2
+ "auto": "Detect Language",
3
+ "af": "Afrikaans",
4
+ "sq": "Albanian",
5
+ "am": "Amharic",
6
+ "ar": "Arabic",
7
+ "hy": "Armenian",
8
+ "az": "Azerbaijani",
9
+ "eu": "Basque",
10
+ "bn": "Bengali",
11
+ "be": "Belarusian",
12
+ "bs": "Bosnian",
13
+ "bg": "Bulgarian",
14
+ "ca": "Catalan",
15
+ "ceb": "Cebuano",
16
+ "zh-CN": "Chinese (Simplified)",
17
+ "zh-TW": "Chinese (Traditional)",
18
+ "hr": "Croatian",
19
+ "cs": "Czech",
20
+ "da": "Danish",
21
+ "nl": "Dutch",
22
+ "en": "English",
23
+ "eo": "Esperanto",
24
+ "et": "Estonian",
25
+ "tl": "Filipino",
26
+ "fi": "Finnish",
27
+ "fr": "French",
28
+ "gl": "Galician",
29
+ "ka": "Georgian",
30
+ "de": "German",
31
+ "el": "Greek",
32
+ "gu": "Gujarati",
33
+ "ht": "Haitian Creole",
34
+ "ha": "Hausa",
35
+ "he": "Hebrew",
36
+ "hi": "Hindi",
37
+ "hu": "Hungarian",
38
+ "is": "Icelandic",
39
+ "id": "Indonesian",
40
+ "it": "Italian",
41
+ "ja": "Japanese",
42
+ "jw": "Javanese",
43
+ "kn": "Kannada",
44
+ "kk": "Kazakh",
45
+ "km": "Khmer",
46
+ "ko": "Korean",
47
+ "ku": "Kurdish (Kurmanji)",
48
+ "ky": "Kyrgyz",
49
+ "lo": "Lao",
50
+ "la": "Latin",
51
+ "lv": "Latvian",
52
+ "lt": "Lithuanian",
53
+ "lb": "Luxembourgish",
54
+ "mk": "Macedonian",
55
+ "ms": "Malay",
56
+ "ml": "Malayalam",
57
+ "mt": "Maltese",
58
+ "mi": "Maori",
59
+ "mr": "Marathi",
60
+ "mn": "Mongolian",
61
+ "my": "Myanmar (Burmese)",
62
+ "ne": "Nepali",
63
+ "no": "Norwegian",
64
+ "or": "Odia (Oriya)",
65
+ "ps": "Pashto",
66
+ "fa": "Persian",
67
+ "pl": "Polish",
68
+ "pt": "Portuguese",
69
+ "pa": "Punjabi",
70
+ "ro": "Romanian",
71
+ "ru": "Russian",
72
+ "sm": "Samoan",
73
+ "gd": "Scots Gaelic",
74
+ "sr": "Serbian",
75
+ "st": "Sesotho",
76
+ "sn": "Shona",
77
+ "sd": "Sindhi",
78
+ "si": "Sinhala",
79
+ "sk": "Slovak",
80
+ "sl": "Slovenian",
81
+ "so": "Somali",
82
+ "es": "Spanish",
83
+ "su": "Sundanese",
84
+ "sw": "Swahili",
85
+ "sv": "Swedish",
86
+ "tg": "Tajik",
87
+ "ta": "Tamil",
88
+ "te": "Telugu",
89
+ "th": "Thai",
90
+ "tr": "Turkish",
91
+ "uk": "Ukrainian",
92
+ "ur": "Urdu",
93
+ "uz": "Uzbek",
94
+ "vi": "Vietnamese",
95
+ "cy": "Welsh",
96
+ "xh": "Xhosa",
97
+ "yi": "Yiddish",
98
+ "yo": "Yoruba",
99
+ "zu": "Zulu"
100
+ };
101
+
102
+ // Populate language dropdowns
103
+ const fromSelect = document.querySelector("#from");
104
+ const toSelect = document.querySelector("#to");
105
+
106
+ Object.entries(languages).forEach(([code, name]) => {
107
+ const optionFrom = document.createElement("option");
108
+ optionFrom.value = code;
109
+ optionFrom.textContent = name;
110
+ fromSelect.appendChild(optionFrom);
111
+
112
+ const optionTo = document.createElement("option");
113
+ optionTo.value = code;
114
+ optionTo.textContent = name;
115
+ toSelect.appendChild(optionTo);
116
+ });
117
+
118
+ // Set default values
119
+ fromSelect.value = "auto"; // Auto-detect
120
+ toSelect.value = "vi"; // Default to Vietnamese
121
+
122
+ // Translation Function
123
+ document.querySelector("#translate").addEventListener("click", () => {
124
+ const text = document.querySelector("#text").value;
125
+ const fromLang = fromSelect.value;
126
+ const toLang = toSelect.value;
127
  const resultDiv = document.querySelector("#result");
128
 
129
+ if (!text) {
130
+ resultDiv.textContent = "Please enter text to translate.";
131
+ return;
132
+ }
133
+
134
+ const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${fromLang}&tl=${toLang}&dt=t&q=${encodeURIComponent(text)}`;
135
+
136
+ fetch(url)
137
+ .then(response => response.json())
138
+ .then(data => {
139
+ resultDiv.textContent = data[0].map(item => item[0]).join(" ");
140
+ })
141
+ .catch(error => {
142
+ resultDiv.textContent = "Error: " + error.message;
143
+ });
 
 
 
 
 
 
144
  });