Skip to content

Commit b77e03f

Browse files
authored
Add 'unquoted' flag to unquote strings from PoEditor (#63)
1 parent 32d44d7 commit b77e03f

File tree

10 files changed

+80
-24
lines changed

10 files changed

+80
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222

2323
## [Unreleased]
2424
### Added
25-
- No new features!
25+
- Add option to unquote strings via export API
2626
### Changed
2727
- No changed features!
2828
### Deprecated

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,20 @@ poEditor {
7979

8080
The complete attribute list is the following:
8181

82-
Attribute | Description
83-
-----------------------------------|-----------------------------------------
84-
```apiToken``` | PoEditor API Token.
85-
```projectId``` | PoEditor project ID.
86-
```defaultLang``` | (Optional) The lang to be used to build default ```strings.xml``` (```/values``` folder). Defaults to English (`en`).
87-
```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.
88-
```enabled``` | (Since 1.4.0) (Optional) Enables the generation of the block's related task. Defaults to `true`.
89-
```tags``` | (Since 2.1.0) (Optional) List of PoEditor tags to download. Defaults to empty list.
82+
Attribute | Description
83+
---------------------------------------|-----------------------------------------
84+
```apiToken``` | PoEditor API Token.
85+
```projectId``` | PoEditor project ID.
86+
```defaultLang``` | (Optional) The lang to be used to build default ```strings.xml``` (```/values``` folder). Defaults to English (`en`).
87+
```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.
88+
```enabled``` | (Since 1.4.0) (Optional) Enables the generation of the block's related task. Defaults to `true`.
89+
```tags``` | (Since 2.1.0) (Optional) List of PoEditor tags to download. Defaults to empty list.
9090
```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.
91-
```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.
92-
```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.
93-
```resFileName``` | (Since 3.1.0) (Optional) Sets the file name for the imported string resource XML files. Defaults to `strings`.
94-
```order``` | (Since 3.1.0) (Optional) Defines how to order the export. Accepted values are defined by the POEditor API.
91+
```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.
92+
```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.
93+
```resFileName``` | (Since 3.1.0) (Optional) Sets the file name for the imported string resource XML files. Defaults to `strings`.
94+
```order``` | (Since 3.1.0) (Optional) Defines how to order the export. Accepted values are defined by the POEditor API.
95+
```unquoted``` | (Since 3.2.0) (Optional) Defines if the strings should be unquoted, overriding default PoEditor configuration. Defaults to `false`.
9596

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

src/main/kotlin/com/hyperdevs/poeditor/gradle/Main.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ fun main() {
5757
}
5858
?: emptyMap()
5959
val minimumTranslationPercentage = dotenv.get("MINIMUM_TRANSLATION_PERCENTAGE", "85").toInt()
60+
val unquoted = dotenv.get("UNQUOTED", "false").toBoolean()
6061

6162
PoEditorStringsImporter.importPoEditorStrings(
6263
apiToken,
@@ -68,6 +69,7 @@ fun main() {
6869
tags,
6970
languageValuesOverridePathMap,
7071
minimumTranslationPercentage,
71-
resFileName
72+
resFileName,
73+
unquoted
7274
)
7375
}

src/main/kotlin/com/hyperdevs/poeditor/gradle/PoEditorPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class PoEditorPlugin : Plugin<Project> {
5959
languageValuesOverridePathMap.convention(emptyMap())
6060
minimumTranslationPercentage.convention(-1)
6161
resFileName.convention("strings")
62+
unquoted.convention(false)
6263
}
6364

6465
// Add flavor and build-type configurations if the project has the "com.android.application" plugin

src/main/kotlin/com/hyperdevs/poeditor/gradle/PoEditorPluginExtension.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
133133
@get:Input
134134
val minimumTranslationPercentage: Property<Int> = objects.property(Int::class.java)
135135

136+
/**
137+
* Defines if the output strings exported from PoEditor should be unquoted.
138+
*
139+
* Defaults to "false" meaning that all texts will be quoted.
140+
*/
141+
@get:Optional
142+
@get:Input
143+
val unquoted: Property<Boolean> = objects.property(Boolean::class.java)
144+
136145
/**
137146
* Sets the configuration as enabled or not.
138147
*
@@ -242,4 +251,14 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
242251
* Gradle Kotlin DSL users must use `minimumTranslationPercentage.set(value)`.
243252
*/
244253
fun setMinimumTranslationPercentage(value: Int) = minimumTranslationPercentage.set(value)
254+
255+
/**
256+
* Sets if the exported strings should be unquoted or not.
257+
*
258+
* NOTE: added for Gradle Groovy DSL compatibility. Check the note on
259+
* https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties for more details.
260+
*
261+
* Gradle Kotlin DSL users must use `unquoted.set(value)`.
262+
*/
263+
fun setUnquoted(value: Boolean) = unquoted.set(value)
245264
}

src/main/kotlin/com/hyperdevs/poeditor/gradle/PoEditorStringsImporter.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ object PoEditorStringsImporter {
9292
tags: List<String>,
9393
languageValuesOverridePathMap: Map<String, String>,
9494
minimumTranslationPercentage: Int,
95-
resFileName: String) {
95+
resFileName: String,
96+
unquoted: Boolean) {
9697
try {
97-
val poEditorApiController = PoEditorApiControllerImpl(apiToken, poEditorApi)
98+
val poEditorApiController = PoEditorApiControllerImpl(apiToken, moshi, poEditorApi)
9899

99100
// Retrieve available languages from PoEditor
100101
logger.lifecycle("Retrieving project languages...")
@@ -131,7 +132,9 @@ object PoEditorStringsImporter {
131132
type = ExportType.ANDROID_STRINGS,
132133
filters = filters,
133134
order = order,
134-
tags = tags)
135+
tags = tags,
136+
unquoted = unquoted
137+
)
135138

136139
// Download translation File to in-memory string
137140
logger.lifecycle("Downloading file from URL: $translationFileUrl")

src/main/kotlin/com/hyperdevs/poeditor/gradle/network/PoEditorApiController.kt

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
package com.hyperdevs.poeditor.gradle.network
2020

2121
import com.hyperdevs.poeditor.gradle.network.api.*
22+
import com.squareup.moshi.JsonAdapter
23+
import com.squareup.moshi.Moshi
24+
import com.squareup.moshi.Types
2225
import retrofit2.Response
2326

2427
/**
@@ -32,22 +35,28 @@ interface PoEditorApiController {
3235

3336
/**
3437
* Retrieves the translation file URL for a given project, language code, and export type.
35-
* Also supports a list of tags to filter.
38+
* Also supports a series of options.
3639
*/
3740
@Suppress("LongParameterList")
3841
fun getTranslationFileUrl(projectId: Int,
3942
code: String,
4043
type: ExportType,
4144
filters: List<FilterType>?,
4245
order: OrderType,
43-
tags: List<String>?): String
46+
tags: List<String>?,
47+
unquoted: Boolean): String
4448
}
4549

4650
/**
4751
* Implementation of [PoEditorApiController] using Retrofit.
4852
*/
4953
class PoEditorApiControllerImpl(private val apiToken: String,
54+
private val moshi: Moshi,
5055
private val poEditorApi: PoEditorApi) : PoEditorApiController {
56+
57+
private val optionsAdapter: JsonAdapter<List<Options>> =
58+
moshi.adapter(Types.newParameterizedType(List::class.java, Options::class.java))
59+
5160
override fun getProjectLanguages(projectId: Int): List<ProjectLanguage> {
5261
val response = poEditorApi.getProjectLanguages(
5362
apiToken = apiToken,
@@ -61,16 +70,25 @@ class PoEditorApiControllerImpl(private val apiToken: String,
6170
type: ExportType,
6271
filters: List<FilterType>?,
6372
order: OrderType,
64-
tags: List<String>?): String {
73+
tags: List<String>?,
74+
unquoted: Boolean): String {
75+
val options = listOf(
76+
Options(unquoted = if (unquoted) 1 else 0)
77+
).let {
78+
optionsAdapter.toJson(it)
79+
}
80+
6581
val response = poEditorApi.getExportFileInfo(
6682
apiToken = apiToken,
6783
id = projectId,
6884
type = type.toString().toLowerCase(),
6985
filters = filters?.map { it.name.toLowerCase() },
7086
language = code,
7187
order = order.name.toLowerCase(),
72-
tags = tags)
73-
.execute()
88+
tags = tags,
89+
options = options
90+
).execute()
91+
7492
return response.onSuccessful { it.result.url }
7593
}
7694

src/main/kotlin/com/hyperdevs/poeditor/gradle/network/api/PoEditorApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ interface PoEditorApi {
5050
@Field("filters") filters: List<String>? = null,
5151
@Field("order") order: String? = null,
5252
@Field("tags") tags: List<String>? = null,
53-
@Field("options") options: Map<String, String>? = null): Call<ExportResponse>
53+
@Field("options") options: String? = null): Call<ExportResponse>
5454
}

src/main/kotlin/com/hyperdevs/poeditor/gradle/network/api/PoEditorApiModels.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,12 @@ enum class OrderType {
148148
}
149149
}
150150
}
151+
152+
/**
153+
* Object passed as export options.
154+
*/
155+
data class Options(val unquoted: Int) {
156+
init {
157+
require(unquoted in 0..1) { "unquoted value must be 0 or 1" }
158+
}
159+
}

src/main/kotlin/com/hyperdevs/poeditor/gradle/tasks/ImportPoEditorStringsTask.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ abstract class ImportPoEditorStringsTask
5353
val languageOverridePathMap: Map<String, String>
5454
val minimumTranslationPercentage: Int
5555
val resFileName: String
56+
val unquoted: Boolean
5657

5758
try {
5859
apiToken = extension.apiToken.get()
@@ -65,6 +66,7 @@ abstract class ImportPoEditorStringsTask
6566
languageOverridePathMap = extension.languageValuesOverridePathMap.get()
6667
minimumTranslationPercentage = extension.minimumTranslationPercentage.get()
6768
resFileName = extension.resFileName.get()
69+
unquoted = extension.unquoted.get()
6870
} catch (e: Exception) {
6971
logger.error("Import configuration failed", e)
7072

@@ -84,6 +86,7 @@ abstract class ImportPoEditorStringsTask
8486
tags,
8587
languageOverridePathMap,
8688
minimumTranslationPercentage,
87-
resFileName)
89+
resFileName,
90+
unquoted)
8891
}
8992
}

0 commit comments

Comments
 (0)