From af0d8f034d66f81e64482b9fc5b01d9786eb343c Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 18 Dec 2024 02:14:12 +0000 Subject: [PATCH] Check for leading, trailing and duplicate whitespace in translations --- .../buildscript/CheckLanguageFilesTask.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/buildSrc/src/main/kotlin/net/earthcomputer/clientcommands/buildscript/CheckLanguageFilesTask.kt b/buildSrc/src/main/kotlin/net/earthcomputer/clientcommands/buildscript/CheckLanguageFilesTask.kt index 34a07d1a..bd88a31f 100644 --- a/buildSrc/src/main/kotlin/net/earthcomputer/clientcommands/buildscript/CheckLanguageFilesTask.kt +++ b/buildSrc/src/main/kotlin/net/earthcomputer/clientcommands/buildscript/CheckLanguageFilesTask.kt @@ -15,6 +15,8 @@ abstract class CheckLanguageFilesTask : DefaultTask() { companion object { private val formatSpecifierRegex = "%((?\\d+)\\$)?[-#+ 0,(<]*\\d*(\\.\\d+)?[tT]?[a-zA-Z%]".toRegex() private val allowedFormatSpecifierRegex = "%(%|(\\d+\\$)?s)".toRegex() + + private val nonSpaceWhitespaceRegex = "[^\\S ]".toRegex() } @get:InputDirectory @@ -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 } @@ -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("...")) {