Skip to content

A Gradle plugin that uploads and downloads strings from lokalise using the lokalise CLI under the hood.

License

Notifications You must be signed in to change notification settings

Fabi755/LokaliseGradlePlugin

 
 

Repository files navigation

LokaliseGradlePlugin

A Gradle plugin that up- and downloads strings from lokalise using the lokalise CLI under the hood.

Usage

Apply the plugin

Add JitPack to the settings.gradle[.kts] file:

pluginManagement {
    repositories {
        maven { 
            url("https://jitpack.io")
            content {
                includeGroup("com.github.ioki-mobility.LokaliseGradlePlugin")
            }
        }
        resolutionStrategy {
            it.eachPlugin {
                if (requested.id.id == "com.ioki.lokalise") {
                    useModule(
                        "com.github.ioki-mobility.LokaliseGradlePlugin:lokalise:${requested.version}"
                    )
                }
            }
        }
    }
}

Note: If you use JitPack, the [CURRENT_VERSION] can either be a (git) tag (recommended), branch name, or hash.

Add the plugin to the build.gradle[.kts] file:

plugins {
    id("com.ioki.lokalise") version "[CURRENT_VERSION]"
}

Configuration

After you applied the plugin, you have access to the lokalise extension. You need to set up the apiToken as well as the projectId there:

lokalise {
    apiToken.set(providers.environmentVariable("LOKALISE_API_TOKEN"))
    projectId.set(providers.environmentVariable("LOKALISE_PROJECT_ID"))
}

Upload configuration

To configure the upload you can use the lokalise.uploadStringsConfig function:

lokalise {
    uploadStringsConfig {
        translationsFilesToUpload.set(filesToUpload)
        arguments(
            "--replace-modified",
            "--cleanup-mode",
            "--include-path",
            "--distinguish-by-file",
            "--lang-iso", "en_BZ",
            "--poll"
        )
    }    
}

The plugin provides a uploadTranslations task that uses the configuration you upload the given translation files. The base arguments for that tasks that are put to the lokalise CLI are:

file upload --token [TOKEN] --project-id [PROJECT_ID] --file [trnslationsFilesToUploadAsString]

The arguments you provide via the extension will be added afterward. In our example you would end up with:

file upload --token [TOKEN] --project-id [PROJECT_ID] --file [trnslationsFilesToUploadAsString] /
    --replace-modified --cleanup-mode --include-path --distinguish-by-file --lang-iso en_BZ --poll

Download configuration

To configure the download you can use the lokalise.downloadStringsConfigs function. Be note that, in contrast to the upload config, you can create multiple download configurations:

lokalise {
    downloadStringsConfigs {
        register("main") {
            arguments = listOf(
                "--format", "xml",
                "--filter-langs", "en,de,de_CH,fr_CH,es,it,nl,ca,ar",
            )
        }
    }
}

The lokalise.downloadStringsConfigs function is a NamedDomainObjectContainer that configured a DownloadStringsConfig. With that you can configure the arguments you want to put to the CLI file download command. Each of the created configurations will create a Gradle tasks named like the following:

downloadTranslationsFor[name]

Mostly you don't need registering more than one. But you can register multiple ones in case you want to download only a subset of strings for a specific use case. For example, if you only want to download spanish strings you can do this:

downloadStringsConfigs {
    register("main") {
        arguments = listOf(
            "--format", "xml",
            "--filter-langs", "en,de,de_CH,fr_CH,es,it,nl,ca,ar",
        )
    }
    register("spanishOnly") {
        arguments = listOf(
            "--format", "xml",
            "--filter-langs", "es",
        )
    }
}

This will generate two tasks downloadTranslationsForMain and downloadTranslationsForSpanishhOnly. If you run the latter, it will only download the translated strings for spanish.

By default, there is also on downloadTranslationsForAll task that will execute all the created tasks in case you have to execute all of them after each other.

Release

  • Checkout main branch
  • Update the version in build.gradle.kts
  • Commit with message Next version
  • Tag the version with the same version and push it to origin
  • Update the version to the "next minor version" (including -SNAPSHOT)
  • Push to origin
  • Create a new release

About

A Gradle plugin that uploads and downloads strings from lokalise using the lokalise CLI under the hood.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%