Skip to content

Commit

Permalink
Add csv to po action
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaymo committed Sep 15, 2023
1 parent b237567 commit 00efaa4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/csv-to-po.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
me: CSV-To-PO

on:
workflow_dispatch:
inputs:
PACKAGE_VERSION:
description: 'Package Version'
required: true
jobs:
languages-diff:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: "Create CSV of diff translations"
run: |
chmod +x ./csv-to-po.sh
./csv-to-po.sh
- name: Set artifact name
id: set-artifact-name
run: echo "artifact=mollie-payments-for-woocommerce-languages-${{ inputs.PACKAGE_VERSION }}" >> $GITHUB_OUTPUT

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: ${{ steps.set-artifact-name.outputs.artifact }}
path: languages/*
59 changes: 59 additions & 0 deletions csv-to-po.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/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"
done

0 comments on commit 00efaa4

Please sign in to comment.