Skip to content

Commit

Permalink
ci: check and delete unused keys in the localization files
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnguyenbitmark committed Sep 9, 2024
1 parent f077452 commit ec2504f
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/check-localization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Check Localization Keys Usage

on:
workflow_dispatch:
inputs:
locale:
description: "Locale to check (e.g., en-US)"
required: true
default: "en-US"

jobs:
check-localization:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_ASSETS_KEY }}

- name: Submoudles update
run: git -c submodule.auto-test.update=none submodule update --init --recursive

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '14'

- name: Install jq for JSON processing
run: sudo apt-get install jq

- name: Extract and remove unused localization keys
run: |
# Initialize the markdown summary with the header for the table
echo "## Unused Localization Keys" >> $GITHUB_STEP_SUMMARY
echo "| Localization Key | Status |" >> $GITHUB_STEP_SUMMARY
echo "| ---------------- | ------ |" >> $GITHUB_STEP_SUMMARY
# Define locale from input and file path for the JSON file
LOCALE=${{ github.event.inputs.locale }}
FILE_PATH="assets/translations/$LOCALE.json"
# Check if the localization file exists
if [ ! -f "$FILE_PATH" ]; then
echo "Localization file $FILE_PATH does not exist." >> $GITHUB_STEP_SUMMARY
exit 1
fi
# Variable to track if any keys are unused
UNUSED_KEYS=0
# Extract keys from the specified locale JSON file and check if they are used in .dart files in lib/
TEMP_FILE=$(mktemp) # Temp file for modified JSON
jq 'keys' $FILE_PATH | tr -d '[],"' | tr -s ' ' '\n' | while read -r key; do
if ! grep -r "$key.tr()" ./lib/**/*.dart; then
echo "| $key | Not Used |" >> $GITHUB_STEP_SUMMARY
# Remove the unused key from the JSON file
jq "del(.\"$key\")" $FILE_PATH > "$TEMP_FILE" && mv "$TEMP_FILE" $FILE_PATH
UNUSED_KEYS=1
fi
done
# Commit and create a PR if there are unused keys
if [ "$UNUSED_KEYS" -eq 1 ]; then
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git checkout -b remove-unused-localization-keys-$LOCALE
# Add and commit the changes
git add $FILE_PATH
git commit -m "Remove unused localization keys for locale $LOCALE"
# Push the branch
git push origin remove-unused-localization-keys-$LOCALE
# Create a pull request using GitHub CLI
gh pr create --title "Remove unused localization keys for locale $LOCALE" --body "This PR removes unused localization keys detected in the Flutter source code for locale $LOCALE."
else
echo "No unused localization keys found for locale $LOCALE." >> $GITHUB_STEP_SUMMARY
fi

0 comments on commit ec2504f

Please sign in to comment.