Skip to content

Instantly share code, notes, and snippets.

@kovs705
Created December 28, 2023 12:50
Show Gist options
  • Save kovs705/e6570141e05c373012f931fea697149b to your computer and use it in GitHub Desktop.
Save kovs705/e6570141e05c373012f931fea697149b to your computer and use it in GitHub Desktop.
Gradle script to generate localization from Android strings.xml to iOS Localizable.strings
val generateLocalization: TaskProvider<Task> by tasks.registering {
dependsOn("linkPodDebugFrameworkIosArm64")
fun generateIOSLocalizableStrings(androidStrings: String): String {
val regex = Regex("<string name=\"(.*?)\">(.*?)</string>")
val matches = regex.findAll(androidStrings)
val iosStrings = StringBuilder()
for (match in matches) {
val key = match.groupValues[1]
val value = match.groupValues[2]
iosStrings.append(
"\"$key\" = \"$value\";\n"
.replace("%s", "%@")
.replace("%d", "%ld")
.replace("%02d", "%02ld")
.replace("%f", "%.2f")
)
}
return iosStrings.toString()
}
doLast {
println("Starting localizations")
val sourceFle = file("${project.rootDir}/__ANDROID__FOLDER___/src/main/res/values/strings.xml")
val destinationFile = file("${project.rootDir}/__IOS__FOLDER___/ru.lproj/Localizable.strings")
val iosLocalization = generateIOSLocalizableStrings(sourceFle.readText())
if(destinationFile.exists()) {
destinationFile.writeText(iosLocalization)
} else {
println("File not found")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment