Skip to content

Commit

Permalink
add limit 参数,解决 xlf 过大问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lemisky committed Apr 27, 2023
1 parent caddc3e commit d483ff1
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/docts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import argparse
import html
import re
from itertools import islice
from typing import Callable, Pattern, AnyStr, List

from pygtrans import Translate, Null
Expand Down Expand Up @@ -87,13 +88,16 @@ def parse_xlf(xlf_path: str) -> List[str]:
return words


def write_xlf(xlf_path: str, origins: List[str], client: Translate, trans: List[str] = None):
def write_xlf(xlf_path: str, origins: List[str], client: Translate, trans: List[str] = None, limit=5000):
# 翻译
if trans is None:
trans = client.translate(origins)
if isinstance(trans, Null):
print(trans.msg)
raise
trans = []
for lst in [list(islice(origins, i, i + limit)) for i in range(0, len(origins), limit)]:
tl = client.translate(lst)
if isinstance(tl, Null):
print(tl.msg)
raise trans
trans.extend(tl)
trans = [i.translatedText for i in trans]

# 写入文件
Expand Down Expand Up @@ -126,11 +130,12 @@ def write_xlf(xlf_path: str, origins: List[str], client: Translate, trans: List[

class Docts:

def __init__(self, xlf_path: str, client: Translate):
def __init__(self, xlf_path: str, client: Translate, limit: int = 5000):
self.xlf_path = xlf_path
self.words = parse_xlf(xlf_path)
self.ignores = []
self.client = client
self.limit = limit

def add_filter(self, _filter: Callable[[str], bool]):
"""
Expand Down Expand Up @@ -219,13 +224,13 @@ def reset(self):
def save_words(self):
"""..."""
xlf_path = self.xlf_path[:-4] + '_words.xlf'
write_xlf(xlf_path, self.words, self.client)
write_xlf(xlf_path, self.words, self.client, limit=self.limit)
return xlf_path

def save_ignores(self):
"""..."""
xlf_path = self.xlf_path[:-4] + '_ignores.xlf'
write_xlf(xlf_path, self.ignores, self.client, self.ignores)
write_xlf(xlf_path, self.ignores, self.client, self.ignores, limit=self.limit)
return xlf_path


Expand Down

0 comments on commit d483ff1

Please sign in to comment.