From f7494cab7d1bef474b4bebe8cdac2cd5d3b82fee Mon Sep 17 00:00:00 2001
From: jo <ljonas@riseup.net>
Date: Fri, 15 Oct 2021 00:23:45 +0200
Subject: [PATCH] Add locale Makefile and allow building mo files

---
 legacy/Makefile        |  8 +++++++-
 legacy/locale/Makefile | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 1 deletion(-)
 create mode 100644 legacy/locale/Makefile

diff --git a/legacy/Makefile b/legacy/Makefile
index 21d532566..3c627d9ae 100644
--- a/legacy/Makefile
+++ b/legacy/Makefile
@@ -1,12 +1,18 @@
 SHELL = bash
 
-all: vendor
+all: vendor locale-build
 
 PHP-CS-FIXER = tools/vendor/bin/php-cs-fixer
 $(PHP-CS-FIXER):
 	mkdir -p tools
 	composer require --working-dir=tools friendsofphp/php-cs-fixer
 
+include locale/Makefile
+
+locale-update: .locale-update
+locale-build:
+	$(MAKE) -C locale build
+
 vendor:
 	composer install --no-progress --no-interaction $(COMPOSER_ARGS)
 
diff --git a/legacy/locale/Makefile b/legacy/locale/Makefile
new file mode 100644
index 000000000..a8c762a94
--- /dev/null
+++ b/legacy/locale/Makefile
@@ -0,0 +1,34 @@
+.PHONY: .locale-update build
+.DEFAULT: build
+
+SHELL = bash
+
+DOMAIN = libretime
+PO_FILE = $(DOMAIN).po
+PO_FILES = $(wildcard */LC_MESSAGES/$(PO_FILE))
+MO_FILES = $(PO_FILES:.po=.mo)
+
+SRC = application build public
+
+XGETTEXT_ARGS = --default-domain=$(DOMAIN) \
+				--language=php \
+				--from-code=UTF-8 \
+				--no-wrap \
+				--sort-by-file
+
+MSGMERGE_ARGS = --no-fuzzy-matching \
+				--update \
+				--no-wrap \
+				--sort-by-file
+
+# This target is run by another Makefile
+.locale-update:
+	find $(SRC) -name "*.phtml" -o -name "*.php" -type f -print0 | xargs -0 xgettext $(XGETTEXT_ARGS)
+	sed -i 's/CHARSET/UTF-8/g' $(PO_FILE)
+	find ./locale -name $(PO_FILE) -exec msgmerge $(MSGMERGE_ARGS) "{}" $(PO_FILE) \;
+	rm $(PO_FILE)
+
+%.mo: %.po
+	msgfmt $< -o $@
+
+build: $(MO_FILES)