Skip to content

Gradle Plugin

Thomas Cashman edited this page Nov 19, 2020 · 7 revisions

The Gradle plugin is similar xgettext - it extracts translatable strings from source code and generates a .pot file (.po template).

Set up

First, add the plugin to your build.gradle

buildscript {
    repositories {
	mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'org.mini2Dx:gettext-gradle-plugin:1.7.1'
    }
}

...

project(":your-project") {
    apply plugin: "org.mini2Dx.gettext"
...

Next, configure your project to extract the strings. For example, let's say we have a UI package containing all of our UI code and we want to extract strings everywhere that the GetText class is invoked. The following configuration will generate a ui.pot in the build/gettext folder.

project(":your-project") {
    apply plugin: "org.mini2Dx.gettext"

    ...

    gettext {
        ui {
            srcDir = 'src'
            include = '**/ui/**/*.java'
            outputFilename = 'ui.pot'
        }
    }
}

If you want to change the output directory, use the outputPath configuration which is a path relative to the project directory.

project(":your-project") {
    apply plugin: "org.mini2Dx.gettext"

    ...

    gettext {
        ui {
            srcDir = 'src'
            include = '**/ui/**/*.java'
            outputPath = '../other/folder'
            outputFilename = 'ui.pot'
        }
    }
}

Supported File Types

The plugin supports extracting strings from several file types with various limitations.

.java

The plugin can extract from .java files wherever the GetText class is used.

The plugin supports basic variable resolution for local, final and static strings. It will not detect strings that use variable concatenation, however concatenation of string literals is supported. If you're unsure, please refer to the test resources for samples on what usages can be detected.

Translator notes can be extracted if the line before the GetText invocation contains the following format:

//#. This is a translator comment
System.out.println(GetText.tr("Translate this string"));

.lua

The plugin can extract from .lua files by search for the usage of a tr, trn, trc or trnc function. The plugin supports basic variable resolution for local variables and string concatenation. If you're unsure, please refer to the test resources for samples on what usages can be detected.

Translator notes can be extracted if the line before the GetText invocation contains the following format:

--#. This is a translator comment
say(GetText:tr("Translate this string"));

.txt

The plugin can extract from .txt files. It assumes each line is a single translatable string. Translator notes can be extracted if the line contains the following format:

#. This is a translator comment
Translate this string
Clone this wiki locally