Skip to content

Commit

Permalink
Update translate tools and add ci (#736)
Browse files Browse the repository at this point in the history
## Что этот PR делает

Переработан converter.py
Добавлена ci проверка на перевод

## Changelog

:cl:
add: Добавлена ci проверка на перевод
fix: Переработан converter.py
/:cl:
  • Loading branch information
KoJIT2009 authored Nov 20, 2023
1 parent 1b38162 commit 9099f8b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 24 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/ci_translate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: CI
on:
push:
branches:
- translate
pull_request:
branches:
- translate

jobs:
translate_check:
name: Run translate checks
runs-on: ubuntu-20.04
steps:
- name: 'Update Branch'
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Installing Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Installing deps
run: |
python -m pip install --upgrade pip
pip install -r ./tools/translator/requirements.txt
- name: Git fetch
run: |
git fetch
- name: Create temporary branch
run: |
git checkout -b translate_tmp origin/translate
- name: Apply PR translation
run: |
git diff ..${{ github.sha }} | git apply
- name: 'Generate Translation'
run: |
python ./tools/translator/converter.py
77 changes: 53 additions & 24 deletions tools/translator/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,71 @@
if not diff:
print("No changes")
exit()
diff = [line for line in diff.split("\n") if line[0] in "+-" and not line.startswith("+++")]

translation = {"files": []}
# Оставляем только стоки, где строки начинаются с "+", "-", "---"
diff = [line for line in diff.split("\n") if line[0] in "+-" and not line.startswith("+++")]

lastfile = ''
lastaction = ''
# Собираем в структуру вида:
# {
# "file": "player.dm",
# "origin": ["Test", "Test2"],
# "replace": ["Тест", "Тест2"]
# }
prepare = []
lastFile = ''
for line in diff:
if line.startswith("---"):
lastfile = line[6:]
lastaction = '---'
translation["files"].append({"path": lastfile, "replaces": []})
lastFile = line[6:]
prepare.append({"file": lastFile, "origin": [], "replace": []})
elif line.startswith("-"):
if lastaction == "---" or lastaction == "+":
translation["files"][-1]["replaces"].append({"original": line[1:], "replace": ""})
elif lastaction == "-":
translation["files"][-1]["replaces"][-1]["original"]+=f"\n{line[1:]}"
lastaction = "-"
prepare[-1]['origin'].append(line[1:])
elif line.startswith("+"):
if lastaction == "-":
translation["files"][-1]["replaces"][-1]["replace"] = line[1:]
elif lastaction == "+":
translation["files"][-1]["replaces"][-1]["replace"] += f"\n{line[1:]}"
lastaction = "+"

filteredTranslation = {"files": []}
for file in translation['files']:
if not allowPathsRegexp.match(file['path']):
prepare[-1]['replace'].append(line[1:])

# Фильтруем структуру: Оставляем только разрешенные файлы
filtered = []
for item in prepare:
if not allowPathsRegexp.match(item['file']):
continue
filteredTranslation["files"].append(file)
filtered.append(item)

# Собираем в структуру для хранения в файле:
# {
# "files": [
# {
# "path": "player.dm",
# "replaces": [
# {"original": "Test", "replace": "Тест"},
# {"original": "Test2", "replace": "Тест2"}
# ]
# }
# ]
# }
jsonStructure = {"files": []}
for item in filtered:
originLen = len(item["origin"])
replaceLen = len(item["replace"])

if originLen != replaceLen:
print("Changes not equals")
print(item)
exit(1)

file = {"path": item["file"], "replaces": []}

for i in range(originLen):
file["replaces"].append({"original": item["origin"][i], "replace": item["replace"][i]})

jsonStructure["files"].append(file)

jsonFilePath = os.path.dirname(os.path.realpath(__file__)) + '/ss220replace.json'

# Добавляем новые элементы к текущим в файле
fullTranslation = json.load(open(jsonFilePath, encoding='utf-8'))
for file in filteredTranslation['files']:
for file in jsonStructure['files']:
fullTranslation["files"].append(file)

with open(jsonFilePath, 'w+', encoding='utf-8') as f:
json.dump(fullTranslation, f, ensure_ascii=False, indent=2)
print(f"Added translation for {len(filteredTranslation['files'])} files.")

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

0 comments on commit 9099f8b

Please sign in to comment.