Skip to content

Commit

Permalink
Merge pull request #830 from mollie/task/PIWOO-186-pot-generation-action
Browse files Browse the repository at this point in the history
Add actions to update pot and diff the translation files
  • Loading branch information
mmaymo authored Sep 26, 2023
2 parents b8f4d44 + 2b051cc commit 12e2f33
Show file tree
Hide file tree
Showing 25 changed files with 24,155 additions and 6,432 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/housekeeping.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Housekeeping

on:
workflow_dispatch:
push:
branches-ignore:
# We expect PRs to be merged when they are "clean". After merge, we don't need to re-trigger on develop
- 'develop'

jobs:
housekeeping:
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the changed files back to the repository.
contents: write
runs-on: ubuntu-latest
steps:
- uses: "actions/checkout@v2"
- name: Store branch and latest SHA
id: git
run: |
echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
- uses: "shivammathur/setup-php@v2"
with:
php-version: "7.2"
- uses: "ramsey/composer-install@v2"
with:
composer-options: "--prefer-dist"
- name: "Run PHPCBF"
run: "composer fix-coding-standards || true"

- name: "Install node modules"
run: "npm install"

- name: "Install WP-CLI"
run: |
mkdir ~/wp-cli && cd ~/wp-cli
curl -L https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar --output wp
chmod +x wp
sudo ln -s ~/wp-cli/wp /usr/local/bin/wp
- name: "Create updated .pot file"
run: |
wp i18n make-pot . ./languages/en_GB.pot --skip-audit --allow-root
# For now, any workflow run would result in a commit due to the updated timestamp even if there are no changes in translations
# We should figure out a way to only set the timestamp when there are new translations, but for now,
# let's just remove that timestamp entirely
- name: "Remove POT-Creation-Date"
run: sed -i '/POT-Creation-Date/d' ./languages/en_GB.pot

- uses: "stefanzweifel/git-auto-commit-action@v4"
with:
push_options: --force
# This task should never update dependencies
file_pattern: :!*.lock
27 changes: 27 additions & 0 deletions .github/workflows/languages-diff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
me: Languages-diff

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 ./convert_csv.sh
./convert_csv.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/*
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/tools/
/build/
/tests/e2e/Shared/default.json

package-lock.json
.env
cypress.json
test-results/
Expand Down
35 changes: 34 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions convert_csv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

cd languages || exit

POT_FILE="en_GB.pot"
CSV_DIR="untranslated_csv"

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 $POT_FILE ]]; then
echo "File $POT_FILE not found."
exit 1
fi

mkdir -p $CSV_DIR # Create the directory if it does not exist

# 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 [[ -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

echo 'location,msgid,msgstr,msgctxt' > "$CSV_FILE"
echo 'line,msgid' > "$UNTRANSLATED_FILE"

# Initialize variables
location=""
msgid=""
msgstr=""
msgctxt=""
line_no=0

# 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=""
}

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

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

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

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
2 changes: 1 addition & 1 deletion inc/settings/mollie_advanced_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
/* translators: Placeholder 1: enabled or disabled */
'desc' => sprintf(
__(
'Should Mollie store customers name and email address for Single Click Payments? Default <code>%1$s</code>. Required if WooCommerce Subscriptions is being used! Read more about <a href="https://help.mollie.com/hc/en-us/articles/115000671249-What-are-single-click-payments-and-how-does-it-work-">%2$s</a> and how it improves your conversion.',
'Should Mollie store customers name and email address for Single Click Payments? Default <code>%1$s</code>. Required if WooCommerce Subscriptions is being used! Read more about <a href=\'https://help.mollie.com/hc/en-us/articles/115000671249-What-are-single-click-payments-and-how-does-it-work-\'>%2$s</a> and how it improves your conversion.',
'mollie-payments-for-woocommerce'
),
strtolower(__('Enabled', 'mollie-payments-for-woocommerce')),
Expand Down
Loading

0 comments on commit 12e2f33

Please sign in to comment.