Skip to content

Commit

Permalink
Support downloading the base language from OneSky (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
frett authored Nov 24, 2022
1 parent ced3fb5 commit 34fa991
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ configure<OneSkyPluginExtension> {
// has src/main/res/ as a default value,
// can be overriden with your custom path (optional)
sourcePath = "path-to-your-string-values-directory"

// Determines if the plugin should download & replace the base language or not.
// defaults to false
downloadBaseLanguage = false
}
```

Expand Down
4 changes: 3 additions & 1 deletion plugin/src/main/java/co/brainly/onesky/OneSkyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ import org.jetbrains.annotations.TestOnly
* @property apiSecret API secret found on OneSky account's settings page
* @property projectId OneSky's project id for syncing files with
* @property sourceStringFiles list of files to be synced with OneSky
* @property downloadBaseLanguage Determines if the plugin should download & replace the base language or not
*/
open class OneSkyPluginExtension(
var verbose: Boolean = false,
var apiKey: String = "",
var apiSecret: String = "",
var projectId: Int = -1,
var sourceStringFiles: List<String> = emptyList(),
var sourcePath: String = "src/main/res"
var sourcePath: String = "src/main/res",
var downloadBaseLanguage: Boolean = false
) {
internal var oneSkyApiUrl: String = ONESKY_API_URL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ open class DownloadTranslationsTask @Inject constructor(
private val projectId = extension.projectId
private val files = extension.sourceStringFiles
private val sourcePath = extension.sourcePath
private val downloadBaseLanguage = extension.downloadBaseLanguage

private val logger = LoggerFactory.getLogger("downloadTranslations")
private val progressLogger by lazy {
Expand Down Expand Up @@ -50,7 +51,7 @@ open class DownloadTranslationsTask @Inject constructor(

private fun downloadLanguages(languageListResponse: LanguageListResponse) {
val languages = languageListResponse.data
.filter { !it.is_base_language }
.filter { downloadBaseLanguage || !it.is_base_language }

val totalFiles = languages.size * files.size
languages.forEachIndexed { languageIndex, language ->
Expand All @@ -73,7 +74,7 @@ open class DownloadTranslationsTask @Inject constructor(
return
}

val languageDir = project.androidResDir.resolve("values-${language.androidDirName}")
val languageDir = project.androidResDir.resolve(language.androidDirName)
languageDir.mkdirs()

val translationFile = File(languageDir, filename)
Expand All @@ -99,17 +100,16 @@ open class DownloadTranslationsTask @Inject constructor(

private val Language.androidDirName: String
get() {
if (code.contains("-")) {
val (locale, region) = code.split("-")
return "$locale-r$region"
return when {
downloadBaseLanguage && is_base_language -> "values"
code.contains("-") -> {
val (locale, region) = code.split("-")
"values-$locale-r$region"
}
// https://developer.android.com/reference/java/util/Locale.html#toLanguageTag()
code == "id" -> "values-in"
else -> "values-$code"
}

// https://developer.android.com/reference/java/util/Locale.html#toLanguageTag()
if (code.contains("id")) {
return "in"
}

return code
}

private val Project.androidResDir: File
Expand Down

0 comments on commit 34fa991

Please sign in to comment.