From 9099f8b2258084dec48aab931b3caa591d11225c Mon Sep 17 00:00:00 2001 From: KoJIT2009 Date: Tue, 21 Nov 2023 00:39:58 +0500 Subject: [PATCH] Update translate tools and add ci (#736) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Что этот PR делает Переработан converter.py Добавлена ci проверка на перевод ## Changelog :cl: add: Добавлена ci проверка на перевод fix: Переработан converter.py /:cl: --- .github/workflows/ci_translate.yml | 44 +++++++++++++++++ tools/translator/converter.py | 77 ++++++++++++++++++++---------- 2 files changed, 97 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/ci_translate.yml diff --git a/.github/workflows/ci_translate.yml b/.github/workflows/ci_translate.yml new file mode 100644 index 000000000000..5915ef350f05 --- /dev/null +++ b/.github/workflows/ci_translate.yml @@ -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 diff --git a/tools/translator/converter.py b/tools/translator/converter.py index d6c298602a59..79bf12e6c281 100644 --- a/tools/translator/converter.py +++ b/tools/translator/converter.py @@ -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.")