aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-11-09 16:47:31 +0100
committerLinnea Gräf <nea@nea.moe>2024-11-09 16:47:31 +0100
commit32ae3f959d35563396579a24d3989c2496b326b9 (patch)
tree012c84eff3cc38832793bafe5a92cc95d0de92bc
parentcb06ffa17537297ea1e8cca1d6fd01d8925d372d (diff)
downloadmcautotranslations-32ae3f959d35563396579a24d3989c2496b326b9.tar.gz
mcautotranslations-32ae3f959d35563396579a24d3989c2496b326b9.tar.bz2
mcautotranslations-32ae3f959d35563396579a24d3989c2496b326b9.zip
fix: Missing translations for standalone classes
-rw-r--r--example/src/main/kotlin/OtherTest.kt6
-rw-r--r--example/src/main/kotlin/Test2.kt5
-rw-r--r--example/src/main/kotlin/test.kt7
-rw-r--r--kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt4
4 files changed, 19 insertions, 3 deletions
diff --git a/example/src/main/kotlin/OtherTest.kt b/example/src/main/kotlin/OtherTest.kt
new file mode 100644
index 0000000..fe31a16
--- /dev/null
+++ b/example/src/main/kotlin/OtherTest.kt
@@ -0,0 +1,6 @@
+package moe.nea.mcautotranslations.example
+
+class OtherTest {
+
+ fun testFunc(x: Int, y: Text) = tr("other-test.test-func", "Hello $y: $x")
+}
diff --git a/example/src/main/kotlin/Test2.kt b/example/src/main/kotlin/Test2.kt
new file mode 100644
index 0000000..40d23db
--- /dev/null
+++ b/example/src/main/kotlin/Test2.kt
@@ -0,0 +1,5 @@
+package moe.nea.mcautotranslations.example
+
+object Test2 {
+ fun x() = tr("test2.object", "Hehheehe")
+}
diff --git a/example/src/main/kotlin/test.kt b/example/src/main/kotlin/test.kt
index cb5ddfa..c5132d4 100644
--- a/example/src/main/kotlin/test.kt
+++ b/example/src/main/kotlin/test.kt
@@ -10,15 +10,18 @@ val resources =
class Text(val key: String, val args: Array<out Any>) {
override fun toString(): String {
- return resources[key]!!.format(*args)
+ return (resources[key] ?: error("Unresolved key $key")).format(*args)
}
}
@Suppress("UNUSED")
fun trResolved(key: String, vararg args: Any) = Text(key, args)
-fun tr(key: String, default: String): Text = error("Did not run compiler plugin")
+@Suppress("UNUSED")
+fun tr(key: String, default: String): Text = error("Did not run compiler plugin for key '$key' with default '$default'")
fun main() {
println(tr("test1", "Hiiiiiii"))
println(tr("test2", "Hello ${Math.random()}"))
println(tr("test3", "Goodbye ${Math.random()} ${Math.E}"))
+ println(OtherTest().testFunc(10, tr("lol", "Lolnea")))
+ println(Test2.x())
}
diff --git a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt
index fc1acc1..d87648d 100644
--- a/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt
+++ b/kotlin-plugin/src/main/kotlin/moe/nea/mcautotranslations/kotlin/MCAutoTranslationsCallTransformerAndCollector.kt
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irCallConstructor
import org.jetbrains.kotlin.ir.builders.irVararg
+import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrConst
@@ -128,7 +129,8 @@ class MCAutoTranslationsCallTransformerAndCollector(
putValueArgument(1, constString(it.value))
}
}
- file.annotations = annotations + file.annotations
+ val annotationContainer = file.declarations.singleOrNull()?.takeIf { it is IrClass } ?: file
+ annotationContainer.annotations = annotations + annotationContainer.annotations
}