Enhanced missing gettext translations check in CI

almirsarajcic

almirsarajcic

Created 15 days ago

Earlier I wrote about a check for strings that haven’t been extracted yet to .pot and .po gettext files (link).

Still, extracting them is not enough. Empty msgstr fields would pass that check. Building on top of that check, we need to make sure all of the extracted strings are translated.

That’s why our Makefile looks like this:

ci:
  # ...
  make check_gettext check_translations

check_gettext:
  # ...

check_translations:
	@echo "Checking for missing translations..."
	@missing=$$($(MAKE) -s _find_missing_translations); \
	if [ -n "$$missing" ]; then \
		echo "Missing translations found:"; \
		echo "$$missing"; \
		echo ""; \
		echo "To fix this:"; \
		echo "1. Run 'make show_missing_translations' to see detailed missing translations"; \
		echo "2. Use Cursor to translate the missing strings"; \
		echo "3. Commit the updated translation files"; \
		exit 1; \
	else \
		echo "All translations are complete!"; \
	fi

show_missing_translations:
	@echo "Missing translations:"
	@$(MAKE) -s _find_missing_translations

_find_missing_translations:
	@for file in $$(find priv/gettext -name "*.po" -not -path "*/en/*"); do \
		count=$$(grep -c "^msgstr \"\"$$" "$$file" | grep -v ":9:" || echo 0); \
		if [ "$$count" -gt 1 ]; then \
			echo "$$count $$file"; \
		fi; \
	done | sort -nr

It looks into all .po files that are not in the en directory (because all our strings are already in English, so there’s no need to translate those) and searches for empty msgstr fields, then gives instructions how to fix that. We don’t translate manually, but use Cursor and Claude for that. In your case, you might want different error message.

This check is a part of both our local checks and the CI running in GitHub Actions.