94 lines
2.4 KiB
Python
Executable File
94 lines
2.4 KiB
Python
Executable File
#!/usr/bin/pyhton3
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
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()
|
|
|
|
_list = _cached.split('\n')
|
|
_to_translate = []
|
|
|
|
for _str in _list:
|
|
if _str.find('%_t') != -1 or _str.find('%_T') != -1:
|
|
_to_translate.append(_str)
|
|
|
|
return _to_translate
|
|
|
|
|
|
def pars_file(name: str) -> list:
|
|
"""
|
|
:param name: name of file
|
|
:return:
|
|
"""
|
|
_list: list = []
|
|
for _str in parsing_file(name):
|
|
_str_parsed = re.findall('"([^/\"]*)(?: /\*(.*)\*/)?"%_[t,T]', _str)
|
|
_list.append(_str_parsed)
|
|
return _list
|
|
|
|
|
|
def all_to_translate(_dir):
|
|
_files = scan_files(_dir)
|
|
_str = ''
|
|
for _file in _files:
|
|
_strings = pars_file(_file)
|
|
_list = []
|
|
if len(_strings) > 0:
|
|
for _string in _strings:
|
|
_list.append(_string)
|
|
_title = f'# ========== {_file.split("weapon-project-extended")[1]} ==========\n'
|
|
_str = _str + _title
|
|
print(f'Generate translation file for {_file.split("weapon-project-extended")[1]}')
|
|
for _name in _list:
|
|
_msgctxt = ''
|
|
if len(_name[0][1]) > 1:
|
|
_msgctxt = f'msgctxt "{_name[0][1]}"\n'
|
|
|
|
_msgid = f'msgid "{_name[0][0]}"\n'
|
|
_msgstr = f'msgstr "{translate(_name[0][0])}"\n'
|
|
|
|
_entity = f'#: {_file.split("weapon-project-extended")[1]}:\n{_msgctxt}{_msgid}{_msgstr}\n'
|
|
_str = _str + _entity
|
|
with open('data/localization/ru.po', 'w', encoding='utf8') as f:
|
|
f.write(_str)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|