All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.chimerapps.gradle.transifex.DownloadTranslationsExtension.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 - Chimerapps BVBA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.chimerapps.gradle.transifex

import com.chimerapps.gradle.transifex.utils.exists
import groovy.lang.Closure
import org.gradle.api.Action

/**
 * @author Nicola Verbeeck
 * @date 04/09/2017.
 */
open class TranslationConfiguration(open var name: String = "default") {

    open var apiKey: String? = null
    open var projectSlug: String? = null
    open var fileName: String = "strings.xml"
    open var sourceRoot: String = "src/main/res"
    open var fileType: String = "xml"
    open var i18n_type: String = "ANDROID"

    open var sourceRootProvider: Closure = object : Closure(null) {
        override fun call(vararg args: Any?): String {
            val code = (args[0] as String)
            return sourceRoot.replace("{language}", languageRename.call(code) ?: code)
        }
    }
    open var languageRename: Closure = object : Closure(null) {
        override fun call(vararg args: Any?): String {
            return (args[0] as String)
        }
    }
    open var fileNameProvider: Closure = object : Closure(null) {
        override fun call(vararg args: Any?): String {
            return fileName
        }
    }
    open var folderProvider: Closure = object : Closure(null) {
        override fun call(vararg args: Any?): String {
            val code = (args[0] as String)
            return "values-${languageRename.call(code) ?: code}"
        }
    }

    override fun toString(): String {
        return "TranslationConfiguration(name='$name', apiKey=$apiKey, projectSlug=$projectSlug, fileName='$fileName', sourceRoot='$sourceRoot', fileType='$fileType', i18n_type='$i18n_type', languageRename=$languageRename, fileNameProvider=$fileNameProvider, folderProvider=$folderProvider)"
    }

}

open class DownloadTranslationsExtension : TranslationConfiguration() {

    val configurations = arrayListOf()

    open fun configuration(name: String, configuration: Action) {
        if (configurations.exists { it.name == name })
            throw IllegalArgumentException("A configuration with the name \"$name\" already exists")
        if (name == "default")
            throw IllegalArgumentException("Creating a configuration with the name \"default\" is not allowed. The outer block is default")

        TranslationConfiguration(name).apply {
            configuration.execute(this)
            configurations.add(this)
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy