[Automated] Crowdin synchronization (#402) #509
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
name: Automated compiling of translations | |
on: | |
push: | |
branches: main | |
ignore_paths: | |
- "**/*.mo" | |
- "messages.pot" | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
name: Compile to .mo | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Install gettext | |
run: sudo apt-get update && sudo apt-get install gettext -y | |
- name: Generate .mo files | |
run: | | |
for po_file in $(find . -name "*.po"); do | |
dirname=$(dirname $po_file) | |
filename=$(basename $po_file .po) | |
msgfmt $po_file -o "$dirname/$filename.mo" | |
done | |
- name: Add .mo files to git | |
run: git add . | |
- name: (Workaround) Rename the Serbian Folder Name | |
run: | | |
if [ -d ./srp ]; then | |
# Ensure the correctly named folder is removed | |
if [ -d ./sr_RS ]; then | |
rm -rf ./sr_RS | |
fi | |
# Now rename the folder within git | |
git mv ./srp ./sr_RS | |
fi | |
- name: Commit and push .mo files | |
uses: actions-js/push@v1.5 | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
message: "Automated .mo file generation" | |
- name: Create artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: translations | |
path: | | |
./**/messages.mo | |
./**/messages.po | |
locales.php | |
LICENSE.md | |
messages.pot | |
readme.md | |
release: | |
name: Publish to release | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Download the artifact | |
uses: actions/download-artifact@v4 | |
- name: Calculate the Translation Tag (messages.pot hash) | |
working-directory: translations | |
run: echo "TRANSLATION_TAG=$(md5sum messages.pot | cut -d' ' -f1)" >> $GITHUB_ENV | |
- name: Display structure of downloaded files | |
run: ls -R | |
- name: Install gettext | |
run: sudo apt-get update && sudo apt-get install gettext -y | |
- name: Remove translations with low completion | |
working-directory: translations | |
run: | | |
now=$(date) | |
echo "## FOSSBilling Translations" >>translations.md | |
echo "Dated: $now" >>translations.md | |
echo "Translation Tag: [$TRANSLATION_TAG](https://github.com/FOSSBilling/locale/releases/tag/$TRANSLATION_TAG)" >>translations.md | |
rm -f messages.pot | |
# Create an associative array to store completion percentages | |
declare -A completion_array | |
for po_file in $(find . -name "*.po"); do | |
dirname=$(dirname "$po_file") | |
translation=$(dirname "$dirname") | |
# Use msgfmt to generate temporary file with statistics | |
msgfmt --statistics "$po_file" >/tmp/msgfmt_stats.tmp 2>&1 | |
# Capture the statistics output | |
statistics=$(cat /tmp/msgfmt_stats.tmp) | |
# Remove the temporary file | |
rm /tmp/msgfmt_stats.tmp | |
# Extract the number of translated and total messages | |
translated_messages=$(echo "$statistics" | awk -F ',' '{print $1}' | awk '{print $1}') | |
untranslated_messages=$(echo "$statistics" | awk -F ',' '{print $2}' | awk '{print $1}') | |
total_messages=$((translated_messages + untranslated_messages)) | |
# Calculate completion percentage | |
if [ -n "$translated_messages" ] && [ -n "$total_messages" ]; then | |
completion_percentage=$((translated_messages * 100 / total_messages)) | |
else | |
completion_percentage=0 | |
fi | |
completeBars=$(($completion_percentage / 10)) | |
incompleteBars=$((10 - $completeBars)) | |
bar="" | |
for ((i = 0; i < completeBars; i++)); do | |
bar+="▰" | |
done | |
for ((i = 0; i < incompleteBars; i++)); do | |
bar+="▱" | |
done | |
# If the translation is less than 25% complete, delete it entirely. Otherwise, just remove the source file to make things cleaner | |
if [ "$completion_percentage" -lt 25 ] && [ "$dirname" != "./en_US/LC_MESSAGES" ]; then | |
rm -rf "$translation" | |
translation=${translation#./} | |
completion_array["$translation"]=$completion_percentage | |
echo " - \`$translation\`: $bar $completion_percentage% translated (excluded, too incomplete)" >>translations.md | |
else | |
if [ "$dirname" != "./en_US/LC_MESSAGES" ]; then | |
translation=${translation#./} | |
echo " - \`$translation\`: $bar $completion_percentage% translated" >>translations.md | |
completion_array["$translation"]=$completion_percentage | |
fi | |
rm -f "$dirname/messages.po" | |
fi | |
done | |
# Create a PHP file with the completion percentages | |
echo '<?php' > completion.php | |
echo '/**' >> completion.php | |
echo ' * FOSSBilling translations' >> completion.php | |
echo " * Dated: $now" >> completion.php | |
echo ' * License: https://github.com/FOSSBilling/locale/blob/main/LICENSE.md' >> completion.php | |
echo ' */' >> completion.php | |
echo 'return [' >> completion.php | |
for locale in "${!completion_array[@]}"; do | |
percentage=${completion_array[$locale]} | |
echo " '$locale' => $percentage," >> completion.php | |
done | |
echo '];' >> completion.php | |
rm -f messages.mo | |
- name: Create a zip of the translations | |
run: zip -r ./translations.zip . | |
working-directory: translations | |
- name: Release to GitHub | |
uses: ncipollo/release-action@v1 | |
with: | |
name: Translations (${{ env.TRANSLATION_TAG }}) | |
tag: ${{ env.TRANSLATION_TAG }} | |
commit: main | |
bodyFile: translations/translations.md | |
artifacts: translations/translations.zip | |
allowUpdates: true |