<project name="BuildI18n" default="build">
	<!--
		Builds the i18n resource bundles
	-->

	<import file="build-import.xml"/>
	<property name="i18n.build.target.dir" location="${tmp.dir}/i18n"/>
	<!--
		Regarding default.i18n.locale...
		Must match one of the locales we have provided source properties for in i18n directory.
		Pick a locale that has a complete set of messages because any incomplete locales will
		"inherit" missing messages from the default.
	-->
	<property name="default.i18n.locale" value="en"/>

	<target name="init" depends="clean">
		<mkdir dir="${i18n.unpack.dir}" />
		<mkdir dir="${i18n.build.target.dir}"/>
		<condition property="i18n.resources.exists">
			<available file="${i18n.resources.dir}" type="dir"/>
		</condition>
	</target>

	<target name="build" depends="init, copyResources, buildJsonBundles" description="Builds i18n resource bundles">
		<!-- i18n -->
		<copy todir="${resource.build.target.dir}/translation" overwrite="true">
			<fileset dir="${i18n.build.target.dir}" includes="*.json"/>
		</copy>
	</target>

	<target name="copyResources" if="${i18n.resources.exists}">
		<echo message="Copy i18n resources from ${i18n.resources.dir}"/>
		<copy todir="${i18n.unpack.dir}">
			<fileset dir="${i18n.resources.dir}" />
		</copy>
	</target>

	<target name="buildJsonBundles">
		<property name="propertiesToJson.js" value="${component.rootdir}/propertiesToJson.js" />
		<property name="default.locale.properties" location="${i18n.build.target.dir}/${default.i18n.locale}.properties"/>
		<property name="default.locale.json" location="${i18n.build.target.dir}/${default.i18n.locale}.json"/>
		<buildProps destfile="${default.locale.properties}"/>
		<nodejs.exec failonerror="true" cwd="${target.rootdir}">
			<arguments>
				<arg value="${propertiesToJson.js}"/>
				<arg value="${default.locale.properties}"/>
				<arg value="${default.locale.json}"/>
			</arguments>
		</nodejs.exec>
	</target>

	<!--
		Builds a properties file from the source files in the core and implementation directories.
		This is largely for backwards compatibility.
	-->
	<macrodef name="buildProps">
		<attribute name="filename" default="theme-messages.properties"/>
		<attribute name="destfile"/>
		<sequential>
			<concat fixlastline="true" append="false" destfile="@{destfile}">
				<path>
					<fileset dir="${i18n.unpack.dir}/com/github/bordertech/wcomponents" includes="@{filename}" />
				</path>
				<filterchain>
					<expandproperties/>
				</filterchain>
			</concat>
		</sequential>
	</macrodef>

	<target name="clean" description="Cleans up all artifacts produced by this build">
		<echo message="Clean from ${ant.file}"/>
		<delete dir="${i18n.build.target.dir}"/>
	</target>
</project>