Skip to content

How to create a maven project with gettext

Oliver edited this page Feb 5, 2019 · 1 revision

Prerequisites

Install GNU gettext for your system and ensure its bin directory is part of you path variable.

For most operating systems this is straight forward. If using Mac OS and Eclipse make sure to add the bin directory to /etc/paths (i. e. "/usr/local/opt/gettext/bin") and do not start Eclipse.app but Eclipse.app/Contents/MacOS/eclipse.

Create Maven project

Create a maven project or use an existing one and add i. e. the following configuration. The example uses generated java source files. Alternatives are .class files or .properties files. An Eclipse Lifecycle Mapping is included.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>gettext-example</groupId>
	<artifactId>gettext-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>gettext-example</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<build>
		<!-- Use default directory layout for Java classes -->
		<sourceDirectory>src/main/java</sourceDirectory>
		<plugins>
			<!-- Add a generated sources directory (can be cleaned up easily before generating) -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>3.0.0</version>
				<executions>
					<execution>
						<phase>generate-sources</phase>
						<goals>
							<goal>add-source</goal>
						</goals>
						<configuration>
							<sources>
								<source>src/generated/java</source>
							</sources>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<!-- Add Gettext Maven Plugin -->
			<plugin>
				<groupId>com.github.vlsi.gettext</groupId>
				<artifactId>gettext-maven-plugin</artifactId>
				<version>1.4.0</version>
				<configuration>
					<!-- This example defines only _ as method name (i. e. System.out.println(_("Translate this")); -->
					<keywords>-k_ -k</keywords> <!-- -k overwrites the defaults to only use what is defined in the keywords -->
					<!-- Directory for the generated keys.pot files and language files -->
					<poDirectory>src/main/resources/po</poDirectory>
					<!-- name of the resource bundle to be generated -->
					<targetBundle>gettext_example.Messages</targetBundle>
					<!-- Set the previously set sources directory as output directory -->
					<outputDirectory>src/generated/java</outputDirectory>
					<!-- Generate a java class (alternatives are .properties and .class files -->
					<outputFormat>java</outputFormat>
				</configuration>
				<executions>
					<execution>
						<!-- Execute the plugin in the generate-sources phase -->
						<phase>generate-sources</phase>
						<goals>
							<!-- The dist goal calls the gettext tools to generate the keys.pot file and processes the language files that lie next to this keys file to generate the Java source, class or the properties file -->
							<goal>dist</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<pluginManagement>
			<plugins>
				<!--This plugin's configuration is used to store Eclipse m2e settings 
					only. It has no influence on the Maven build itself. -->
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>
											com.github.vlsi.gettext
										</groupId>
										<artifactId>
											gettext-maven-plugin
										</artifactId>
										<versionRange>
											[1.4.0,)
										</versionRange>
										<goals>
											<goal>dist</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<execute></execute>
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

Clone this wiki locally