changed main

This commit is contained in:
bacon
2023-09-01 14:06:11 +03:00
parent 8ac3a092b7
commit 0039f9ff29
4 changed files with 49 additions and 44 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
/venv/
/.idea/

24
lib/opener.py Normal file
View File

@@ -0,0 +1,24 @@
import json
import os
def scan_files(path) -> list:
"""
Scans dirs and subdirs in PATH to gind *.lua files
:param path: set path to scan
:return: list of files in dir and subdir
"""
list_files = []
for root, dirs, files in os.walk(path):
for file in files:
if '.lua' in file: list_files.append(f'{root}/{file}')
return list_files
def open_translate(lang='ru'):
with open(f'{lang}.po', 'r') as f:
_read = f.read()
return _read

View File

@@ -16,5 +16,3 @@ def translate(_input, lang='ru'):
)
return r.json()['translatedText']

65
main.py
View File

@@ -1,33 +1,11 @@
#!/usr/bin/pyhton3
import os
#!/usr/bin/pyhton
import re
import sys
from lib.opener import scan_files, open_translate
from lib.translate import translate
def scan_files(path) -> list:
"""
Scans dirs and subdirs in PATH to gind *.lua files
:param path: set path to scan
:return: list of files in dir and subdir
"""
list_files = []
for root, dirs, files in os.walk(path):
for file in files:
if '.lua' in file: list_files.append(f'{root}/{file}')
return list_files
def open_translate(arg, lang='ru'):
translation_file = arg + fr'/data/localization/{lang}.po'
with open(translation_file, 'r') as f:
translation_text = f.read()
translation_json = translation_text.split('\n\n')
return translation_json
def parsing_file(f):
with open(f) as _f:
_cached = _f.read()
@@ -49,8 +27,8 @@ def pars_file(name: str) -> list:
"""
_list: list = []
for _str in parsing_file(name):
_str_parsed = re.findall('"([^/\"]*)(?: /\*(.*)\*/)?"%_[t,T]', _str)
_list.append(_str_parsed)
_str_parsed = re.findall(r'= "([^/\"]*)(?: /\* (.*)\*/)?"%_[t,T]', _str)
if _str_parsed: _list.append(_str_parsed)
return _list
@@ -60,34 +38,37 @@ def all_to_translate(_dir):
for _file in _files:
_strings = pars_file(_file)
_list = []
_fname = _file.split("WPE2")[1].replace("/", "\\")
if len(_strings) > 0:
for _string in _strings:
_list.append(_string)
_title = f'# ========== {_file.split("weapon-project-extended")[1]} ==========\n'
_title = f'# ========== {_fname} ==========\n'
_str = _str + _title
print(f'Generate translation file for {_file.split("weapon-project-extended")[1]}')
print(f'Generate translation file for {_fname}')
for _name in _list:
_msgctxt = ''
if len(_name[0][1]) > 1:
# print(_name)
if len(_name[0]) > 1:
_msgctxt = f'msgctxt "{_name[0][1]}"\n'
else:
_msgctxt = f'msgctxt ""\n'
_msgid = f'msgid "{_name[0][0]}"\n'
_msgstr = f'msgstr "{translate(_name[0][0])}"\n'
_msgstr = f'msgstr ""\n'
_entity = f'#: {_file.split("weapon-project-extended")[1]}:\n{_msgctxt}{_msgid}{_msgstr}\n'
_entity = f'#: {_fname}:\n{_msgctxt}{_msgid}{_msgstr}\n'
_str = _str + _entity
with open('data/localization/ru.po', 'w', encoding='utf8') as f:
with open('ru.po', 'w', encoding='UTF-8') as f:
f.write(_str)
print(open_translate())
if __name__ == '__main__':
if len(sys.argv) > 1:
arg = sys.argv[1]
else:
arg = r'C:\Users\riksl\AppData\Roaming\Avorion\mods\WPE2'
all_to_translate(arg)
# open_translate(arg)
# if __name__ == '__main__':
# if len(sys.argv) > 1:
# arg = sys.argv[1]
# else:
# arg = r'C:\Users\riksl\AppData\Roaming\Avorion\mods\WPE2'
#
# all_to_translate(arg)