Created
December 28, 2023 12:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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