mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-14 03:58:18 +00:00
59cf78be24
Alphabetically as the files are located in directory structure. Keeps the language files more stable
75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
import os
|
|
import re
|
|
from collections import OrderedDict
|
|
|
|
class LanguageDecodeError(Exception):
|
|
def __init__(self, message, filename, line):
|
|
error = "File \"{1}\", line {2}: {0}".format(message, filename, line+1)
|
|
super(LanguageDecodeError, self).__init__(error)
|
|
|
|
|
|
def decode(fileobj, elements_per_key):
|
|
data = {}
|
|
current_key = None
|
|
for index, line in enumerate(fileobj):
|
|
line = line.encode("utf-8").decode("utf-8-sig")
|
|
line = line[:-1]
|
|
if line and line[-1] == "\r":
|
|
line = line[:-1]
|
|
if not line or line[:1] == "#":
|
|
continue
|
|
if line[:3] == "== ":
|
|
if len(data[current_key]) >= 1+elements_per_key:
|
|
raise LanguageDecodeError("Wrong number of elements per key", fileobj.name, index)
|
|
if current_key:
|
|
data[current_key].extend([line[3:]])
|
|
else:
|
|
raise LanguageDecodeError("Element before key given", fileobj.name, index)
|
|
else:
|
|
if current_key:
|
|
if len(data[current_key]) != 1+elements_per_key:
|
|
raise LanguageDecodeError("Wrong number of elements per key", fileobj.name, index)
|
|
data[current_key].append(index)
|
|
if line in data:
|
|
raise LanguageDecodeError("Key defined multiple times: " + line, fileobj.name, index)
|
|
data[line] = [index]
|
|
current_key = line
|
|
if len(data[current_key]) != 1+elements_per_key:
|
|
raise LanguageDecodeError("Wrong number of elements per key", fileobj.name, index)
|
|
data[current_key].append(index+1)
|
|
return data
|
|
|
|
|
|
def check_file(path):
|
|
with open(path) as fileobj:
|
|
matches = re.findall("Localize\s*\(\s*\"([^\"]+)\"\s*\)", fileobj.read())
|
|
return matches
|
|
|
|
|
|
def check_folder(path):
|
|
englishlist = OrderedDict()
|
|
for path, dirs, files in os.walk(path):
|
|
dirs.sort()
|
|
for f in sorted(files):
|
|
if not any(f.endswith(x) for x in ".cpp .c .h".split()):
|
|
continue
|
|
for sentence in check_file(os.path.join(path, f)):
|
|
englishlist[sentence] = None
|
|
return englishlist
|
|
|
|
|
|
def languages():
|
|
index = decode(open("data/languages/index.txt"), 2)
|
|
langs = {"data/languages/"+key+".txt" : [key]+elements for key, elements in index.items()}
|
|
return langs
|
|
|
|
|
|
def translations(filename):
|
|
translations = decode(open(filename), 1)
|
|
return translations
|
|
|
|
|
|
def localizes():
|
|
englishlist = list(check_folder("src"))
|
|
return englishlist
|