Skip to content

Commit

Permalink
update: recursion_trans
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-jun-xiang committed May 2, 2023
1 parent cc0953c commit 4185e13
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 66 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/translate-readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ name: Translate Readme

on:
push:
branches:
- dev
- test
- main
branches: ['**']

jobs:
translate:
Expand Down Expand Up @@ -44,7 +41,7 @@ jobs:
LATEST_COMMIT_MSG=$(git show -s --format='%s')
if [[ "$LATEST_COMMIT_MSG" != "Auto-translate README" ]]; then
git add README.zh-TW.md
git add .
git commit -m "Auto-translate README"
git push
fi
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# How to use ?

1. Click on the "star" icon to add this project to your Github repository.
1. Click on the :star: icon to add this project to your Github repository.
2. Download the `translate-readme.yaml` file from this project's Github repository.
3. Add the branched which want to use in `translate-readme.yaml`:
```
Expand All @@ -39,3 +39,4 @@
* Check out [Test Document](https://github.com/Lin-jun-xiang/vscode-extensions-best/tree/main)
* The Test Document is used `action-translate-markdown` to update.
126 changes: 66 additions & 60 deletions translation.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash
get_inline_code() {
line=$1

Expand All @@ -16,65 +17,70 @@ get_inline_code() {
echo "$translated"$'\n'
}

if [[ $(git diff --name-only HEAD~1 HEAD -- README.md) ]]; then
echo "There are changes in README.md."

output=""
in_code_block=0 # Track whether we're currently in a code block

while IFS= read -r line; do
inline_code=$(echo "$line" | awk -v RS='`' 'NR%2==0') # Extract value in inline code

line="${line// /%_ttab_%}" # Replace space with special character
line=$(echo "$line" | awk 'BEGIN{FS=":";OFS=":";} {for(i=2;i<NF;i+=2) if ($i!="") $i="emoji_"$i""}1') # Replace :xxx: with :emoji_xxx:
line="${line//./%_ddot_%}" # Replace . with special character
line="${line//\\/%_bbackslash_%}" # Replace \ with special character

if [[ "$line" =~ ^\<.*\> ]]; then # Ignore <tags>
output+="$line"$'\n'

elif [[ "$line" =~ ^\[中文版 ]]; then
output+="$line"$'\n'

elif [[ "$line" =~ ^# ]]; then # Ignore #
output+="$line"$'\n'

elif [[ "$line" =~ ^\[[^\]]*\]\(#.*\)$ ]]; then # Ignore [](#)
output+="$line"$'\n'

elif [[ "$line" =~ ^#+[[:space:]] ]]; then # Translate headings
line=$(echo "$line" | sed 's/=/{EQUAL}/g')
line=$(echo "$line" | sed -E 's/^#+[[:space:]](.*)$/#\1/')
translated=$(get_inline_code "$line")

output+="$translated"$'\n'

else # Translate text
if [[ "$line" == *"\`\`\`"* ]]; then # Ignore code blocks
if [ $in_code_block -eq 0 ]; then
in_code_block=1
else
in_code_block=0
# Checkout all the README.md which modified
find . -name 'README.md' | while IFS= read -r file; do
if [[ $file == *"README.md" ]]; then
echo "Checkout $file now."
if [[ $(git diff --name-only HEAD~1 HEAD -- "$file") ]]; then
echo "There are changes in $file."

output=""
in_code_block=0 # Track whether we're currently in a code block

while IFS= read -r line; do
inline_code=$(echo "$line" | awk -v RS='`' 'NR%2==0') # Extract value in inline code

line="${line// /%_ttab_%}" # Replace space with special character
line=$(echo "$line" | awk 'BEGIN{FS=":";OFS=":";} {for(i=2;i<NF;i+=2) if ($i!="") $i="emoji_"$i""}1') # Replace :xxx: with :emoji_xxx:
line="${line//./%_ddot_%}" # Replace . with special character
line="${line//\\/%_bbackslash_%}" # Replace \ with special character

if [[ "$line" =~ ^\<.*\> ]]; then # Ignore <tags>
output+="$line"$'\n'

elif [[ "$line" =~ ^\[中文版 ]]; then
output+="$line"$'\n'

elif [[ "$line" =~ ^# ]]; then # Ignore #
output+="$line"$'\n'

elif [[ "$line" =~ ^\[[^\]]*\]\(#.*\)$ ]]; then # Ignore [](#)
output+="$line"$'\n'

elif [[ "$line" =~ ^#+[[:space:]] ]]; then # Translate headings
line=$(echo "$line" | sed 's/=/{EQUAL}/g')
line=$(echo "$line" | sed -E 's/^#+[[:space:]](.*)$/#\1/')
translated=$(get_inline_code "$line")

output+="$translated"$'\n'

else # Translate text
if [[ "$line" == *"\`\`\`"* ]]; then # Ignore code blocks
if [ $in_code_block -eq 0 ]; then
in_code_block=1
else
in_code_block=0
fi
output+="$line"$'\n'
elif [ $in_code_block -eq 1 ]; then
output+="$line"$'\n'
else
translated=$(get_inline_code "$line")

output+="$translated"$'\n'
fi
fi
output+="$line"$'\n'
elif [ $in_code_block -eq 1 ]; then
output+="$line"$'\n'
else
translated=$(get_inline_code "$line")

output+="$translated"$'\n'
fi
done < "$file"

# Write output file
output_file=$(echo "$file" | sed 's/README.md/README.zh-TW.md/')
echo -e "$output" > "$output_file"
sed -i 's/u003d/=/g' "$output_file"
sed -i 's/%_ttab_%/ /g' "$output_file"
sed -i 's/:/:/g' "$output_file"
sed -i 's/:emoji_/:/g' "$output_file"
sed -i 's/%_ddot_%/./g' "$output_file"
sed -i 's/%_bbackslash_%/\//g' "$output_file"
fi
# Read README.md to do while
done < README.md

# Write output file
echo -e "$output" > README.zh-TW.md
sed -i 's/u003d/=/g' README.zh-TW.md
sed -i 's/%_ttab_%/ /g' README.zh-TW.md
sed -i 's/:/:/g' README.zh-TW.md
sed -i 's/:emoji_/:/g' README.zh-TW.md
sed -i 's/%_ddot_%/./g' README.zh-TW.md
sed -i 's/%_bbackslash_%/\//g' README.zh-TW.md

fi
fi
done

0 comments on commit 4185e13

Please sign in to comment.