Skip to content

Commit

Permalink
πŸ”¨ Automatic update of translation statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzovngl committed Nov 11, 2023
1 parent 1746aac commit 8d31511
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 12 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ This simple app helps you avoid forgetting to consume foods that are about to ex
<div align="center">

![English default](https://img.shields.io/badge/English-default-blue?style=flat-square)
![Arabic 81%](https://img.shields.io/badge/Arabic-81%25-yellow?style=flat-square)
![French 96%](https://img.shields.io/badge/French-96%25-green?style=flat-square)
![German 93%](https://img.shields.io/badge/German-93%25-yellowgreen?style=flat-square)
![Hindi 70%](https://img.shields.io/badge/Hindi-70%25-orange?style=flat-square)
![Indonesian 100%](https://img.shields.io/badge/Indonesian-100%25-brightgreen?style=flat-square)
![Italian 100%](https://img.shields.io/badge/Italian-100%25-brightgreen?style=flat-square)
![Japanese 84%](https://img.shields.io/badge/Japanese-84%25-yellow?style=flat-square)
![Spanish 100%](https://img.shields.io/badge/Spanish-100%25-brightgreen?style=flat-square)
![Arabic 81%](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fraw.githubusercontent.com%2Florenzovngl%2FFoodExpirationDates%2Fmain%2Fshields%2Ftranslations%2Far.json)
![French 96%](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fraw.githubusercontent.com%2Florenzovngl%2FFoodExpirationDates%2Fmain%2Fshields%2Ftranslations%2Ffr.json)
![German 93%](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fraw.githubusercontent.com%2Florenzovngl%2FFoodExpirationDates%2Fmain%2Fshields%2Ftranslations%2Fde.json)
![Hindi 70%](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fraw.githubusercontent.com%2Florenzovngl%2FFoodExpirationDates%2Fmain%2Fshields%2Ftranslations%2Fhi.json)
![Indonesian 100%](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fraw.githubusercontent.com%2Florenzovngl%2FFoodExpirationDates%2Fmain%2Fshields%2Ftranslations%2Fin.json)
![Italian 100%](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fraw.githubusercontent.com%2Florenzovngl%2FFoodExpirationDates%2Fmain%2Fshields%2Ftranslations%2Fit.json)
![Japanese 84%](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fraw.githubusercontent.com%2Florenzovngl%2FFoodExpirationDates%2Fmain%2Fshields%2Ftranslations%2Fja.json)
![Spanish 100%](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fraw.githubusercontent.com%2Florenzovngl%2FFoodExpirationDates%2Fmain%2Fshields%2Ftranslations%2Fes.json)

</div>

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ plugins {
alias(libs.plugins.com.android.application) apply false
alias(libs.plugins.com.android.library) apply false
alias(libs.plugins.org.jetbrains.kotlin.android) apply false
alias(libs.plugins.org.jetbrains.kotlin.jvm) apply false
alias(libs.plugins.com.google.dagger.hilt.android) apply false
alias(libs.plugins.app.cash.paparazzi) apply false
alias(libs.plugins.com.google.devtools.ksp) apply false
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ui-test-junit4 = "1.5.3"
uiautomator = "2.2.0"
work-runtime-ktx = "2.8.1"
splashscreen = "1.0.1"
org-jetbrains-kotlin-jvm = "1.9.10"

[libraries]
accompanist-systemuicontroller = { module = "com.google.accompanist:accompanist-systemuicontroller", version.ref = "accompanist-systemuicontroller" }
Expand Down Expand Up @@ -75,5 +76,6 @@ com-android-library = {id = "com.android.library", version.ref = "agp"}
com-google-dagger-hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "dagger-hilt"}
com-google-devtools-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp"}
org-jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "org-jetbrains-kotlin-android" }
org-jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "org-jetbrains-kotlin-jvm" }

[bundles]
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ dependencyResolutionManagement {
}
rootProject.name = "FoodExpirationDates"
include ':app'
include ':translation-analyzer'
4 changes: 2 additions & 2 deletions shields/translations/ar.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"label": "Arabic",
"message": "50%",
"color": "orange",
"message": "81%",
"color": "yellow",
"style": "flat-square"
}
7 changes: 7 additions & 0 deletions shields/translations/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"schemaVersion": 1,
"label": "French",
"message": "96%",
"color": "green",
"style": "flat-square"
}
4 changes: 2 additions & 2 deletions shields/translations/hi.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"label": "Hindi",
"message": "100%",
"color": "brightgreen",
"message": "70%",
"color": "orange",
"style": "flat-square"
}
7 changes: 7 additions & 0 deletions shields/translations/in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"schemaVersion": 1,
"label": "Indonesian",
"message": "100%",
"color": "brightgreen",
"style": "flat-square"
}
7 changes: 7 additions & 0 deletions shields/translations/ja.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"schemaVersion": 1,
"label": "Japanese",
"message": "84%",
"color": "yellow",
"style": "flat-square"
}
1 change: 1 addition & 0 deletions translation-analyzer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
134 changes: 134 additions & 0 deletions translation-analyzer/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.org.jetbrains.kotlin.jvm)
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

dependencies {
implementation(libs.kotlin.stdlib)
}

val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "17"
}

val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "17"
}

val locales = mapOf(
"ar" to "Arabic",
"de" to "German",
"en" to "English",
"es" to "Spanish",
"fr" to "French",
"hi" to "Hindi",
"in" to "Indonesian",
"it" to "Italian",
"ja" to "Japanese",
)

var results: List<Pair<String, Int>> = mutableListOf()

data class JsonEntry(
val schemaVersion: Int = 1,
val label: String,
val message: String,
val color: String,
val style: String = "flat-square"
) {
fun toJson() = "{\n" +
" \"schemaVersion\": $schemaVersion,\n" +
" \"label\": \"$label\",\n" +
" \"message\": \"$message\",\n" +
" \"color\": \"$color\",\n" +
" \"style\": \"$style\"\n" +
"}"
}

fun path(vararg elements: String) = elements.joinToString(separator = File.separator)

fun analyzeTranslations(androidProjectPath: String) {
val androidProject = File(androidProjectPath)
val defaultXML = androidProject.walkTopDown()
.filter {
it.absolutePath.endsWith(path("values", "strings.xml")) &&
it.absolutePath.contains(path("app", "src", "main"))
}.first()
androidProject.walkTopDown()
.filter {
it.name.endsWith("strings.xml") && it.absolutePath.contains("main")
}.forEach {
parseXML(defaultXML, it)
}
}

fun parseXML(default: File, translation: File) {
val folderName = translation.absolutePath.substring(
translation.absolutePath.indexOf("values"),
translation.absolutePath.lastIndexOf(File.separator)
)
val localeKey = folderName.let {
it.substring(startIndex = it.indexOf("-") + 1)
}
val locale = locales[localeKey]
val percent = (countStrings(translation).toDouble() / countStrings(default) * 100).toInt()
val color = percent.let {
when {
it < 50 -> "red"
it < 80 -> "orange"
it < 90 -> "yellow"
it < 95 -> "yellowgreen"
it < 100 -> "green"
else -> "brightgreen"
}
}
if (locale != null) {
results = results.plus(locale to percent)
File(path("shields", "translations", "${localeKey}.json"))
.writeText(
JsonEntry(
label = locale,
message = "$percent%",
color = color
).toJson()
)
}
}

fun countStrings(file: File): Int {
val doc = javax.xml.parsers.DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(file)
doc.documentElement.normalize()
return doc.getElementsByTagName("string").length +
doc.getElementsByTagName("item").length
}

tasks.register("analyzeTranslations") {
doLast {
println("Computing translation statistics...")
analyzeTranslations(System.getProperty("user.dir"))
println(
"Translation statistics: ${
results.sortedBy {
it.first
}.joinToString(separator = ", ") {
"${it.first}: ${it.second}%"
}
}"
)
}
}

tasks.named("build") { finalizedBy("analyzeTranslations") }

tasks.named("assemble") { finalizedBy("analyzeTranslations") }

0 comments on commit 8d31511

Please sign in to comment.