Repository navigation
Expand file tree
/
Copy pathlangdetect.py
More file actions
55 lines (40 loc) · 1.69 KB
/
Copy pathlangdetect.py
File metadata and controls
55 lines (40 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#langdetect extracts the strings of a lang file and dumps them into a list
import os
from os import PathLike
from typing import Union
FilePathType = Union[str, PathLike[str]]
def extractLang(langPath: FilePathType) -> list[str]:
"""
extracts the strings of a .lang file and dumps them into a list
:param str langPath: Path of the .lang file
"""
langList = []
reachedEnd = False
#lang structure: LENGTH1 LENGTH2 STR LENGTH1 LENGTH2 STR ...
#LENGTH1 is =0 in most cases, but when strings do exceed a certain length, that length will be inside LENGTH1 and LENGTH2
with open(langPath, 'rb') as f:
file_size = os.stat(langPath).st_size
try:
#try until it reaches the end of the file
while f.tell() < file_size:
length = int.from_bytes(f.read(2), byteorder='big') #reads next two bytes, as the length is stored in those two
extractedStr = f.read(length).decode('utf-8')
if extractedStr == '':
break
else:
langList.append(extractedStr)
except Exception as e:
print(e)
f.close()
return langList
def writeLang(langList: list[str], exportPath: FilePathType):
"""
Takes a list of strings and writes a .lang file
:param list langList: the list of strings
:param FilePathType exportPath: Path of the exported file. Should end with ".lang"
"""
with open(exportPath, 'wb') as f:
for i in langList:
lengthByte = len(bytes(i, 'utf-8')).to_bytes(2, 'big')
f.write(lengthByte)
f.write(bytes(i, 'utf-8'))