Skip to content

Commit

Permalink
Merge pull request #161 from Nacosia/optim-auto_format_translation_ac…
Browse files Browse the repository at this point in the history
…tion

Optim auto format translation action
  • Loading branch information
Ada18980 authored Oct 26, 2024
2 parents 28b2e91 + b94108d commit 5659f61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ For some nouns, you can refer to the [Noun Explanation](#Noun-Explanation) secti
##### Writing rules

One line of original text, then the next line is the translated text。

*(Ensure original text on odd lines, translation on even lines)*

Such as:
`````
Original text 1
Translated text 1
Original text 2
Translated text 2
`````
Expand Down
25 changes: 20 additions & 5 deletions .github/scripts/format-translation-script.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
"KDVersionStr"
]

class LineCountingWriter:
def __init__(self, file, init_count=1):
self.file = file
self.count_lines = init_count

def write(self, content):
lines = content.count('\n')
self.count_lines += lines
self.file.write(content)

def line_count(self):
return self.count_lines

# Read the csv file
def read_csv_with_empty_lines(file_path) -> list:
with open(file_path, newline='', encoding='utf-8') as csvfile:
Expand Down Expand Up @@ -57,18 +70,20 @@ def read_translation_file(file_path) -> list:
def write_translated_file(csv_lines : list, translations : list, output_path : str):
translated = set()
with open(output_path, 'w', encoding='utf-8') as file:
writer = LineCountingWriter(file)
for line in csv_lines:
if not line or not line[1]:
file.write('\n')
writer.write('\n')
continue

key, original, *_ = line

if original in translated:
continue

translated.add(original)

if writer.line_count() % 2 == 0:
writer.write('\n')

# The next line of the original text corresponding to the List is the translation. If the translation is not found,will use "### Original" instead.
# When the translation is the same as the original text, "### Original" is also used because it is meaningless.
try:
Expand All @@ -77,10 +92,10 @@ def write_translated_file(csv_lines : list, translations : list, output_path : s
if not translation or (original == translation) :
raise ValueError
except (ValueError, IndexError):
file.write(f'### {original}\n')
writer.write(f'### {original}\n')
continue

file.write(f'{original}\n{translation}\n')
writer.write(f'{original}\n{translation}\n')

original_csv_path = 'Screens/MiniGame/KinkyDungeon/Text_KinkyDungeon.csv'

Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/format-translation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ on:
branches:
- newartwork
paths:
- 'Screens/MiniGame/KinkyDungeon/*'
- "Screens/MiniGame/KinkyDungeon/*"

jobs:
format-translation:
runs-on: ubuntu-latest

if: github.repository == 'Ada18980/KinkiestDungeon'
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
python-version: "3.x"

- name: 🚀 Run format-translation script
run: python .github/scripts/format-translation-script.py Screens/MiniGame/KinkyDungeon/Text_KinkyDungeon.csv
- name: 📝 Commit changes
id: check_changes

- name: 📝 Commit changes
id: check_changes
run: |
git config --local user.email "action@github.com"
git config --local user.name "format-translation-action"
Expand Down

0 comments on commit 5659f61

Please sign in to comment.