当前位置:   article > 正文

python:基于flask&有道智云API在线翻译网页_falsk求一个翻译网站

falsk求一个翻译网站

 基于 Flask 的网页翻译系统,用户可以在页面中输入需要翻译的文本,并选择翻译语言,系统将通过有道智云API返回翻译结果。

系统应该支持多种语言的翻译,能够准确地翻译文本,并将翻译结果返回给用户。用户应该能够在界面中看到翻译的结果,并可以进行复制或分享。

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>基于flask&amp;有道智云API在线翻译</title>
  5. <style>
  6. body {
  7. font-family: Arial, Helvetica, sans-serif;
  8. margin: 0;
  9. padding: 0;
  10. background-color: #f2f2f2;
  11. }
  12. h1 {
  13. text-align: center;
  14. margin-top: 50px;
  15. font-size: 36px;
  16. }
  17. form {
  18. margin-top: 30px;
  19. max-width: 100%;
  20. margin-left: auto;
  21. margin-right: auto;
  22. background-color: #fff;
  23. padding: 20px;
  24. border-radius: 10px;
  25. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  26. }
  27. label {
  28. font-weight: bold;
  29. }
  30. textarea {
  31. width: 100%;
  32. margin-top: 5px;
  33. margin-bottom: 15px;
  34. border-radius: 5px;
  35. padding: 5px;
  36. }
  37. select {
  38. margin-top: 5px;
  39. margin-bottom: 15px;
  40. border-radius: 5px;
  41. padding: 5px;
  42. }
  43. input[type="submit"] {
  44. display: block;
  45. margin-top: 20px;
  46. margin-left: auto;
  47. margin-right: auto;
  48. padding: 10px 20px;
  49. font-size: 18px;
  50. font-weight: bold;
  51. text-transform: uppercase;
  52. color: #fff;
  53. background-color: #4CAF50;
  54. border-radius: 5px;
  55. border: none;
  56. cursor: pointer;
  57. transition: all 0.3s ease;
  58. }
  59. input[type="submit"]:hover {
  60. background-color: #3E8E41;
  61. }
  62. h2 {
  63. margin-top: 30px;
  64. font-size: 24px;
  65. }
  66. p {
  67. margin-top: 10px;
  68. font-size: 18px;
  69. font-weight: bold;
  70. background-color: #D5F5E3;
  71. padding: 10px;
  72. border-radius: 5px;
  73. border: 1px solid #4CAF50;
  74. }
  75. m {
  76. margin-top: 10px;
  77. font-size: 18px;
  78. font-weight: bold;
  79. background-color: #f5f5f5;
  80. padding: 10px;
  81. border-radius: 5px;
  82. }
  83. </style>
  84. </head>
  85. <body>
  86. <h1>基于flask&amp;有道智云API在线翻译</h1>
  87. <form method="post" action="/">
  88. <label for="input_text">需要翻译的文本:</label><br>
  89. <textarea id="input_text" name="input_text" rows="5" cols="40">{% if input_text %}{{ input_text }}{% endif %}</textarea><br>
  90. <label for="target_language">需要翻译的语言:</label><br>
  91. <select id="target_language" name="target_language">
  92. <option value="zh-CHS" {% if target_language == "zh-CHS" %}selected{% endif %}>Chinese Simplified</option>
  93. <option value="zh-CHT" {% if target_language == "zh-CHT" %}selected{% endif %}>Chinese Traditional</option>
  94. <option value="en" {% if target_language == "en" %}selected{% endif %}>English</option>
  95. <option value="ja" {% if target_language == "ja" %}selected{% endif %}>Japanese</option>
  96. <option value="ko" {% if target_language == "ko" %}selected{% endif %}>Korean</option>
  97. <option value="fr" {% if target_language == "fr" %}selected{% endif %}>French</option>
  98. <option value="es" {% if target_language == "es" %}selected{% endif %}>Spanish</option>
  99. <option value="pt" {% if target_language == "pt" %}selected{% endif %}>Portuguese</option>
  100. <option value="it" {% if target_language == "it" %}selected{% endif %}>Italian</option>
  101. <option value="ru" {% if target_language == "ru" %}selected{% endif %}>Russian</option>
  102. <option value="vi" {% if target_language == "vi" %}selected{% endif %}>Vietnamese</option>
  103. <option value="de" {% if target_language == "de" %}selected{% endif %}>German</option>
  104. <option value="ar" {% if target_language == "ar" %}selected{% endif %}>Arabic</option>
  105. <option value="id" {% if target_language == "id" %}selected{% endif %}>Indonesian</option>
  106. <option value="af" {% if target_language == "af" %}selected{% endif %}>Afrikaans</option>
  107. <option value="bs" {% if target_language == "bs" %}selected{% endif %}>Bosnian</option>
  108. <option value="bg" {% if target_language == "bg" %}selected{% endif %}>Bulgarian</option>
  109. <option value="yue" {% if target_language == "yue" %}selected{% endif %}>Cantonese</option>
  110. <option value="ca" {% if target_language == "ca" %}selected{% endif %}>Catalan</option>
  111. <option value="hr" {% if target_language == "hr" %}selected{% endif %}>Croatian</option>
  112. <option value="cs" {% if target_language == "cs" %}selected{% endif %}>Czech</option>
  113. <option value="da" {% if target_language == "da" %}selected{% endif %}>Danish</option>
  114. <option value="nl" {% if target_language == "nl" %}selected{% endif %}>Dutch</option>
  115. <option value="et" {% if target_language == "et" %}selected{% endif %}>Estonian</option>
  116. <option value="fj" {% if target_language == "fj" %}selected{% endif %}>Fijian</option>
  117. <option value="fi" {% if target_language == "fi" %}selected{% endif %}>Finnish</option>
  118. <option value="el" {% if target_language == "el" %}selected{% endif %}>Greek</option>
  119. <option value="ht" {% if target_language == "ht" %}selected{% endif %}>Haitian Creole</option>
  120. <option value="he" {% if target_language == "he" %}selected{% endif %}>Hebrew</option>
  121. <option value="hi" {% if target_language == "hi" %}selected{% endif %}>Hindi</option>
  122. <option value="mww" {% if target_language == "mww" %}selected{% endif %}>Hmong Daw</option>
  123. <option value="hu" {% if target_language == "hu" %}selected{% endif %}>Hungarian</option>
  124. <option value="sw" {% if target_language == "sw" %}selected{% endif %}>Swahili</option>
  125. <option value="tlh" {% if target_language == "tlh" %}selected{% endif %}>Klingon</option>
  126. <option value="lv" {% if target_language == "lv" %}selected{% endif %}>Latvian</option>
  127. <option value="lt" {% if target_language == "lt" %}selected{% endif %}>Lithuanian</option>
  128. <option value="ms" {% if target_language == "ms" %}selected{% endif %}>Malay</option>
  129. <option value="mt" {% if target_language == "mt" %}selected{% endif %}>Maltese</option>
  130. <option value="no" {% if target_language == "no" %}selected{% endif %}>Norwegian</option>
  131. <option value="fa" {% if target_language == "fa" %}selected{% endif %}>Persian</option>
  132. <option value="pl" {% if target_language == "pl" %}selected{% endif %}>Polish</option>
  133. <option value="otq" {% if target_language == "otq" %}selected{% endif %}>Queretaro Otomi</option>
  134. <option value="ro" {% if target_language == "ro" %}selected{% endif %}>Romanian</option>
  135. <option value="sr-Cyrl" {% if target_language == "sr-Cyrl" %}selected{% endif %}>Serbian (Cyrillic)</option>
  136. <option value="sr-Latn" {% if target_language == "sr-Latn" %}selected{% endif %}>Serbian (Latin)</option>
  137. <option value="sk" {% if target_language == "sk" %}selected{% endif %}>Slovak</option>
  138. <option value="sl" {% if target_language == "sl" %}selected{% endif %}>Slovenian</option>
  139. <option value="sv" {% if target_language == "sv" %}selected{% endif %}>Swedish</option>
  140. <option value="ty" {% if target_language == "ty" %}selected{% endif %}>Tahitian</option>
  141. <option value="th" {% if target_language == "th" %}selected{% endif %}>Thai</option>
  142. <option value="to" {% if target_language == "to" %}selected{% endif %}>Tongan</option>
  143. <option value="tr" {% if target_language == "tr" %}selected{% endif %}>Turkish</option>
  144. <option value="uk" {% if target_language == "uk" %}selected{% endif %}>Ukrainian</option>
  145. <option value="ur" {% if target_language == "ur" %}selected{% endif %}>Urdu</option>
  146. <option value="cy" {% if target_language == "cy" %}selected{% endif %}>Welsh</option>
  147. <option value="yua" {% if target_language == "yua" %}selected{% endif %}>Yucatec Maya</option>
  148. <option value="sq" {% if target_language == "sq" %}selected{% endif %}>Albanian</option>
  149. <option value="am" {% if target_language == "am" %}selected{% endif %}>Amharic</option>
  150. <option value="hy" {% if target_language == "hy" %}selected{% endif %}>Armenian</option>
  151. <option value="az" {% if target_language == "az" %}selected{% endif %}>Azerbaijani</option>
  152. <option value="bn" {% if target_language == "bn" %}selected{% endif %}>Bengali</option>
  153. <option value="eu" {% if target_language == "eu" %}selected{% endif %}>Basque</option>
  154. <option value="be" {% if target_language == "be" %}selected{% endif %}>Belarusian</option>
  155. <option value="ceb" {% if target_language == "ceb" %}selected{% endif %}>Cebuano</option>
  156. <option value="co" {% if target_language == "co" %}selected{% endif %}>Corsican</option>
  157. <option value="eo" {% if target_language == "eo" %}selected{% endif %}>Esperanto</option>
  158. <option value="tl" {% if target_language == "tl" %}selected{% endif %}>Filipino</option>
  159. <option value="fy" {% if target_language == "fy" %}selected{% endif %}>Frisian</option>
  160. <option value="gl" {% if target_language == "gl" %}selected{% endif %}>Galician</option>
  161. <option value="ka" {% if target_language == "ka" %}selected{% endif %}>Georgian</option>
  162. <option value="gu" {% if target_language == "gu" %}selected{% endif %}>Gujarati</option>
  163. <option value="ha" {% if target_language == "ha" %}selected{% endif %}>Hausa</option>
  164. <option value="haw" {% if target_language == "haw" %}selected{% endif %}>Hawaiian</option>
  165. <option value="is" {% if target_language == "is" %}selected{% endif %}>Icelandic</option>
  166. <option value="ig" {% if target_language == "ig" %}selected{% endif %}>Igbo</option>
  167. <option value="ga" {% if target_language == "ga" %}selected{% endif %}>Irish</option>
  168. <option value="jw" {% if target_language == "jw" %}selected{% endif %}>Javanese</option>
  169. <option value="kn" {% if target_language == "kn" %}selected{% endif %}>Kannada</option>
  170. <option value="kk" {% if target_language == "kk" %}selected{% endif %}>Kazakh</option>
  171. <option value="km" {% if target_language == "km" %}selected{% endif %}>Khmer</option>
  172. <option value="ku" {% if target_language == "ku" %}selected{% endif %}>Kurdish</option>
  173. <option value="ky" {% if target_language == "ky" %}selected{% endif %}>Kyrgyz</option>
  174. <option value="lo" {% if target_language == "lo" %}selected{% endif %}>Lao</option>
  175. <option value="la" {% if target_language == "la" %}selected{% endif %}>Latin</option>
  176. <option value="lb" {% if target_language == "lb" %}selected{% endif %}>Luxembourgish</option>
  177. <option value="mk" {% if target_language == "mk" %}selected{% endif %}>Macedonian</option>
  178. <option value="mg" {% if target_language == "mg" %}selected{% endif %}>Malagasy</option>
  179. <option value="ml" {% if target_language == "ml" %}selected{% endif %}>Malayalam</option>
  180. <option value="mi" {% if target_language == "mi" %}selected{% endif %}>Maori</option>
  181. <option value="mr" {% if target_language == "mr" %}selected{% endif %}>Marathi</option>
  182. <option value="mn" {% if target_language == "mn" %}selected{% endif %}>Mongolian</option>
  183. <option value="my" {% if target_language == "my" %}selected{% endif %}>Burmese</option>
  184. <option value="ne" {% if target_language == "ne" %}selected{% endif %}>Nepali</option>
  185. <option value="ny" {% if target_language == "ny" %}selected{% endif %}>Chichewa</option>
  186. <option value="ps" {% if target_language == "ps" %}selected{% endif %}>Pashto</option>
  187. <option value="pa" {% if target_language == "pa" %}selected{% endif %}>Punjabi</option>
  188. <option value="sm" {% if target_language == "sm" %}selected{% endif %}>Samoan</option>
  189. <option value="gd" {% if target_language == "gd" %}selected{% endif %}>Scots Gaelic</option>
  190. <option value="st" {% if target_language == "st" %}selected{% endif %}>Sesotho</option>
  191. <option value="sn" {% if target_language == "sn" %}selected{% endif %}>Shona</option>
  192. <option value="sd" {% if target_language == "sd" %}selected{% endif %}>Sindhi</option>
  193. <option value="si" {% if target_language == "si" %}selected{% endif %}>Sinhala</option>
  194. <option value="so" {% if target_language == "so" %}selected{% endif %}>Somali</option>
  195. <option value="su" {% if target_language == "su" %}selected{% endif %}>Sundanese</option>
  196. <option value="tg" {% if target_language == "tg" %}selected{% endif %}>Tajik</option>
  197. <option value="ta" {% if target_language == "ta" %}selected{% endif %}>Tamil</option>
  198. <option value="te" {% if target_language == "te" %}selected{% endif %}>Telugu</option>
  199. <option value="auto" {% if target_language == "auto" %}selected{% endif %}> auto</option>
  200. </select><br>
  201. <input type="submit" value="翻译">
  202. </form>
  203. {% if translated_text %}
  204. <div class="out">
  205. <m>原始文本:</m>
  206. <p>{{ input_text }}</p>
  207. <m>翻译语言:</m>
  208. <p>{{ target_language }}</p>
  209. <m>翻译结果:</m>
  210. <p>{{ translated_text }}</p>
  211. </div>
  212. {% endif %}
  213. </body>
  214. </html>

main.py
 

  1. from flask import Flask, render_template, request
  2. import requests
  3. import json
  4. import hashlib
  5. import random
  6. import time
  7. app = Flask(__name__)
  8. app.config['SECRET_KEY'] = 'dev'
  9. # 有道翻译 API 服务器端点,需要先申请App Key和App Secret
  10. api_endpoint = "https://openapi.youdao.com/api"
  11. app_key = "自己获取"
  12. app_secret = "自己获取"
  13. def generate_sign_params(query):
  14. """
  15. 使用app_key和app_secret生成有道翻译API请求参数中的sign参数
  16. """
  17. salt = str(random.randint(1, 65536))
  18. sign_param = app_key + query + salt + app_secret
  19. md5 = hashlib.md5()
  20. md5.update(sign_param.encode('utf-8'))
  21. sign = md5.hexdigest()
  22. return {'sign': sign, 'salt': salt}
  23. @app.route('/', methods=['GET', 'POST'])
  24. def index():
  25. if request.method == 'POST':
  26. # 处理表单提交请求
  27. input_text = request.form['input_text']
  28. target_language = request.form['target_language']
  29. # 检测输入语言
  30. detect_params = generate_sign_params(input_text)
  31. detect_params.update({
  32. 'q': input_text,
  33. 'from': 'auto',
  34. 'to': 'auto',
  35. 'appKey': app_key
  36. })
  37. detect_response = requests.get(api_endpoint, params=detect_params)
  38. if detect_response.status_code != 200:
  39. # 如果响应状态码不是200,则说明请求出错
  40. return 'Error: Could not retrieve language.'
  41. detected_language = detect_response.json().get('lang')
  42. print(str(detected_language))
  43. # 调用翻译 API 进行翻译
  44. translate_params = generate_sign_params(input_text)
  45. translate_params.update({
  46. 'q': input_text,
  47. 'from': detected_language,
  48. 'to': target_language,
  49. 'appKey': app_key
  50. })
  51. translate_response = requests.get(api_endpoint, params=translate_params)
  52. if translate_response.status_code != 200:
  53. # 如果响应状态码不是200,则说明请求出错
  54. return 'Error: Could not translate the text.'
  55. # 处理翻译结果并返回
  56. translated_text = translate_response.json().get('translation')[0]
  57. return render_template('index.html', input_text=input_text,target_language=target_language,translated_text=translated_text)
  58. # 渲染首页
  59. return render_template('index.html')
  60. if __name__ == '__main__':
  61. app.run(debug=True)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/372192
推荐阅读
相关标签
  

闽ICP备14008679号