-
Notifications
You must be signed in to change notification settings - Fork 22
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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 |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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