Skip to content

Commit

Permalink
move auto_translate to develop tools folder
Browse files Browse the repository at this point in the history
  • Loading branch information
pur1fying committed Jul 17, 2024
1 parent 80c1163 commit 8aa3030
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
!window.py
!window.spec
!i18n.pro
!auto_translate.py
!requirements-i18n.txt
src/explore_task_data/__pycache__/normal_task_11.cpython-39.pyc
*.pyc
Expand Down
28 changes: 14 additions & 14 deletions auto_translate.py → develop_tools/auto_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ def handle(self, request):


class Request:
def __init__(self, handlers: list[Handler],
qt_language: Language,
translator: str = 'bing',
from_lang: str = 'auto',
def __init__(self, handlers: list[Handler],
qt_language: Language,
translator: str = 'bing',
from_lang: str = 'auto',
to_lang: str = 'en'):
"""
Parameters
----------
handlers: list[Handler]
a list of handlers that represent the files to translate.
a list of handlers that represent the files to translate.
qt_language: Language
the memeber of the enum Language to translate
Expand All @@ -44,18 +44,18 @@ def __init__(self, handlers: list[Handler],
self.qt_language = qt_language
self.strLang = qt_language.value.name()
self.handlers = handlers
self.translator = translator
self.translator = translator
self.from_lang = from_lang
self.to_lang = to_lang

def translate_text(self, text):
text = ts.translate_text(text, self.translator, self.from_lang, self.to_lang)
print(text)
return text

def translate_html(self, html_text):
return ts.translate_html(html_text, self.translator, self.from_lang, self.to_lang)

def process(self):
self.handlers[0].handle(self)

Expand All @@ -65,14 +65,14 @@ def handle(self, request):
result = subprocess.run(['pylupdate5', 'i18n.pro'], capture_output=True, text=True)
print(result.stdout)
self.set_next(request)


class XmlHandler(Handler):
"""Translate ts files"""
def handle(self, request):
# Load the XML from a file
input_file = os.path.join('gui/i18n/', f'{request.strLang}.ts')
output_file = os.path.join('gui/i18n/', f'{request.strLang}.ts')
input_file = os.path.join('../gui/i18n/', f'{request.strLang}.ts')
output_file = os.path.join('../gui/i18n/', f'{request.strLang}.ts')

tree = etree.parse(input_file)
root = tree.getroot()
Expand Down Expand Up @@ -123,7 +123,7 @@ def translate_mission_types(self, request, input_dir, output_dir):
f.write(text)

def handle(self, request):
input_dir = 'src/descriptions/'
input_dir = '../src/descriptions/'
output_dir = f'src/descriptions/{request.strLang}'
# Ensure the output directory exists
if not os.path.exists(output_dir):
Expand All @@ -142,7 +142,7 @@ def handle(self, request):
translated_html = request.translate_html(html)
soup = BeautifulSoup(translated_html, 'lxml')
prettyHTML = soup.prettify()

# Write the translated HTML to the output directory
name, extension = os.path.splitext(filename)
output_name = f'{request.translate_text(name)}.html'
Expand All @@ -152,7 +152,7 @@ def handle(self, request):

class LreleaseHandler(Handler):
def handle(self, request):
directory = os.path.join(os.getcwd(), 'gui', 'i18n')
directory = os.path.join(os.getcwd(), '../gui', 'i18n')
result = subprocess.run(['lrelease', f'{request.strLang}.ts'], cwd=directory, capture_output=True, text=True)
print(result.stdout)
self.set_next(request)
Expand Down

0 comments on commit 8aa3030

Please sign in to comment.