Skip to content

Commit

Permalink
Тримим и убираем дубли
Browse files Browse the repository at this point in the history
  • Loading branch information
KoJIT2009 committed Nov 20, 2023
1 parent f0ed191 commit 8f5432c
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions tools/translator/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import json
import re
import os
BUILD_PATH = "./"
import hashlib
BUILD_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..")

allowPathsRegexp = re.compile('^code/.*')

Expand All @@ -26,15 +27,13 @@
# "replace": ["Тест", "Тест2"]
# }
prepare = []
lastFile = ''
for line in diff:
if line.startswith("---"):
lastFile = line[6:]
prepare.append({"file": lastFile, "origin": [], "replace": []})
prepare.append({"file": line[6:], "origin": [], "replace": []})
elif line.startswith("-"):
prepare[-1]['origin'].append(line[1:])
prepare[-1]['origin'].append(line[1:].strip())
elif line.startswith("+"):
prepare[-1]['replace'].append(line[1:])
prepare[-1]['replace'].append(line[1:].strip())

# Фильтруем структуру: Оставляем только разрешенные файлы
filtered = []
Expand Down Expand Up @@ -79,7 +78,22 @@
for file in jsonStructure['files']:
fullTranslation["files"].append(file)

# Убираем дубли
hashCache = {}
filteredTranslation = {"files": []}
for file in fullTranslation['files']:
filteredFile = {"path": file["path"], "replaces": []}
for replace in file['replaces']:
hash = hashlib.sha256((file['path'] + replace["original"]).encode("utf-8")).hexdigest()
if hash in hashCache:
continue
hashCache[hash] = hash

filteredFile["replaces"].append({"original": replace["original"], "replace": replace["replace"]})

filteredTranslation["files"].append(filteredFile)

with open(jsonFilePath, 'w+', encoding='utf-8') as f:
json.dump(fullTranslation, f, ensure_ascii=False, indent=2)
json.dump(filteredTranslation, f, ensure_ascii=False, indent=2)

print(f"Added translation for {len(jsonStructure['files'])} files.")

0 comments on commit 8f5432c

Please sign in to comment.