Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: check missing translations #366

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +40 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By adding this to the current CI workflow, we trigger an error because of the current 33 missing translations.

We could either:

  • Add them within the scope of this PR or;
  • Leave the workflow unchanged and add the translations + workflow on another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're going to be added after @alexruzenhack PR's are merged

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
120 changes: 120 additions & 0 deletions scripts/check_missing_translations
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/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"

if [ "$exit_code" -eq 0 ]; then
echo "No missing translations found."
fi

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 $?
Loading