Skip to content

Commit

Permalink
Check for leading, trailing and duplicate whitespace in translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Dec 18, 2024
1 parent c18d9cb commit af0d8f0
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ abstract class CheckLanguageFilesTask : DefaultTask() {
companion object {
private val formatSpecifierRegex = "%((?<argIndex>\\d+)\\$)?[-#+ 0,(<]*\\d*(\\.\\d+)?[tT]?[a-zA-Z%]".toRegex()
private val allowedFormatSpecifierRegex = "%(%|(\\d+\\$)?s)".toRegex()

private val nonSpaceWhitespaceRegex = "[^\\S ]".toRegex()
}

@get:InputDirectory
Expand Down Expand Up @@ -275,6 +277,10 @@ abstract class CheckLanguageFilesTask : DefaultTask() {
errored = true
}

if (!checkStrayWhitespace(filename, lineNumber, key, value)) {
errored = true
}

if (!checkNotEndWithPeriod(filename, lineNumber, key, value)) {
errored = true
}
Expand Down Expand Up @@ -304,6 +310,32 @@ abstract class CheckLanguageFilesTask : DefaultTask() {
return true
}

private fun checkStrayWhitespace(filename: String, lineNumber: Int, key: String, value: String): Boolean {
var errored = false

if (nonSpaceWhitespaceRegex.find(value) != null) {
logger.error("$filename:$lineNumber: translation '$key' contains a whitespace character which isn't a space")
errored = true
}

if (value.startsWith(' ')) {
logger.error("$filename:$lineNumber: translation '$key' starts with a space")
errored = true
}

if (value.endsWith(' ')) {
logger.error("$filename:$lineNumber: translation '$key' ends with a space")
errored = true
}

if (value.contains(" ")) {
logger.error("$filename:$lineNumber: translation '$key' contains duplicate space")
errored = true
}

return !errored
}

private fun checkNotEndWithPeriod(filename: String, lineNumber: Int, key: String, value: String): Boolean {
// only check English, it's a mess otherwise with other languages' weird rules
if (filename == "en_us.json" && value.endsWith('.') && !value.endsWith("...")) {
Expand Down

0 comments on commit af0d8f0

Please sign in to comment.