Skip to content

Commit

Permalink
Update scripts to work with separated full files
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Sep 26, 2023
1 parent d58a68d commit e58ff1f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 110 deletions.
109 changes: 55 additions & 54 deletions convert_csv.sh
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
#!/bin/bash

cd languages || exit

POT_FILE="en_GB.pot"
PO_FILE="mollie-payments-for-woocommerce-nl_NL.po"
TEMP_POT="temp_untranslated.pot"
CSV_FILE="en_GB.csv"
CSV_DIR="untranslated_csv"

if [[ ! -e $POT_FILE ]]; then
echo "File $POT_FILE not found."
exit 1
fi
LANG_EXT=("-de_DE" "-de_DE_formal" "-es_ES" "-fr_FR" "-it_IT" "-nl_NL" "-nl_NL_formal" "-nl_BE" "-nl_BE_formal")

if [[ ! -e $PO_FILE ]]; then
if [[ ! -e $POT_FILE ]]; then
echo "File $POT_FILE not found."
exit 1
fi

current_msgid=""
current_msgctxt=""
block=""
mkdir -p $CSV_DIR # Create the directory if it does not exist

while IFS= read -r line; do
block="$block$line\n"
if [[ "$line" =~ ^msgid\ \"(.+)\"$ ]]; then
current_msgid="${BASH_REMATCH[1]}"
fi

if [[ "$line" =~ ^msgctxt\ \"(.+)\"$ ]]; then
current_msgctxt="${BASH_REMATCH[1]}"
fi
# Loop through each language extension
for lang in "${LANG_EXT[@]}"; do
PO_FILE="mollie-payments-for-woocommerce${lang}.po"
CSV_FILE="$CSV_DIR/mollie-payments-for-woocommerce${lang}.csv"
UNTRANSLATED_FILE="$CSV_DIR/mollie-payments-for-woocommerce${lang}_untranslated.csv"

if [[ "$line" =~ ^msgstr ]]; then
if ! grep -Fq "msgid \"$current_msgid\"" "$PO_FILE" || ( [ -n "$current_msgctxt" ] && ! grep -Fq "msgctxt \"$current_msgctxt\"" "$PO_FILE" ); then
echo -e "$block" >> $TEMP_POT
fi
current_msgid=""
current_msgctxt=""
block=""
if [[ -e $PO_FILE ]]; then
msgmerge --update --no-wrap --backup=none "$PO_FILE" "$POT_FILE" # Update PO file with POT file
echo "Updated $PO_FILE with new strings from $POT_FILE"
else
echo "File $PO_FILE not found. Skipping..."
continue
fi
done < "$POT_FILE"

echo 'msgid,msgstr,msgctxt,location,-de_DE, -de_DE_formal, -es_ES, -fr_FR, -it_IT, -nl_NL, -nl_NL_formal, -nl_BE, -nl_BE_formal' > $CSV_FILE
location=""
msgid=""
msgstr=""
msgctxt=""
echo 'location,msgid,msgstr,msgctxt' > "$CSV_FILE"
echo 'line,msgid' > "$UNTRANSLATED_FILE"

while IFS= read -r line; do
if [[ "$line" =~ ^\#:\ (.+)$ ]]; then
location="${BASH_REMATCH[1]}"
fi
# Initialize variables
location=""
msgid=""
msgstr=""
msgctxt=""
line_no=0

if [[ "$line" =~ ^msgid\ \"(.+)\"$ ]]; then
msgid="${BASH_REMATCH[1]}"
fi

if [[ "$line" =~ ^msgstr\ \"(.+)\"$ ]]; then
msgstr="${BASH_REMATCH[1]}"
fi

if [[ "$line" =~ ^msgctxt\ \"(.+)\"$ ]]; then
msgctxt="${BASH_REMATCH[1]}"
fi

if [[ "$line" =~ ^msgstr ]]; then
echo "\"$msgid\",\"$msgstr\",\"$msgctxt\",\"$location\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\" " >> $CSV_FILE
# Function to write to CSV
write_to_csv() {
echo "\"$location\",\"$msgid\",\"$msgstr\",\"$msgctxt\"" >> "$CSV_FILE"
if [[ -z "$msgstr" && -n "$msgid" ]]; then
echo "$line_no,\"$msgid\"" >> "$UNTRANSLATED_FILE"
fi
# Reset variables
location=""
msgid=""
msgstr=""
msgctxt=""
fi
}

while IFS= read -r line || [[ -n "$line" ]]; do # also handle the last line
((line_no++))

done < "$TEMP_POT"
if [[ "$line" =~ ^\#:\ (.+)$ ]]; then
location="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^msgid\ \"(.*)\"$ ]]; then
if [[ -n "$msgid" || -n "$msgstr" ]]; then
write_to_csv
fi
msgid="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^msgstr\ \"(.*)\"$ ]]; then
msgstr="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^msgctxt\ \"(.+)\"$ ]]; then
msgctxt="${BASH_REMATCH[1]}"
fi
done < "$PO_FILE"

# For the last msgid in the file
if [[ -n "$msgid" || -n "$msgstr" ]]; then
write_to_csv
fi

rm $TEMP_POT
echo "Created CSV $CSV_FILE and $UNTRANSLATED_FILE for $PO_FILE"
done
83 changes: 27 additions & 56 deletions csv-to-po.sh
Original file line number Diff line number Diff line change
@@ -1,59 +1,30 @@
#!/bin/bash

cd languages || exit

CSV_FILE="en_GB.csv"
OUTPUT_DIR="intermediate_po"
TEMPLATE_POT="en_GB.pot"

if [[ ! -e $CSV_FILE ]]; then
echo "File $CSV_FILE not found."
exit 1
fi

mkdir -p $OUTPUT_DIR

# Remove surrounding quotes
trim_quotes() {
local val="$1"
echo "$val" | sed -e 's/^"//' -e 's/"$//'
}

# Parse the header to get the list of languages
IFS=',' read -ra HEADER <<< "$(head -n 1 $CSV_FILE)"

# For each language in the CSV, create an intermediate .po file
for i in "${!HEADER[@]}"; do
if (( $i > 3 )); then
LANGUAGE=$(trim_quotes "${HEADER[$i]}")
echo -n "" > "$OUTPUT_DIR/$LANGUAGE.po"
while IFS=',' read -ra LINE; do
# Skip the header line
if [[ "${LINE[0]}" != "msgid" ]]; then
if [[ -n "${LINE[3]}" ]]; then
echo "#: $(trim_quotes "${LINE[3]}")" >> "$OUTPUT_DIR/$LANGUAGE.po"
fi
if [[ -n "${LINE[2]}" ]]; then
echo "msgctxt \"$(trim_quotes "${LINE[2]}")\"" >> "$OUTPUT_DIR/$LANGUAGE.po"
fi
echo "msgid \"$(trim_quotes "${LINE[0]}")\"" >> "$OUTPUT_DIR/$LANGUAGE.po"
echo "msgstr \"$(trim_quotes "${LINE[$i]}")\"" >> "$OUTPUT_DIR/$LANGUAGE.po"
echo "" >> "$OUTPUT_DIR/$LANGUAGE.po"
fi
done < "$CSV_FILE"
fi
done
# Append intermediate .po to existing .po
for po in $OUTPUT_DIR/*.po; do
BASENAME=$(basename "$po" ".po" | tr -d ' ')
EXISTING_PO="mollie-payments-for-woocommerce${BASENAME}.po"
if [[ -e $EXISTING_PO ]]; then
cat "$po" >> $EXISTING_PO
fi
done

# Compile .po to .mo
for po in *.po; do
MO_FILE="${po%.po}.mo"
msgfmt "$po" -o "$MO_FILE"
CSV_DIR="languages/translated_csv"
PO_DIR="languages"
MO_DIR="languages"

for csv_file in "$CSV_DIR"/*.csv; do
[ -e "$csv_file" ] || continue

base_name=$(basename -- "$csv_file" .csv)
po_file="$PO_DIR/$base_name.po"
mo_file="$MO_DIR/$base_name.mo"

awk -F'"' '
BEGIN { OFS=""; print "msgid \"\""; print "msgstr \"\""; print "\"Content-Type: text/plain; charset=UTF-8\\n\""; }
NR > 1 {
gsub(/\\/, "\\\\", $2);
gsub(/\"/, "\\\"", $2);
gsub(/\\/, "\\\\", $4);
gsub(/\"/, "\\\"", $4);
print "\n#: " $2;
print "msgid \"" $4 "\"";
print "msgstr \"" $6 "\"";
}' "$csv_file" > "$po_file"

echo "Created PO file: $po_file"

msgfmt "$po_file" -o "$mo_file"
echo "Created MO file: $mo_file"
done

0 comments on commit e58ff1f

Please sign in to comment.