Placeholder and Conditional function optimization - see #6695

This commit is contained in:
Yair Morgenstern
2022-05-04 22:56:15 +03:00
parent 7986134929
commit 6812caf3b3
2 changed files with 26 additions and 14 deletions

View File

@ -515,7 +515,6 @@ object TranslationFileWriter {
private fun updateFastlaneChangelog() {
// Relative path since we're in android/assets
val changelogFile = File("../../changelog.md").readText()
// first group catches the version name, second group catches the rest of the version changelog
// changelogs by definition do not have #'s in them so we can use it as a delimiter
val latestVersionRegexGroup = Regex("## \\S*([^#]*)").find(changelogFile)
val versionChangelog = latestVersionRegexGroup!!.groups[1]!!.value.trim()

View File

@ -419,6 +419,7 @@ fun String.tr(): String {
* allowing us to have nested translations!
*/
fun String.getPlaceholderParametersIgnoringLowerLevelBraces(): List<String> {
if (!this.contains('[')) return emptyList()
val parameters = ArrayList<String>()
var depthOfBraces = 0
var startOfCurrentParameter = -1
@ -448,9 +449,15 @@ fun String.equalsPlaceholderText(str:String): Boolean {
return this.getPlaceholderText() == str
}
fun String.hasPlaceholderParameters() = squareBraceRegex.containsMatchIn(this.removeConditionals())
fun String.hasPlaceholderParameters(): Boolean {
if (!this.contains('[')) return false
return squareBraceRegex.containsMatchIn(this.removeConditionals())
}
fun String.getPlaceholderParameters() = squareBraceRegex.findAll(this.removeConditionals()).map { it.groups[1]!!.value }.toList()
fun String.getPlaceholderParameters(): List<String> {
if (!this.contains('[')) return emptyList()
return squareBraceRegex.findAll(this.removeConditionals()).map { it.groups[1]!!.value }.toList()
}
/** Substitutes placeholders with [strings], respecting order of appearance. */
fun String.fillPlaceholders(vararg strings: String): String {
@ -464,16 +471,22 @@ fun String.fillPlaceholders(vararg strings: String): String {
return filledString
}
fun String.getConditionals() = pointyBraceRegex.findAll(this).map { Unique(it.groups[1]!!.value) }.toList()
fun String.getConditionals(): List<Unique> {
if (!this.contains('<')) return emptyList()
return pointyBraceRegex.findAll(this).map { Unique(it.groups[1]!!.value) }.toList()
}
fun String.removeConditionals() = this
.replace(pointyBraceRegex, "")
// So, this is a quick hack, but it works as long as nobody uses word separators different from " " (space) and "" (none),
// And no translations start or end with a space.
// According to https://linguistics.stackexchange.com/questions/6131/is-there-a-long-list-of-languages-whose-writing-systems-dont-use-spaces
// This is a reasonable but not fully correct assumption to make.
// By doing it like this, we exclude languages such as Tibetan, Dzongkha (Bhutan), and Ethiopian.
// If we ever start getting translations for these, we'll work something out then.
.replace(" ", " ")
.trim()
fun String.removeConditionals(): String {
if (!this.contains('<')) return this // no need to regex search
return this
.replace(pointyBraceRegex, "")
// So, this is a quick hack, but it works as long as nobody uses word separators different from " " (space) and "" (none),
// And no translations start or end with a space.
// According to https://linguistics.stackexchange.com/questions/6131/is-there-a-long-list-of-languages-whose-writing-systems-dont-use-spaces
// This is a reasonable but not fully correct assumption to make.
// By doing it like this, we exclude languages such as Tibetan, Dzongkha (Bhutan), and Ethiopian.
// If we ever start getting translations for these, we'll work something out then.
.replace(" ", " ")
.trim()
}