Skip to content

Commit a73f04f

Browse files
authored
Add support for custom resource file name (#59)
1 parent f40d6b7 commit a73f04f

File tree

8 files changed

+39
-10
lines changed

8 files changed

+39
-10
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 configuration support to define custom resource file names.
2626
### Changed
2727
- No changed features!
2828
### Deprecated

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Attribute | Description
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.
9191
```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.
9292
```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-
93+
```resFileName``` | (Since 3.1.0) (Optional) Sets the file name for the imported string resource XML files. Defaults to `strings`
9494
After the configuration is done, just run the new ```importPoEditorStrings``` task via Android Studio or command line:
9595

9696
```
@@ -100,7 +100,7 @@ After the configuration is done, just run the new ```importPoEditorStrings``` ta
100100
This task will:
101101
* Download all strings files (every available lang) from PoEditor given the api token and project id.
102102
* Process the incoming strings to fix some PoEditor incompatibilities with Android strings system.
103-
* Create and save strings.xml files to ```/values-<lang>``` (or ```/values``` in case of the default lang). It supports
103+
* Create and save strings.xml (or whatever file name you desire by using the `resFileName` attribute) files to ```/values-<lang>``` (or ```/values``` in case of the default lang). It supports
104104
region specific languages by creating the proper folders (i.e. ```/values-es-rMX```).
105105

106106
## Enhanced syntax

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fun main() {
3232

3333
val apiToken = dotenv.get("API_TOKEN", "")
3434
val projectId = dotenv.get("PROJECT_ID", "-1").toInt()
35+
val resFileName = dotenv.get("RES_FILE_NAME", "strings")
3536
val resDirPath = dotenv.get("RES_DIR_PATH", "")
3637
val defaultLanguage = dotenv.get("DEFAULT_LANGUAGE", "")
3738
val filters = dotenv.get("FILTERS", "")
@@ -63,6 +64,7 @@ fun main() {
6364
filters,
6465
tags,
6566
languageValuesOverridePathMap,
66-
minimumTranslationPercentage
67+
minimumTranslationPercentage,
68+
resFileName
6769
)
6870
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class PoEditorPlugin : Plugin<Project> {
5656
tags.convention(emptyList())
5757
languageValuesOverridePathMap.convention(emptyMap())
5858
minimumTranslationPercentage.convention(-1)
59+
resFileName.convention("strings")
5960
}
6061

6162
// 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
@@ -78,6 +78,15 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
7878
@get:Input
7979
val defaultResPath: Property<String> = objects.property(String::class.java)
8080

81+
/**
82+
* File name of the string resource files.
83+
*
84+
* Defaults to "strings" if not defined.
85+
*/
86+
@get:Optional
87+
@get:Input
88+
val resFileName: Property<String> = objects.property(String::class.java)
89+
8190
/**
8291
* Filters to limit downloaded strings with, from the officially supported list in PoEditor.
8392
*
@@ -165,6 +174,16 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
165174
*/
166175
fun setDefaultResPath(value: String) = defaultResPath.set(value)
167176

177+
/**
178+
* Sets the file name for the resource files.
179+
*
180+
* NOTE: added for Gradle Groovy DSL compatibility. Check the note on
181+
* https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties for more details.
182+
*
183+
* Gradle Kotlin DSL users must use `resFileName.set(value)`.
184+
*/
185+
fun setResFileName(value: String) = resFileName.set(value)
186+
168187
/**
169188
* Sets the filters to limit downloaded strings with, from the officially supported list in PoEditor.
170189
*

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ object PoEditorStringsImporter {
8989
filters: List<FilterType>,
9090
tags: List<String>,
9191
languageValuesOverridePathMap: Map<String, String>,
92-
minimumTranslationPercentage: Int) {
92+
minimumTranslationPercentage: Int,
93+
resFileName: String) {
9394
try {
9495
val poEditorApiController = PoEditorApiControllerImpl(apiToken, poEditorApi)
9596

@@ -140,10 +141,11 @@ object PoEditorStringsImporter {
140141

141142
xmlWriter.saveXml(
142143
resDirPath,
144+
resFileName,
143145
postProcessedXmlDocumentMap,
144146
defaultLang,
145147
languageCode,
146-
languageValuesOverridePathMap
148+
languageValuesOverridePathMap,
147149
)
148150
}
149151
} catch (e: Exception) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ abstract class ImportPoEditorStringsTask
5050
val tags: List<String>
5151
val languageOverridePathMap: Map<String, String>
5252
val minimumTranslationPercentage: Int
53+
val resFileName: String
5354

5455
try {
5556
apiToken = extension.apiToken.get()
@@ -60,6 +61,7 @@ abstract class ImportPoEditorStringsTask
6061
tags = extension.tags.get()
6162
languageOverridePathMap = extension.languageValuesOverridePathMap.get()
6263
minimumTranslationPercentage = extension.minimumTranslationPercentage.get()
64+
resFileName = extension.resFileName.get()
6365
} catch (e: Exception) {
6466
logger.error("Import configuration failed", e)
6567

@@ -77,6 +79,7 @@ abstract class ImportPoEditorStringsTask
7779
filters,
7880
tags,
7981
languageOverridePathMap,
80-
minimumTranslationPercentage)
82+
minimumTranslationPercentage,
83+
resFileName)
8184
}
8285
}

src/main/kotlin/com/hyperdevs/poeditor/gradle/xml/AndroidXmlWriter.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class AndroidXmlWriter {
3535
* Saves a given map of XML files related to a language that the project contains to the
3636
* project's strings folder.
3737
*/
38+
@Suppress("LongParameterList")
3839
fun saveXml(resDirPath: String,
40+
resFileName: String,
3941
postProcessedXmlDocumentMap: Map<String, Document>,
4042
defaultLang: String,
4143
languageCode: String,
@@ -63,11 +65,11 @@ class AndroidXmlWriter {
6365
}
6466

6567
folderToXmlMap.forEach { (valuesFolderFile, document) ->
66-
saveXmlToFolder(valuesFolderFile, document)
68+
saveXmlToFolder(valuesFolderFile, document, resFileName)
6769
}
6870
}
6971

70-
private fun saveXmlToFolder(stringsFolderFile: File, document: Document) {
72+
private fun saveXmlToFolder(stringsFolderFile: File, document: Document, resFileName: String) {
7173
if (!stringsFolderFile.exists()) {
7274
logger.debug("Creating strings folder for new language")
7375
val folderCreated = stringsFolderFile.mkdirs()
@@ -76,6 +78,6 @@ class AndroidXmlWriter {
7678
}
7779

7880
logger.lifecycle("Saving strings to ${stringsFolderFile.absolutePath}")
79-
File(stringsFolderFile, "strings.xml").writeText(document.toAndroidXmlString())
81+
File(stringsFolderFile, "$resFileName.xml").writeText(document.toAndroidXmlString())
8082
}
8183
}

0 commit comments

Comments
 (0)