Skip to content

Commit

Permalink
refactor: check_pot now checks for missing translations
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed Nov 8, 2023
1 parent b758397 commit b5e2fb5
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ jobs:
run: make check_pot
- name: Po file
run: make check_po
- name: Check for missing translations
run: make check_missing_translations
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ all:
@echo - check_po
@echo - check_pot
@echo - update_pot
@echo - check_missing_translations
@echo

.PHONY: check_version
Expand Down Expand Up @@ -50,6 +51,10 @@ check_po_strict: _touch_pot $(src_files)
check_pot:
./scripts/check_pot

.PHONY: check_missing_translations
check_missing_translations:
./scripts/check_missing_translations

.PHONY: _touch_pot
_touch_pot:
touch $(locale_src)/texts.pot
Expand Down
116 changes: 116 additions & 0 deletions scripts/check_missing_translations
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/bash

SRC_DIR="./src/"
LOCALE_DIR=./locale/
TMP_DIR=$(mktemp -d)
# Temporary file to store the missing msgid entries along with line numbers
MISSING_MSGIDS_FILE=$TMP_DIR/missing_msgids
# Paths to the old and new .pot files
OLD_POT_FILE="$LOCALE_DIR/texts.pot"
NEW_POT_FILE="$TMP_DIR/new.pot"

# Trap the exit signal to clean the temp folder
trap 'rm -rf "$TMP_DIR"' EXIT

# Generate the new .pot file
npx ttag extract -o "$NEW_POT_FILE" "$SRC_DIR"

# Here we will read both files, one after the other in a single awk script
#
# NR is the number of records, it increments for every line read
# FNR is the current count of lines read for the open file, **so it will reset to 1 when new files are open**
#
# If NR==FNR, we're in the first file, as NR and FNR will be incremented together
# So, when processing the first file, the only thing we'll do is to add the msgid
# to the msgid[] array.
#
# When processing the second file, the awk script will check if the msgid is not
# present, and if it's not, it will print the line number (FNR) with the msgid text
#
# The result of this will be a file (MISSING_MSGIDS_FILE) with all the missing msgids
# with one msgid and its line number per line, here is a result example:
#
# 229:Reject
# 230:Approve
# 231:Connect to this dApp?
# 233:Sign this message?
# 235:Custom network
#
# We will use this later on to print the missing translations.
#
# Here is a pseudocode to help understand what's happening here:
#
# if (NR == FNR) {
# msgid[$0] = 1; // Storing the line in the associative array msgid
# next(); // Skip to the next record
# } else {
# if (!($0 in msgid)) {
# print FNR ":" $0; // Print the line number and the line itself
# }
# }
awk 'NR==FNR {msgid[$0]; next} !($0 in msgid) {print FNR ":" $0}' \
<(awk -F'"' '/msgid/ {print $2}' "$OLD_POT_FILE") \
<(awk -F'"' '/msgid/ {print $2}' "$NEW_POT_FILE") \
> "$MISSING_MSGIDS_FILE"

# Function to extract msgstr for a given msgid from a .po file
extract_msgstr() {
local msgid=$1
local po_file=$2
local msgstr=""
local in_msgid_block=false
while IFS= read -r line; do
if [[ "$line" == "msgid \"$msgid\"" ]]; then
in_msgid_block=true
elif $in_msgid_block && [[ "$line" == "msgstr "* ]]; then
msgstr=$(awk -F'"' '{print $2}' <<< "$line")
break
elif $in_msgid_block && [[ "$line" == "" ]]; then
in_msgid_block=false
fi
done < "$po_file"
echo "$msgstr"
}

# Function to extract source code line number for a given msgid from the .pot file
extract_line_number() {
local msgid=$1
local pot_file=$2
local line_number=""
while IFS= read -r line; do
if [[ "$line" == "#: "* ]]; then
line_number=$(awk '{print $2}' <<< "$line")
elif [[ "$line" == "msgid \"$msgid\"" ]]; then
echo "$line_number"
break
fi
done < "$pot_file"
}

exit_code=0;

echo "check_pot: Checking missing translations on pot file..."

# Check each missing msgid in the .po files
while IFS= read -r entry; do
msgid=$(cut -d':' -f2 <<< "$entry")

code_line_number=$(extract_line_number "$msgid" "$NEW_POT_FILE")

translation_found=false

for po_file in $(find "locale/" -name "*.po" -type f); do
msgstr=$(extract_msgstr "$msgid" "$po_file")
if [ -n "$msgstr" ]; then
translation_found=true
break
fi
done

if [ "$translation_found" = false ]; then
exit_code=1;
echo "Missing translation for msgid \"$msgid\" ($code_line_number)"
fi
done < "$MISSING_MSGIDS_FILE"

exit $exit_code
1 change: 1 addition & 0 deletions scripts/check_pot
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ trap 'rm -rf "$MYTMPDIR"' EXIT
npx ttag extract -o "$MYTMPDIR/pot" "$SRCDIR"
msgen -o "$MYTMPDIR/po" "$MYTMPDIR/pot"
msgcmp "$MYTMPDIR/po" "$LOCALE/texts.pot"

exit $?

0 comments on commit b5e2fb5

Please sign in to comment.