-
Notifications
You must be signed in to change notification settings - Fork 2.1k
163 lines (130 loc) · 4.6 KB
/
auto-translate.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
name: Auto-Translate
on:
push:
branches:
- 'main'
paths:
- 'README.md'
pull_request:
types:
- closed
branches:
- 'main'
paths:
- "README.md"
workflow_dispatch:
jobs:
translate:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/[email protected]
- name: Install Python 3.10
uses: actions/[email protected]
with:
python-version: '3.10'
- name: Verify Python and pip packages
shell: sh
run: |
python --version
pip install googletrans==4.0.0-rc1
pip install requests
pip install wget
- name: mkdir and cd
shell: sh
run: |
mkdir parent
- name: Download parent commit
shell: python
run: |
import requests
import os
import wget
import zipfile
def un_zip(file_name):
zip_file = zipfile.ZipFile(file_name)
for names in zip_file.namelist():
zip_file.extract(names, 'parent/')
zip_file.close()
s = requests.Session()
commit_response = s.get("https://api.github.com/repos/geekan/HowToLiveLonger/commits?per_page=1").text
parent_sha = commit_response.split('"parents":[{"sha":"')[1].split('","')[0]
d_url = "https://github.com/geekan/HowToLiveLonger/archive/" + parent_sha + ".zip"
path = 'temp.zip'
try:
wget.download(d_url, path)
except Exception as e:
print('Error')
print(e)
un_zip('temp.zip')
os.remove('temp.zip')
os.system("cp parent/HowToLiveLonger-{}/README.md parent/README.md".format(parent_sha))
- name: Compare and translate(Google)
shell: python
run: |
# -*- coding: utf-8 -*-
import difflib
from googletrans import Translator
d = difflib.Differ()
tl = Translator()
with open("parent/README.md","r",encoding='utf-8') as parent:
parent_md = parent.readlines()
with open("README.md","r",encoding='utf-8') as new:
new_md = new.readlines()
result = list(d.compare(parent_md, new_md))
newlines = []
del_list = []
for i,item in enumerate(result):
if '+' in item:
newlines.append(item.strip('\n'))
for j,jtem in enumerate(newlines):
if newlines[j][0] != '+' or newlines[j] == '\n':
del_list.append(j)
for a,atem in enumerate(del_list):
newlines.pop(atem - a)
for k,ktem in enumerate(newlines):
# ktem = ktem.lstrip('+ ').lstrip('# ')
print(ktem)
newlines[k] = tl.translate(text=ktem, src='zh-cn', dest='en').text
tlr = '\n\n'.join(newlines)
with open("README_en.md","a") as tlf:
tlf.write("\n\n> The following content is translated by machine, and can be merged after manual modification\n")
tlf.write(tlr)
- name: Upload translation
uses: actions/[email protected]
with:
name: temp_trans_file
path: README_en.md
push-and-PR:
if: ${{ github.event.pull_request.merged == true || github.event_name == 'push' }}
needs: translate
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/[email protected]
- name: Checkout new branch
shell: sh
run: |
git checkout -b auto-translation
git push origin auto-translation
- name: Download translation
uses: actions/[email protected]
with:
name: temp_trans_file
- name: Git as GitHub Actions Bot
uses: Lucky3028/[email protected]
- name: Commit translation to review branch
shell: sh
run: |
git add .
git commit -m "Auto translation for new lines"
git push origin auto-translation
- name: Open PR and request reviews
uses: repo-sync/pull-request@master
with:
source_branch: "auto-translation"
destination_branch: "main"
pr_title: "Auto Translation"
pr_body: "*Automated PR, submitting translation.*"
pr_reviewer: "qhy040404,geekan"
github_token: ${{ github.token }}