Browse Source

解决下载语言功能存在的问题

tanghongbin 5 năm trước cách đây
mục cha
commit
a8772185f0
2 tập tin đã thay đổi với 85 bổ sung35 xóa
  1. 84 34
      controller/LangArea.py
  2. 1 1
      controller/LangWord.py

+ 84 - 34
controller/LangArea.py

@@ -190,64 +190,114 @@ class LangAreaView(TemplateView):
 
     def do_download_avss(self, id):
         content = ''
-        result = {}
-        lkc_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=(1, 2)).values('word_key', 'langkeyclassmodel__clazz__name', 'langvalmodel__word_val', 'langlocationmodel__filename', 'langlocationmodel__line')
-        if lkc_qs.exists():
-            for lkc in lkc_qs:
-                # print(lkc)
-                name = lkc['langkeyclassmodel__clazz__name']
-                hasClass = result.__contains__(name)
-                if hasClass is False:
-                    result[name] = {}
-
-                source = lkc['word_key']
-                if not result[name].__contains__(source):
-                    result[name][source] = {}
-                message = result[name][source]
-
-                message['source'] = source
-                message['translation'] = lkc['langvalmodel__word_val']
-
-                # location
-                locations = []
-                locations.append({'filename': lkc['langlocationmodel__filename'], 'line': lkc['langlocationmodel__line']})
-                message['locations'] = locations
-            print(result)
-
-            content += '<TS>\n'
-            keys = result.keys()
+        en_result = self.get_area_language(20)
+        target_result = self.get_area_language(id)
+        if target_result:
+            content += '<TS version=\"2.1\" language=\"' + self.get_avss_language(int(id)) + '\">\n'
+            # keys = result.keys()
+            keys = en_result.keys()
             for key in keys:
                 content += '<context>\n<name>'
                 content += key
                 content += '</name>\n'
 
-                messages = result[key]
-                messageKeys = messages.keys()
+                messages = None
+                if target_result.__contains__(key):
+                    messages = target_result[key]
+                else:
+                    messages = en_result[key]
+                messageKeys = en_result[key].keys()
                 for messageKey in messageKeys:
-                    message = messages[messageKey]
+                    message = None
+                    if messages.__contains__(messageKey):
+                        message = messages[messageKey]
+                    else:
+                        message = en_result[key][messageKey]
+
+                    # message = messages[messageKey]
                     content += '<message>\n'
                     content += '<source>'
                     content += message['source']
                     content += '</source>\n'
                     content += '<translation>'
-                    content += message['translation']
+                    translation = message['translation']
+                    # if translation is None or translation == '':
+                    #     content += '23333'
+                    # else:
+                    content += translation
                     content += '</translation>\n'
-
                     locations = message['locations']
                     print(message)
                     for location in locations:
                         if location['filename'] != '':
-                            content += '<location filename=\"' + location['filename'] + '\" line=\"' + str(location['line']) + '\"\n/>'
+                            content += '<location filename=\"' + location['filename'] + '\" line=\"' + str(location['line']) + '\"/>\n'
                     content += '</message>\n'
-                content += '</TS>'
+                content += '</context>\n'
+            content += '</TS>'
             return content
         else:
             print('none')
 
         return content
 
+    def get_area_language(self, id):
+        result = {}
+        lkc_qs = LangKeyModel.objects.filter(langvalmodel__la__id=id, type__in=(1, 2)).values('word_key',
+                                                                                                 'langkeyclassmodel__clazz__name',
+                                                                                                 'langvalmodel__word_val',
+                                                                                                 'langlocationmodel__filename',
+                                                                                                 'langlocationmodel__line')
+        for lkc in lkc_qs:
+            # print(lkc)
+            name = lkc['langkeyclassmodel__clazz__name']
+            hasClass = result.__contains__(name)
+            if hasClass is False:
+                result[name] = {}
 
-from django.http import StreamingHttpResponse
+            source = lkc['word_key']
+            if not result[name].__contains__(source):
+                result[name][source] = {}
+            message = result[name][source]
+
+            message['source'] = source
+            message['translation'] = lkc['langvalmodel__word_val']
+
+            # location
+            locations = []
+            locations.append({'filename': lkc['langlocationmodel__filename'], 'line': lkc['langlocationmodel__line']})
+            message['locations'] = locations
+        print(result)
+        return result
+
+    def get_avss_language(self, id):
+        if id == 18:  # 简体中文
+            return 'zh_CN'
+        elif id == 19:  # 繁体中文
+            return 'zh_HK'
+        elif id == 20:  # 英文
+            return 'en_US'
+        elif id == 21:  # 俄语
+            return 'ru'
+        elif id == 22:  # 德语
+            return 'de'
+        elif id == 23:  # 波兰语
+            return 'pl'
+        elif id == 24:  # 法语
+            return 'fr'
+        elif id == 25:  # 西班牙语
+            return 'es'
+        elif id == 26:  # 日语
+            return 'ja'
+        elif id == 27:  # 阿拉伯语
+            return 'ar'
+        elif id == 29:  # 意大利语
+            return 'it'
+        elif id == 30:  # 葡萄牙语
+            return 'pt'
+        elif id == 31:  # 荷兰语
+            return 'nl'
+        elif id == 32:  # 越南语
+            return 'vi'
 
 
 class exportLangView(TemplateView):

+ 1 - 1
controller/LangWord.py

@@ -238,7 +238,7 @@ class LangWordView(TemplateView):
         if word_key:
             word_key_list = word_key.split(',')
 
-            #获取该用户可以修改的语种
+            # 获取该用户可以修改的语种
             lp_qs = LangPermModel.objects.filter(user_id=userID).values_list('lang__id')
             lq_list = list(lp_qs)
             # ls_qs = LangSetModel.objects.filter(user_id=userID, lang_id__in=lq_list).values_list('lang__id')