|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | +</head> |
| 5 | +<body> |
| 6 | +<textarea id="input_area" name="message" rows="10" cols="100">ลองทำการแปลภาษา</textarea> |
| 7 | +<br/> |
| 8 | +<br/> |
| 9 | +<button onclick="submit_translate()">แปลภาษา</button> |
| 10 | + จาก <select id="select_language_from"></select> |
| 11 | + เป็น <select id="select_language_to"></select> |
| 12 | + |
| 13 | +<br> |
| 14 | +<br/> |
| 15 | +<textarea id="translated_area" name="message" rows="10" cols="100"></textarea> |
| 16 | + |
| 17 | +<script> |
| 18 | + |
| 19 | + |
| 20 | +// The languages that Google Translate supports ISO 639-1 codes |
| 21 | +// See more https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes |
| 22 | +var langs = { |
| 23 | + //'auto': 'Automatic', |
| 24 | + 'af': 'Afrikaans', |
| 25 | + 'sq': 'Albanian', |
| 26 | + 'am': 'Amharic', |
| 27 | + 'ar': 'Arabic', |
| 28 | + 'hy': 'Armenian', |
| 29 | + 'az': 'Azerbaijani', |
| 30 | + 'eu': 'Basque', |
| 31 | + 'be': 'Belarusian', |
| 32 | + 'bn': 'Bengali', |
| 33 | + 'bs': 'Bosnian', |
| 34 | + 'bg': 'Bulgarian', |
| 35 | + 'ca': 'Catalan', |
| 36 | + 'ceb': 'Cebuano', |
| 37 | + 'ny': 'Chichewa', |
| 38 | + 'zh-cn': 'Chinese Simplified', |
| 39 | + 'zh-tw': 'Chinese Traditional', |
| 40 | + 'co': 'Corsican', |
| 41 | + 'hr': 'Croatian', |
| 42 | + 'cs': 'Czech', |
| 43 | + 'da': 'Danish', |
| 44 | + 'nl': 'Dutch', |
| 45 | + 'en': 'English', |
| 46 | + 'eo': 'Esperanto', |
| 47 | + 'et': 'Estonian', |
| 48 | + 'tl': 'Filipino', |
| 49 | + 'fi': 'Finnish', |
| 50 | + 'fr': 'French', |
| 51 | + 'fy': 'Frisian', |
| 52 | + 'gl': 'Galician', |
| 53 | + 'ka': 'Georgian', |
| 54 | + 'de': 'German', |
| 55 | + 'el': 'Greek', |
| 56 | + 'gu': 'Gujarati', |
| 57 | + 'ht': 'Haitian Creole', |
| 58 | + 'ha': 'Hausa', |
| 59 | + 'haw': 'Hawaiian', |
| 60 | + 'iw': 'Hebrew', |
| 61 | + 'hi': 'Hindi', |
| 62 | + 'hmn': 'Hmong', |
| 63 | + 'hu': 'Hungarian', |
| 64 | + 'is': 'Icelandic', |
| 65 | + 'ig': 'Igbo', |
| 66 | + 'id': 'Indonesian', |
| 67 | + 'ga': 'Irish', |
| 68 | + 'it': 'Italian', |
| 69 | + 'ja': 'Japanese', |
| 70 | + 'jw': 'Javanese', |
| 71 | + 'kn': 'Kannada', |
| 72 | + 'kk': 'Kazakh', |
| 73 | + 'km': 'Khmer', |
| 74 | + 'ko': 'Korean', |
| 75 | + 'ku': 'Kurdish (Kurmanji)', |
| 76 | + 'ky': 'Kyrgyz', |
| 77 | + 'lo': 'Lao', |
| 78 | + 'la': 'Latin', |
| 79 | + 'lv': 'Latvian', |
| 80 | + 'lt': 'Lithuanian', |
| 81 | + 'lb': 'Luxembourgish', |
| 82 | + 'mk': 'Macedonian', |
| 83 | + 'mg': 'Malagasy', |
| 84 | + 'ms': 'Malay', |
| 85 | + 'ml': 'Malayalam', |
| 86 | + 'mt': 'Maltese', |
| 87 | + 'mi': 'Maori', |
| 88 | + 'mr': 'Marathi', |
| 89 | + 'mn': 'Mongolian', |
| 90 | + 'my': 'Myanmar (Burmese)', |
| 91 | + 'ne': 'Nepali', |
| 92 | + 'no': 'Norwegian', |
| 93 | + 'ps': 'Pashto', |
| 94 | + 'fa': 'Persian', |
| 95 | + 'pl': 'Polish', |
| 96 | + 'pt': 'Portuguese', |
| 97 | + 'ma': 'Punjabi', |
| 98 | + 'ro': 'Romanian', |
| 99 | + 'ru': 'Russian', |
| 100 | + 'sm': 'Samoan', |
| 101 | + 'gd': 'Scots Gaelic', |
| 102 | + 'sr': 'Serbian', |
| 103 | + 'st': 'Sesotho', |
| 104 | + 'sn': 'Shona', |
| 105 | + 'sd': 'Sindhi', |
| 106 | + 'si': 'Sinhala', |
| 107 | + 'sk': 'Slovak', |
| 108 | + 'sl': 'Slovenian', |
| 109 | + 'so': 'Somali', |
| 110 | + 'es': 'Spanish', |
| 111 | + 'su': 'Sundanese', |
| 112 | + 'sw': 'Swahili', |
| 113 | + 'sv': 'Swedish', |
| 114 | + 'tg': 'Tajik', |
| 115 | + 'ta': 'Tamil', |
| 116 | + 'te': 'Telugu', |
| 117 | + 'th': 'Thai', |
| 118 | + 'tr': 'Turkish', |
| 119 | + 'uk': 'Ukrainian', |
| 120 | + 'ur': 'Urdu', |
| 121 | + 'uz': 'Uzbek', |
| 122 | + 'vi': 'Vietnamese', |
| 123 | + 'cy': 'Welsh', |
| 124 | + 'xh': 'Xhosa', |
| 125 | + 'yi': 'Yiddish', |
| 126 | + 'yo': 'Yoruba', |
| 127 | + 'zu': 'Zulu' |
| 128 | +}; |
| 129 | + |
| 130 | +key_langs = Object.keys(langs) |
| 131 | +for (var i = 0; i < key_langs.length; i++) { |
| 132 | + select_language_from.options[i] = new Option(langs[key_langs[i]], i); |
| 133 | + select_language_to.options[i] = new Option(langs[key_langs[i]], i); |
| 134 | + select_language_from.selectedIndex = 93 |
| 135 | + select_language_to.selectedIndex = 21 |
| 136 | +} |
| 137 | + |
| 138 | +function translate(sentences, targetDiv, from_lang ='th', to_lang='en'){ |
| 139 | + /*if (language == 'english') { |
| 140 | + endPoint = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=ar&tl=en&dt=t&ie=UTF-8&oe=UTF-8&q=" |
| 141 | + } */ |
| 142 | + |
| 143 | + sentences = sentences.replace(/\n/g, '<br>') |
| 144 | + let endPoint = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=${from_lang}&tl=${to_lang}&dt=t&ie=UTF-8&oe=UTF-8&q=${encodeURIComponent(sentences)}` ; |
| 145 | + |
| 146 | + var xhttp = new XMLHttpRequest(); |
| 147 | + xhttp.onreadystatechange = function() { |
| 148 | + if (this.readyState == 4 && this.status == 200) { |
| 149 | + var jsonText = JSON.parse(this.responseText); |
| 150 | + text = jsonText[0][0][0] |
| 151 | + text = text.replace(/<br>/g, '\n') |
| 152 | + targetDiv.innerHTML = " " + text; |
| 153 | + } |
| 154 | + }; |
| 155 | + xhttp.open("GET", endPoint, true); |
| 156 | + xhttp.send(); |
| 157 | +} |
| 158 | + |
| 159 | +function submit_translate(){ |
| 160 | + from_lang = key_langs[select_language_from.value] |
| 161 | + to_lang = key_langs[select_language_to.value] |
| 162 | + console.log("translate from: ", langs[from_lang] , " to ", langs[to_lang]) |
| 163 | + translate(input_area.value, |
| 164 | + translated_area, |
| 165 | + from_lang=from_lang, |
| 166 | + to_lang=to_lang) |
| 167 | +} |
| 168 | + |
| 169 | +</script> |
| 170 | +</body> |
| 171 | + |
| 172 | +</html> |
| 173 | + |
0 commit comments