i18n Consistency Check #31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------- | |
# To add a new language, do the following: | |
# - add the 2-letter language code in matrix.language | |
# - add the flag and language name that you want to use at the beginning of the #check-consistency step | |
# ------------------------- | |
name: i18n Consistency Check | |
on: | |
schedule: | |
- cron: '0 0 1 * *' # run at midnight, on day 1 of the month | |
workflow_dispatch: | |
jobs: | |
consistency-check: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
language: [ja, zh, pt-br, gl] | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: '0' | |
- name: Check consistency | |
id: check-consistency | |
run: | | |
# Declare the flags | |
declare -A flags=( ["ja"]=":jp: Japanese" ["zh"]=":cn: Chinese" ["pt-br"]=":brazil: Brazilian Portuguese" ["gl"]="Galician" ) | |
# Loop through all files in the English directory | |
for file in $(find patterns/{2-structured,3-validated} -name '*.md'); do | |
echo "Original file: $file" | |
#[[ $file =~ "3-validated" ]] && continue # if the file is under 3-validated, skip (one liner) - 2023/08/26 | |
#[[ $file =~ "/templates" ]] && continue # if the file is under /templates, skip. consistency check does not work for that yet. | |
if [[ $file =~ "/templates" ]]; then | |
i18n_filename=$(echo "$file" | sed -E "s_patterns/(2-structured|3-validated)/templates_translation/${{matrix.language}}/templates") | |
else | |
i18n_filename=$(echo "$file" | sed -E "s_patterns/(2-structured|3-validated)_translation/${{matrix.language}}/patterns_g") | |
fi | |
echo "Translation file: $i18n_filename" | |
if [[ ! -e "$i18n_filename" ]]; then | |
echo "NOT TRANSLATED" | |
continue | |
fi | |
original_updated_at=$(date -d "$(git log -1 --format=%cd --date=iso $file)" +%s) | |
i18n_content_updated_at=$(date -d "$(git log -1 --format=%cd --date=iso $i18n_filename)" +%s) | |
if [[ $((original_updated_at - i18n_content_updated_at)) -ge 1 ]]; then | |
echo "WARNING" | |
content_header=$(grep -E '^title+' "$file" | sort -r | head -n1) | |
content_title=$(echo "$content_header" | sed 's/title: //g') | |
content_title=$(awk '/## Title/{flag=1; next} /##/{flag=0; exit} flag{printf "%s", $0}' "$file") | |
days_since_overdue_updates=$(( ($(date +%s) - original_updated_at) / 60 / 60 / 24 )) | |
original_last_update_hash=$(git log -1 --format=%H $file) | |
parent_hash=$(git log -1 --format=%P $original_last_update_hash) | |
result=$(git diff "${parent_hash}" HEAD "$file" | sed '1,4 s/^/# /') | |
# Append to the issue.md file | |
cat <<- EOM >> issue.md | |
<details><summary><b>$content_title</b> ($file)</summary> | |
For more information, please compare [the original file(en)](https://github.com/$GITHUB_REPOSITORY/blob/master/$file) with [the translated file](https://github.com/$GITHUB_REPOSITORY/blob/master/$i18n_filename). You can view [the differences](https://github.com/$GITHUB_REPOSITORY/compare/$i18n_last_update_hash...$original_last_update_hash) on GitHub. The number of days since overdue updates is **$days_since_overdue_updates** days. | |
\`\`\`diff | |
$result | |
\`\`\` | |
</details> | |
EOM | |
else | |
echo "CHECK" | |
fi | |
done | |
- name: Check issue.md existence | |
id: check_files | |
uses: andstor/file-existence-action@v2 | |
with: | |
files: "issue.md" | |
- name: Create issue | |
if: steps.check_files.outputs.files_exists == 'true' | |
run: | | |
# Declare the flags | |
declare -A flags=( ["ja"]=":jp: Japanese" ["zh"]=":cn: Chinese" ["pt-br"]=":brazil: Brazilian Portuguese" ["gl"]="Galician" ) | |
# Heredoc for issue header | |
cat <<- EOM > issue-full.md | |
# i18n Contents Consistency Issue | |
The following files may have consistency issues with the English version. Please check and update the files. | |
This issue is created when any of the English patterns have changed (in folder `patterns/`). It compares the git update history to let you know what updates are overdue. The issue should be closed when the update is complete. | |
EOM | |
# combine header and rest of file created in previous steps | |
cat issue.md >> issue-full.md | |
# title of the GitHub issue to look for | |
issue_title="${flags['${{matrix.language}}']}: Content Consistency Issue" | |
# Get the existing issue ID | |
search_query="is:issue is:open in:title \"$issue_title\"" | |
echo "search_query: $search_query" | |
existing_issue_id=$(gh issue list -L 1 -S "$search_query" | cut -f1) | |
echo "existing_issue_id: $existing_issue_id" | |
# Create a new issue or comment on the existing one | |
#if expr "$existing_issue_id" : '^[0-9]*$' >/dev/null; then | |
# gh issue comment "$existing_issue_id" -F issue-full.md -R $GITHUB_REPOSITORY | |
#else | |
# gh issue create -t "$issue_title" -F issue-full.md -R $GITHUB_REPOSITORY -l "Type - Translation" | |
#fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |