/i18NextToAndroidConverter

Converts i18next locales to Android strings on each build automatically

Primary LanguageGoMIT LicenseMIT

i18NextToAndroidConverter

This script allows you to both use i18next internationalization framework in your Android project and keep native Android strings. It automatically downloads i18next file and converts it into strings.xml on each build. i18next file can be provided as local file / remote file / Gitlab private repository file.

E.g. such i18next file

{
    "auth": {
        "login": {
            "email": "Enter your Email:",
            "password": "Enter your password:"
        }
    },
    "welcome": {
        "title": "Hello, {{username}}!"
    }
}

will be converted to

<resources>
    <string name="auth_login_email">Enter your Email:</string>
    <string name="auth_login_password">Enter your password:</string>
    <string name="welcome_title">Hello, {username}!</string>
</resources>

Table of contents:

  1. Getting started
  2. Converting multiple files
  3. Running on release build only
  4. Using with Gitlab

Getting started

  1. Add the following lines to the top of your app level build.gradle file:
import com.github.blindpirate.gogradle.Go

plugins {
    id 'com.github.blindpirate.gogradle' version '0.11.4'
}

golang {
    packagePath = '/'
}

Here we add gogradle plugin to the project.

  1. Add the following lines to the same build.gradle file:
task runI18N(type: Go) {
    go('run . -local "<local_file_address>" -write ""') {
        stderr { stderrLine ->
            println stderrLine
        }
    }
}

preBuild.dependsOn(runI18N)

Here we create runI18N Gradle task. Note that in the line go('run .') you should add flags according to your needs. All available flags are listed below.

  • -console Print output to console.
  • -gitlab "<access_token>" Gitlab token to get remote i18next file via Gitlab Rest API.
  • -local "<local_file_address>" Local i18next file absolute address.
  • -remote "<url>" Remote i18next file url.
  • -write "<qualifier>" Android resource qualifier that specifies values-XXX folder. Can be empty "". If not set, output won't be written to a file.
  1. Download main.go and put it into your app folder.

  2. Build your project.

Converting multiple files

If you have multiple locales, e.g. "en" and "ru", you can convert them all simultaneously. Just add -local or -remote flag for each locale and also -write flags in the same order like so:

go run . -local <en_locale_file> -write "" -local <ru_locale_file> -write "ru"

Running on release build only

Add these lines into your runI18N task in build.gradle:

def isRelease = gradle.startParameter.taskRequests.any {
    it.args[0].contains("Release")
}

if (isRelease) {
    // run script
}

Using with Gitlab

Use -gitlab flag to specify personal access token and -remote flag (one or more) to specify i18next file URL. Note that URL must satisfy Gitlab Rest API rules.