Skip to content

Commit e344dbc

Browse files
authored
Add property to fetch only languages that are translated over a certain percentage (#37)
* Fetch only languages that are over 85% translated * Add minimumTranslationPercentage property * Improve logging related to translated percentage * Change default of minimumTranslationPercentage to -1 * Move away joinAndFormat() and isPercentageTooLow() * Change how languages are skipped
1 parent ef06fe1 commit e344dbc

File tree

7 files changed

+67
-21
lines changed

7 files changed

+67
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
## [Unreleased]
2424
### Added
2525
- Add some code style rules to be shared via editorconfig (insert_final_newline=false)
26+
- Add `minimumTranslationPercentage` parameter to `poEditorConfig` block to specify the minimum accepted percentage of translated strings per language
2627
### Changed
2728
- The xml attribute "encoding" in the generated string resource files is now lowercase "utf-8"
2829
- Migrate to PoEditor API v2

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@ poEditor {
7676

7777
The complete attribute list is the following:
7878

79-
Attribute | Description
80-
--------------------------------|-----------------------------------------
81-
```apiToken``` | PoEditor API Token.
82-
```projectId``` | PoEditor project ID.
83-
```defaultLang``` | (Optional) The lang to be used to build default ```strings.xml``` (```/values``` folder). Defaults to English (`en`).
84-
```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.
85-
```enabled``` | (Since 1.4.0) (Optional) Enables the generation of the block's related task. Defaults to `true`.
86-
```tags``` | (Since 2.1.0) (Optional) List of PoEditor tags to download. Defaults to empty list.
87-
```languageValuesOverrideMap``` | (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.
79+
Attribute | Description
80+
-----------------------------------|-----------------------------------------
81+
```apiToken``` | PoEditor API Token.
82+
```projectId``` | PoEditor project ID.
83+
```defaultLang``` | (Optional) The lang to be used to build default ```strings.xml``` (```/values``` folder). Defaults to English (`en`).
84+
```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.
85+
```enabled``` | (Since 1.4.0) (Optional) Enables the generation of the block's related task. Defaults to `true`.
86+
```tags``` | (Since 2.1.0) (Optional) List of PoEditor tags to download. Defaults to empty list.
87+
```languageValuesOverrideMap``` | (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.
88+
```minimumTranslationPercentage``` | (TBD) (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.
8889

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ fun main() {
4646
key to value
4747
}
4848
?: emptyMap()
49+
val minimumTranslationPercentage = dotenv.get("MINIMUM_TRANSLATION_PERCENTAGE", "85").toInt()
4950

5051
PoEditorStringsImporter.importPoEditorStrings(
5152
apiToken,
5253
projectId,
5354
defaultLanguage,
5455
resDirPath,
5556
tags,
56-
languageValuesOverridePathMap
57+
languageValuesOverridePathMap,
58+
minimumTranslationPercentage
5759
)
5860
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class PoEditorPlugin : Plugin<Project> {
5454
defaultResPath.convention(mainResourceDirectory.asFile.absolutePath)
5555
tags.convention(emptyList())
5656
languageValuesOverridePathMap.convention(emptyMap())
57+
minimumTranslationPercentage.convention(-1)
5758
}
5859

5960
// 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: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
4242
*/
4343
@get:Optional
4444
@get:Input
45-
val enabled : Property<Boolean> = objects.property(Boolean::class.java)
45+
val enabled: Property<Boolean> = objects.property(Boolean::class.java)
4646

4747
/**
4848
* PoEditor API token.
@@ -97,13 +97,22 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
9797
val languageValuesOverridePathMap: MapProperty<String, String> =
9898
objects.mapProperty(String::class.java, String::class.java)
9999

100+
/**
101+
* The minimum accepted percentage of translated strings per language. Languages with fewer translated strings will not be fetched.
102+
*
103+
* Defaults to no minimum if not present, meaning that all languages will be fetched.
104+
*/
105+
@get:Optional
106+
@get:Input
107+
val minimumTranslationPercentage: Property<Int> = objects.property(Int::class.java)
108+
100109
/**
101110
* Sets the configuration as enabled or not.
102111
*
103112
* NOTE: added for Gradle Groovy DSL compatibility. Check the note on
104113
* https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties for more details.
105114
*
106-
* Gradle Kotlin DSL users must use `apiToken.set(value)`.
115+
* Gradle Kotlin DSL users must use `enabled.set(value)`.
107116
*/
108117
fun setEnabled(value: Boolean) = enabled.set(value)
109118

@@ -163,7 +172,17 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
163172
* NOTE: added for Gradle Groovy DSL compatibility. Check the note on
164173
* https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties for more details.
165174
*
166-
* Gradle Kotlin DSL users must use `languageOverridePathMap.set(value)`.
175+
* Gradle Kotlin DSL users must use `languageValuesOverridePathMap.set(value)`.
167176
*/
168177
fun setLanguageValuesOverridePathMap(value: Map<String, String>) = languageValuesOverridePathMap.set(value)
178+
179+
/**
180+
* Sets the minimum accepted percentage of translated strings per language.
181+
*
182+
* NOTE: added for Gradle Groovy DSL compatibility. Check the note on
183+
* https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties for more details.
184+
*
185+
* Gradle Kotlin DSL users must use `minimumTranslationPercentage.set(value)`.
186+
*/
187+
fun setMinimumTranslationPercentage(value: Int) = minimumTranslationPercentage.set(value)
169188
}

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
package com.hyperdevs.poeditor.gradle
2020

21-
import com.hyperdevs.poeditor.gradle.network.api.PoEditorApi
22-
import com.hyperdevs.poeditor.gradle.network.PoEditorApiControllerImpl
23-
import com.hyperdevs.poeditor.gradle.utils.TABLET_REGEX_STRING
2421
import com.hyperdevs.poeditor.gradle.ktx.downloadUrlToString
22+
import com.hyperdevs.poeditor.gradle.network.PoEditorApiControllerImpl
2523
import com.hyperdevs.poeditor.gradle.network.api.ExportType
24+
import com.hyperdevs.poeditor.gradle.network.api.PoEditorApi
25+
import com.hyperdevs.poeditor.gradle.network.api.ProjectLanguage
26+
import com.hyperdevs.poeditor.gradle.utils.TABLET_REGEX_STRING
2627
import com.hyperdevs.poeditor.gradle.utils.logger
2728
import com.hyperdevs.poeditor.gradle.xml.AndroidXmlWriter
2829
import com.hyperdevs.poeditor.gradle.xml.XmlPostProcessor
@@ -51,6 +52,7 @@ object PoEditorStringsImporter {
5152
private const val CONNECT_TIMEOUT_SECONDS = 30L
5253
private const val READ_TIMEOUT_SECONDS = 30L
5354
private const val WRITE_TIMEOUT_SECONDS = 30L
55+
5456
private val okHttpClient = OkHttpClient.Builder()
5557
.connectTimeout(CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS)
5658
.readTimeout(READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
@@ -80,17 +82,31 @@ object PoEditorStringsImporter {
8082
defaultLang: String,
8183
resDirPath: String,
8284
tags: List<String>,
83-
languageValuesOverridePathMap: Map<String, String>) {
85+
languageValuesOverridePathMap: Map<String, String>,
86+
minimumTranslationPercentage: Int) {
8487
try {
8588
val poEditorApiController = PoEditorApiControllerImpl(apiToken, poEditorApi)
8689

8790
// Retrieve available languages from PoEditor
8891
logger.lifecycle("Retrieving project languages...")
8992
val projectLanguages = poEditorApiController.getProjectLanguages(projectId)
93+
val skippedLanguages = projectLanguages.filter { it.percentage < minimumTranslationPercentage }
9094

9195
// Iterate over every available language
92-
logger.lifecycle("Available languages: [${projectLanguages.joinToString(", ") { it.code }}]")
93-
projectLanguages.forEach { languageData ->
96+
logger.lifecycle("Available languages: ${projectLanguages.joinAndFormat { it.code }}")
97+
98+
if (minimumTranslationPercentage > -1) {
99+
val skippedLanguagesMessage = if (skippedLanguages.isEmpty()) {
100+
"All languages are translated above the minimum of $minimumTranslationPercentage%"
101+
} else {
102+
val formattedLanguages = skippedLanguages.joinAndFormat { "${it.code} (${it.percentage}%)" }
103+
"Skipping languages translated under $minimumTranslationPercentage%: $formattedLanguages"
104+
}
105+
106+
logger.lifecycle(skippedLanguagesMessage)
107+
}
108+
109+
projectLanguages.minus(skippedLanguages).forEach { languageData ->
94110
val languageCode = languageData.code
95111

96112
// Retrieve translation file URL for the given language and for the "android_strings" type,
@@ -125,4 +141,7 @@ object PoEditorStringsImporter {
125141
throw e
126142
}
127143
}
144+
145+
private fun List<ProjectLanguage>.joinAndFormat(transform: ((ProjectLanguage) -> CharSequence)) =
146+
joinToString(separator = ", ", prefix = "[", postfix = "]", transform = transform)
128147
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ abstract class ImportPoEditorStringsTask
4545
val projectId: Int
4646
val defaultLang: String
4747
val defaultResPath: String
48-
val tags : List<String>
48+
val tags: List<String>
4949
val languageOverridePathMap: Map<String, String>
50+
val minimumTranslationPercentage: Int
5051

5152
try {
5253
apiToken = extension.apiToken.get()
@@ -55,6 +56,7 @@ abstract class ImportPoEditorStringsTask
5556
defaultResPath = extension.defaultResPath.get()
5657
tags = extension.tags.get()
5758
languageOverridePathMap = extension.languageValuesOverridePathMap.get()
59+
minimumTranslationPercentage = extension.minimumTranslationPercentage.get()
5860
} catch (e: Exception) {
5961
logger.error("Import configuration failed", e)
6062

@@ -70,6 +72,7 @@ abstract class ImportPoEditorStringsTask
7072
defaultLang,
7173
defaultResPath,
7274
tags,
73-
languageOverridePathMap)
75+
languageOverridePathMap,
76+
minimumTranslationPercentage)
7477
}
7578
}

0 commit comments

Comments
 (0)