Skip to content

Commit

Permalink
Translation pr1 (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Furrior authored Apr 11, 2024
1 parent 35d0891 commit ccab16a
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/auto_translate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Add new translation

on:
workflow_dispatch:
push:
branches: ['translate']

concurrency:
group: translation
cancel-in-progress: false

jobs:
add_translation:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
name: 'Add new translation'

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: Run script
run: ./tools/translate/add_new_translation.sh
28 changes: 28 additions & 0 deletions .github/workflows/translate_branch_update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Update translation branch

on:
workflow_dispatch:
push:
branches: ['master']

concurrency:
group: translation
cancel-in-progress: false

jobs:
update_translation:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
name: 'Update old translation'

steps:
- name: 'Update Branch'
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Run script
run: ./tools/translate/update_translation.sh

24 changes: 24 additions & 0 deletions tools/translate/add_new_translation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
python -m pip install --upgrade pip
pip install -r ./tools/translate/requirements.txt

git fetch origin
git config --local user.email "[email protected]"
git config --local user.name "ss220bot"

git checkout -b translate_tmp
git reset --hard origin/master

git checkout translate -- ./tools/translate/ss220replace.json
git commit -m "Move old translation"

git cherry-pick translate

python ./tools/translate/converter.py
git add ./tools/translate/ss220replace.json
git commit -m "Generate translation file"
git reset .

./tools/translate/ss220_replacer_linux
git commit -m "Apply translation"

git push -f origin translate_tmp:translate
90 changes: 90 additions & 0 deletions tools/translate/converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import git
import json
import re
import os
import hashlib
BUILD_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "..")

repo = git.Repo(BUILD_PATH)
tree = repo.head.commit.tree
if not tree:
print("No changes")
exit()
diff = repo.git.diff(tree)
if not diff:
print("No changes")
exit()

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

# Собираем в структуру вида:
# {
# "file": "player.dm",
# "origin": ["Test", "Test2"],
# "replace": ["Тест", "Тест2"]
# }
files = []
for line in diff:
if line.startswith("---"):
files.append({"file": line[6:], "origin": [], "replace": []})
elif line.startswith("-"):
files[-1]['origin'].append(line[1:].strip())
elif line.startswith("+"):
files[-1]['replace'].append(line[1:].strip())

# Собираем в структуру для хранения в файле:
# {
# "files": [
# {
# "path": "player.dm",
# "replaces": [
# {"original": "Test", "replace": "Тест"},
# {"original": "Test2", "replace": "Тест2"}
# ]
# }
# ]
# }
jsonStructure = {"files": []}
for item in files:
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 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(filteredTranslation, f, ensure_ascii=False, indent=2)

print(f"Added translation for {len(jsonStructure['files'])} files.")
1 change: 1 addition & 0 deletions tools/translate/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GitPython
Binary file added tools/translate/ss220_replacer_linux
Binary file not shown.
Binary file added tools/translate/ss220_replacer_windows.exe
Binary file not shown.
3 changes: 3 additions & 0 deletions tools/translate/ss220replace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files": []
}
16 changes: 16 additions & 0 deletions tools/translate/update_translation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
python -m pip install --upgrade pip
pip install -r ./tools/translate/requirements.txt

git fetch origin
git config --local user.email "[email protected]"
git config --local user.name "ss220bot"

git checkout -b translate_tmp
git reset --hard origin/master

git checkout translate -- ./tools/translate/ss220replace.json

./tools/translate/ss220_replacer_linux
git commit -m "Apply translation"

git push -f origin translate_tmp:translate

0 comments on commit ccab16a

Please sign in to comment.