Skip to content

Commit

Permalink
(maint) update i18n Makefile
Browse files Browse the repository at this point in the history
The makefile was out of date. This updates it to be more recent.
  • Loading branch information
jonathannewman committed Apr 4, 2024
1 parent 0e54582 commit 38e9e27
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions dev-resources/Makefile.i18n
Expand Up @@ -4,20 +4,20 @@
# lein i18n init
#

# The locale in which our messages are written, and for which we therefore
# have messages without any further effort
MESSAGE_LOCALE=en

# The name of the package into which the translations bundle will be placed
BUNDLE=puppetlabs.trapperkeeper_metrics

# The name of the POT file into which the gettext code strings (msgid) will be placed
POT_NAME=trapperkeeper-metrics.pot

# The list of names of packages covered by the translation bundle;
# by default it contains a single package - the same where the translations
# bundle itself is placed - but this can be overridden - preferably in
# the top level Makefile
PACKAGES?=$(BUNDLE)
LOCALES=$(basename $(notdir $(wildcard locales/*.po)))
BUNDLE_DIR=$(subst .,/,$(BUNDLE))
BUNDLE_FILES=$(patsubst %,resources/$(BUNDLE_DIR)/Messages_%.class,$(MESSAGE_LOCALE) $(LOCALES))
BUNDLE_FILES=$(patsubst %,resources/$(BUNDLE_DIR)/Messages_%.class,$(LOCALES))
FIND_SOURCES=find src -name \*.clj
# xgettext before 0.19 does not understand --add-location=file. Even CentOS
# 7 ships with an older gettext. We will therefore generate full location
Expand All @@ -27,7 +27,7 @@ LOC_OPT=$(shell xgettext --add-location=file -f - </dev/null >/dev/null 2>&1 &&
LOCALES_CLJ=resources/locales.clj
define LOCALES_CLJ_CONTENTS
{
:locales #{$(patsubst %,"%",$(MESSAGE_LOCALE) $(LOCALES))}
:locales #{$(patsubst %,"%",$(LOCALES))}
:packages [$(patsubst %,"%",$(PACKAGES))]
:bundle $(patsubst %,"%",$(BUNDLE).Messages)
}
Expand All @@ -37,10 +37,10 @@ export LOCALES_CLJ_CONTENTS

i18n: msgfmt

# Update locales/messages.pot
update-pot: locales/messages.pot
# Update locales/<project-name>.pot
update-pot: locales/$(POT_NAME)

locales/messages.pot: $(shell $(FIND_SOURCES)) | locales
locales/$(POT_NAME): $(shell $(FIND_SOURCES)) | locales
@tmp=$$(mktemp $@.tmp.XXXX); \
$(FIND_SOURCES) \
| xgettext --from-code=UTF-8 --language=lisp \
Expand Down Expand Up @@ -68,29 +68,37 @@ locales/messages.pot: $(shell $(FIND_SOURCES)) | locales

# Run msgfmt over all .po files to generate Java resource bundles
# and create the locales.clj file
msgfmt: $(BUNDLE_FILES) $(LOCALES_CLJ)
msgfmt: $(BUNDLE_FILES) $(LOCALES_CLJ) clean-orphaned-bundles

# force rebuild of locales.clj if its contents is not the
# the desired one
ifneq ($(shell cat $(LOCALES_CLJ) 2> /dev/null),$(shell echo '$(subst ','\'',$(LOCALES_CLJ_CONTENTS))'))
# Force rebuild of locales.clj if its contents is not the the desired one. The
# shell echo is used to add a trailing newline to match the one from `cat`
ifneq ($(shell cat $(LOCALES_CLJ) 2> /dev/null),$(shell echo '$(LOCALES_CLJ_CONTENTS)'))
.PHONY: $(LOCALES_CLJ)
endif
$(LOCALES_CLJ): | resources
@echo "Writing $@"
@echo "$$LOCALES_CLJ_CONTENTS" > $@

# Remove every resource bundle that wasn't generated from a PO file.
# We do this because we used to generate the english bundle directly from the POT.
.PHONY: clean-orphaned-bundles
clean-orphaned-bundles:
@for bundle in resources/$(BUNDLE_DIR)/Messages_*.class; do \
locale=$$(basename "$$bundle" | sed -E -e 's/\$$?1?\.class$$/_class/' | cut -d '_' -f 2;); \
if [ ! -f "locales/$$locale.po" -a -f "$$bundle" ]; then \
rm "$$bundle"; \
fi \
done

resources/$(BUNDLE_DIR)/Messages_%.class: locales/%.po | resources
msgfmt --java2 -d resources -r $(BUNDLE).Messages -l $(*F) $<

resources/$(BUNDLE_DIR)/Messages_$(MESSAGE_LOCALE).class: locales/messages.pot | resources
msgfmt --java2 -d resources -r $(BUNDLE).Messages -l $(MESSAGE_LOCALE) $<

# Use this to initialize translations. Updating the PO files is done
# automatically through a CI job that utilizes the scripts in the project's
# `bin` file, which themselves come from the `clj-i18n` project.
locales/%.po: | locales
@if [ ! -f $@ ]; then \
touch $@ && msginit --no-translator -l $(*F) -o $@ -i $<; \
touch $@ && msginit --no-translator -l $(*F) -o $@ -i locales/$(POT_NAME); \
fi

resources locales:
Expand All @@ -107,7 +115,7 @@ This Makefile assists in handling i18n related tasks during development. Files
that need to be checked into source control are put into the locales/ directory.
They are

locales/messages.pot - the POT file generated by 'make update-pot'
locales/$(POT_NAME) - the POT file generated by 'make update-pot'
locales/$$LANG.po - the translations for $$LANG

Only the $$LANG.po files should be edited manually; this is usually done by
Expand All @@ -116,7 +124,7 @@ translators.
You can use the following targets:

i18n: refresh all the files in locales/ and recompile resources
update-pot: extract strings and update locales/messages.pot
update-pot: extract strings and update locales/$(POT_NAME)
locales/LANG.po: create translations for LANG
msgfmt: compile the translations into Java classes; this step is
needed to make translations available to the Clojure code
Expand Down

0 comments on commit 38e9e27

Please sign in to comment.