Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- No new features!
- Add option to unquote strings via export API
### Changed
- No changed features!
### Deprecated
Expand Down
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,20 @@ poEditor {

The complete attribute list is the following:

Attribute | Description
-----------------------------------|-----------------------------------------
```apiToken``` | PoEditor API Token.
```projectId``` | PoEditor project ID.
```defaultLang``` | (Optional) The lang to be used to build default ```strings.xml``` (```/values``` folder). Defaults to English (`en`).
```defaultResPath``` | (Since 1.3.0) (Optional) Path where the plug-in should dump strings. Defaults to the module's default (or build variant) `res` path.
```enabled``` | (Since 1.4.0) (Optional) Enables the generation of the block's related task. Defaults to `true`.
```tags``` | (Since 2.1.0) (Optional) List of PoEditor tags to download. Defaults to empty list.
Attribute | Description
---------------------------------------|-----------------------------------------
```apiToken``` | PoEditor API Token.
```projectId``` | PoEditor project ID.
```defaultLang``` | (Optional) The lang to be used to build default ```strings.xml``` (```/values``` folder). Defaults to English (`en`).
```defaultResPath``` | (Since 1.3.0) (Optional) Path where the plug-in should dump strings. Defaults to the module's default (or build variant) `res` path.
```enabled``` | (Since 1.4.0) (Optional) Enables the generation of the block's related task. Defaults to `true`.
```tags``` | (Since 2.1.0) (Optional) List of PoEditor tags to download. Defaults to empty list.
```languageValuesOverridePathMap``` | (Since 2.2.0) (Optional) Map of `language_code:path` entries that you want to override the default language values folder with. Defaults to empty map.
```minimumTranslationPercentage``` | (Since 2.3.0) (Optional) The minimum accepted percentage of translated strings per language. Languages with fewer translated strings will not be fetched. Defaults to no minimum, allowing all languages to be fetched.
```filters``` | (Since 2.4.0) (Optional) List of PoEditor filters to use during download. Defaults to empty list. Accepted values are defined by the POEditor API.
```resFileName``` | (Since 3.1.0) (Optional) Sets the file name for the imported string resource XML files. Defaults to `strings`.
```order``` | (Since 3.1.0) (Optional) Defines how to order the export. Accepted values are defined by the POEditor API.
```minimumTranslationPercentage``` | (Since 2.3.0) (Optional) The minimum accepted percentage of translated strings per language. Languages with fewer translated strings will not be fetched. Defaults to no minimum, allowing all languages to be fetched.
```filters``` | (Since 2.4.0) (Optional) List of PoEditor filters to use during download. Defaults to empty list. Accepted values are defined by the POEditor API.
```resFileName``` | (Since 3.1.0) (Optional) Sets the file name for the imported string resource XML files. Defaults to `strings`.
```order``` | (Since 3.1.0) (Optional) Defines how to order the export. Accepted values are defined by the POEditor API.
```unquoted``` | (Since 3.2.0) (Optional) Defines if the strings should be unquoted, overriding default PoEditor configuration. Defaults to `false`.

After the configuration is done, just run the new ```importPoEditorStrings``` task via Android Studio or command line:

Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/hyperdevs/poeditor/gradle/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fun main() {
}
?: emptyMap()
val minimumTranslationPercentage = dotenv.get("MINIMUM_TRANSLATION_PERCENTAGE", "85").toInt()
val unquoted = dotenv.get("UNQUOTED", "false").toBoolean()

PoEditorStringsImporter.importPoEditorStrings(
apiToken,
Expand All @@ -68,6 +69,7 @@ fun main() {
tags,
languageValuesOverridePathMap,
minimumTranslationPercentage,
resFileName
resFileName,
unquoted
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class PoEditorPlugin : Plugin<Project> {
languageValuesOverridePathMap.convention(emptyMap())
minimumTranslationPercentage.convention(-1)
resFileName.convention("strings")
unquoted.convention(false)
}

// Add flavor and build-type configurations if the project has the "com.android.application" plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
@get:Input
val minimumTranslationPercentage: Property<Int> = objects.property(Int::class.java)

/**
* Defines if the output strings exported from PoEditor should be unquoted.
*
* Defaults to "false" meaning that all texts will be quoted.
*/
@get:Optional
@get:Input
val unquoted: Property<Boolean> = objects.property(Boolean::class.java)

/**
* Sets the configuration as enabled or not.
*
Expand Down Expand Up @@ -242,4 +251,14 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
* Gradle Kotlin DSL users must use `minimumTranslationPercentage.set(value)`.
*/
fun setMinimumTranslationPercentage(value: Int) = minimumTranslationPercentage.set(value)

/**
* Sets if the exported strings should be unquoted or not.
*
* NOTE: added for Gradle Groovy DSL compatibility. Check the note on
* https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties for more details.
*
* Gradle Kotlin DSL users must use `unquoted.set(value)`.
*/
fun setUnquoted(value: Boolean) = unquoted.set(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ object PoEditorStringsImporter {
tags: List<String>,
languageValuesOverridePathMap: Map<String, String>,
minimumTranslationPercentage: Int,
resFileName: String) {
resFileName: String,
unquoted: Boolean) {
try {
val poEditorApiController = PoEditorApiControllerImpl(apiToken, poEditorApi)
val poEditorApiController = PoEditorApiControllerImpl(apiToken, moshi, poEditorApi)

// Retrieve available languages from PoEditor
logger.lifecycle("Retrieving project languages...")
Expand Down Expand Up @@ -131,7 +132,9 @@ object PoEditorStringsImporter {
type = ExportType.ANDROID_STRINGS,
filters = filters,
order = order,
tags = tags)
tags = tags,
unquoted = unquoted
)

// Download translation File to in-memory string
logger.lifecycle("Downloading file from URL: $translationFileUrl")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
package com.hyperdevs.poeditor.gradle.network

import com.hyperdevs.poeditor.gradle.network.api.*
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import retrofit2.Response

/**
Expand All @@ -32,22 +35,28 @@ interface PoEditorApiController {

/**
* Retrieves the translation file URL for a given project, language code, and export type.
* Also supports a list of tags to filter.
* Also supports a series of options.
*/
@Suppress("LongParameterList")
fun getTranslationFileUrl(projectId: Int,
code: String,
type: ExportType,
filters: List<FilterType>?,
order: OrderType,
tags: List<String>?): String
tags: List<String>?,
unquoted: Boolean): String
}

/**
* Implementation of [PoEditorApiController] using Retrofit.
*/
class PoEditorApiControllerImpl(private val apiToken: String,
private val moshi: Moshi,
private val poEditorApi: PoEditorApi) : PoEditorApiController {

private val optionsAdapter: JsonAdapter<List<Options>> =
moshi.adapter(Types.newParameterizedType(List::class.java, Options::class.java))

override fun getProjectLanguages(projectId: Int): List<ProjectLanguage> {
val response = poEditorApi.getProjectLanguages(
apiToken = apiToken,
Expand All @@ -61,16 +70,25 @@ class PoEditorApiControllerImpl(private val apiToken: String,
type: ExportType,
filters: List<FilterType>?,
order: OrderType,
tags: List<String>?): String {
tags: List<String>?,
unquoted: Boolean): String {
val options = listOf(
Options(unquoted = if (unquoted) 1 else 0)
).let {
optionsAdapter.toJson(it)
}

val response = poEditorApi.getExportFileInfo(
apiToken = apiToken,
id = projectId,
type = type.toString().toLowerCase(),
filters = filters?.map { it.name.toLowerCase() },
language = code,
order = order.name.toLowerCase(),
tags = tags)
.execute()
tags = tags,
options = options
).execute()

return response.onSuccessful { it.result.url }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ interface PoEditorApi {
@Field("filters") filters: List<String>? = null,
@Field("order") order: String? = null,
@Field("tags") tags: List<String>? = null,
@Field("options") options: Map<String, String>? = null): Call<ExportResponse>
@Field("options") options: String? = null): Call<ExportResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,12 @@ enum class OrderType {
}
}
}

/**
* Object passed as export options.
*/
data class Options(val unquoted: Int) {
init {
require(unquoted in 0..1) { "unquoted value must be 0 or 1" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class ImportPoEditorStringsTask
val languageOverridePathMap: Map<String, String>
val minimumTranslationPercentage: Int
val resFileName: String
val unquoted: Boolean

try {
apiToken = extension.apiToken.get()
Expand All @@ -65,6 +66,7 @@ abstract class ImportPoEditorStringsTask
languageOverridePathMap = extension.languageValuesOverridePathMap.get()
minimumTranslationPercentage = extension.minimumTranslationPercentage.get()
resFileName = extension.resFileName.get()
unquoted = extension.unquoted.get()
} catch (e: Exception) {
logger.error("Import configuration failed", e)

Expand All @@ -84,6 +86,7 @@ abstract class ImportPoEditorStringsTask
tags,
languageOverridePathMap,
minimumTranslationPercentage,
resFileName)
resFileName,
unquoted)
}
}