Skip to content

Instantly share code, notes, and snippets.

@woylie
Last active October 20, 2023 03:17
Show Gist options
  • Save woylie/0488f916427bf4a4ff0cc81d0df7084c to your computer and use it in GitHub Desktop.
Save woylie/0488f916427bf4a4ff0cc81d0df7084c to your computer and use it in GitHub Desktop.
Shell script for checking gettext .po files for missing translations
#!/bin/bash
# Script generated by ChatGPT, no edits
# Example: ./check_missing_translations.sh apps/my_app_web/priv/gettext/ja
# Ensure the script is given an argument (the folder to check)
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <locale_folder>"
exit 1
fi
# Store the name of the folder and construct the LC_MESSAGES path
folder="$1"
lc_messages_folder="$folder/LC_MESSAGES"
# Check if the folder exists and is readable
if [ ! -d "$lc_messages_folder" ] || [ ! -r "$lc_messages_folder" ]; then
echo "LC_MESSAGES folder doesn't exist or is not readable"
exit 1
fi
# Initialize global missing count
global_missing_count=0
# Function to check a single .po file
check_po_file() {
local file="$1"
local ignore_first=true
local missing_count=0
local last_msgid=""
while read -r line; do
if [[ "$line" =~ ^msgid\ \"(.*)\" ]]; then
last_msgid="${BASH_REMATCH[1]}"
fi
if [ "$line" = 'msgstr ""' ]; then
if [ "$ignore_first" = true ]; then
ignore_first=false
else
echo " Missing translation for msgid \"$last_msgid\""
((missing_count++))
fi
fi
done < "$file"
if [ "$missing_count" -gt 0 ]; then
echo "In $file: Found $missing_count missing translations."
((global_missing_count+=missing_count))
else
echo "In $file: No missing translations found."
fi
}
# Iterate through all .po files in the LC_MESSAGES folder
for po_file in "$lc_messages_folder"/*.po; do
check_po_file "$po_file"
done
# Output final results
if [ "$global_missing_count" -gt 0 ]; then
echo "Total missing translations across all files: $global_missing_count"
exit 1 # Exit with a non-zero status code to indicate missing translations
else
echo "No missing translations found across all files."
exit 0 # Exit normally
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment