Преглед на файлове

210808 translate url fix

yukyo0821 преди 3 години
родител
ревизия
44584c48b7
променени са 3 файла, в които са добавени 14 реда и са изтрити 6 реда
  1. 3 2
      backstage/collections/routes.py
  2. 7 3
      backstage/utils/__init__.py
  3. 4 1
      backstage/utils/routes.py

+ 3 - 2
backstage/collections/routes.py

@@ -38,6 +38,7 @@ def collection_list():
 @collections_app.route('/backstage/collection/create', methods=['POST'])
 def create():
     form = CollectionCreateForm()
+    eng_name = get_trans_title_url_name(form.title.data)
     front_matter = '''---
 title: "{}"\n\
 date: {}\n\
@@ -50,11 +51,11 @@ description: "{}"\n\
               get_now_time(),
               'false',
               'collection',
-              '/collection/{}'.format(get_trans_title_url_name(form.title.data)),
+              '/collection/{}'.format(eng_name),
               form.image.data.filename,
               form.description.data)
     data = {'frontMatter': front_matter,
-            'name': request.form['title'],
+            'name': eng_name,
             'type': 'collection'}
     return create_content(data, form.image.data)
 

+ 7 - 3
backstage/utils/__init__.py

@@ -1,10 +1,14 @@
-from googletrans import Translator
+# coding:utf-8
+from translate import Translator
+#from googletrans import Translator
 from datetime import datetime, timezone, timedelta
 
 
 def translate(text):
-    result = Translator().translate(text, dest='en')
-    return result.text
+    #result = Translator().translate(text, dest='en')
+    translator= Translator(from_lang="zh-tw",to_lang="en")
+    result = translator.translate(text)
+    return str(result).lower()
 
 
 def get_now_time():

+ 4 - 1
backstage/utils/routes.py

@@ -37,7 +37,10 @@ def remove_content():
 
 
 def get_trans_title_url_name(title):
-    return translate(title).replace(' ', '_')
+    tary = translate(title).replace('$','_').replace('&','_').replace('+','_').replace(',','_').replace('/','_').replace(':','_').replace(';','_').replace('=','_').replace('?','_').replace('@','_').replace(' ','_').split('_')
+    while("" in tary) :
+        tary.remove("")
+    return ('_'.join(tary))
     #return translate(title).replace(' ', '_')