# -*- Makefile -*-
# This file was generated by the i18n leiningen plugin
# Do not edit this file; it will be overwritten the next time you run
#   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 tranlsations will be placed
PACKAGE=puppetlabs.i18n
LOCALES=$(basename $(notdir $(wildcard locales/*.po)))
PACKAGE_DIR=$(subst .,/,$(PACKAGE))
BUNDLE_FILES=$(patsubst %,resources/$(PACKAGE_DIR)/Messages_%.class,$(LOCALES) $(MESSAGE_LOCALE))
SRC_FILES=$(shell find src -name \*.clj)

i18n: setup update-pot msgfmt

# Update locales/messages.pot
update-pot: locales/messages.pot

locales/messages.pot: $(SRC_FILES)
	@tmp=$(mktemp $@.tmp.XXXX);                                              \
	find src -name \*.clj                                                    \
	    | xgettext --from-code=UTF-8 --language=lisp                         \
	               --copyright-holder 'Puppet Labs <docs@puppetlabs.com>' -F \
	               --package-name "$(PACKAGE)"                               \
	               --package-version "$(PACKAGE_VERSION)"                    \
	               --msgid-bugs-address "docs@puppetlabs.com"                \
	               -ktrs:1 -ki18n/trs:1                                      \
	               -ktru:1 -ki18n/tru:1                                      \
	               -ktrun:1,2 -ki18n/trun:1,2                                \
	               -ktrsn:1,2 -ki18n/trsn:1,2                                \
	               --add-comments -o $tmp -f -;                              \
	sed -i -e 's/charset=CHARSET/charset=UTF-8/' $tmp;                       \
	sed -i -e 's/POT-Creation-Date: [^\\]*/POT-Creation-Date: /' $tmp;       \
	if ! diff -q -I POT-Creation-Date $tmp $@ >& /dev/null; then             \
		mv $tmp $@;                                                          \
	else                                                                     \
		rm $tmp;                                                             \
	fi

# Run msgfmt over all .po files to generate Java resource bundles
msgfmt: $(BUNDLE_FILES)
	@(printf '{\n  :locales #{'; \
	  printf "\"%s\"" $(MESSAGE_LOCALE); \
	  for l in $(LOCALES); do printf " \"%s\"" $$l; done; \
	  printf '}\n'; \
	  printf '  :package "$(PACKAGE)"\n'; \
	  printf '  :bundle  "$(PACKAGE).Messages"\n}\n') > resources/locales.clj

resources/$(PACKAGE_DIR)/Messages_%.class: locales/%.po
	msgfmt --java2 -d resources -r $(PACKAGE).Messages -l $$(basename $< .po) $<

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

# Translators use this when they update translations; this copies any
# changes in the pot file into their language-specific po file
locales/%.po: locales/messages.pot
	@if [ -f $@ ]; then												\
		msgmerge -U $@ $< && touch $@; 								\
	else															\
		touch $@ && 												\
		msginit --no-translator -l $$(basename $@ .po) -o $@ -i $<;	\
	fi

help:
	$(info $(HELP))
	@echo

setup:
	@mkdir -p locales

.PHONY:setup help

define HELP
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/$$LANG.po       - the translations for $$LANG

Only the $$LANG.po files should be edited manually; this is usually done by
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
  locales/LANG.po:  refresh or create translations for LANG
  msgfmt:           compile the translations into Java classes; this step is
                    needed to make translations available to the Clojure code
                    and produces Java class files in resources/
endef
# @todo lutter 2015-04-20: for projects that use libraries with their own
# translation, we need to combine all their translations into one big po
# file and then run msgfmt over that so that we only have to deal with one
# resource bundle
