From 455db4468167b65f796d346c472c9bb32d657ee5 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Wed, 30 Oct 2024 17:14:39 +0100 Subject: Add example and fix switched option names --- example/build.gradle.kts | 9 +++++++-- example/src/main/kotlin/test.kt | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'example') diff --git a/example/build.gradle.kts b/example/build.gradle.kts index b3f0a88..99b62f9 100644 --- a/example/build.gradle.kts +++ b/example/build.gradle.kts @@ -9,14 +9,19 @@ repositories { mavenCentral() } +dependencies { + implementation("com.google.code.gson:gson:2.11.0") +} + mcAutoTranslations { translationFunction.set("moe.nea.mcautotranslations.example.tr") translationFunctionResolved.set("moe.nea.mcautotranslations.example.trResolved") } -tasks.register("collectTranslations", CollectTranslations::class) { +val collectTranslations by tasks.registering(CollectTranslations::class) { this.baseTranslations.from(file("en_us.json")) this.classes.from(sourceSets.main.map { it.kotlin.classesDirectory }) - this.outputFile.set(layout.buildDirectory.file("compiled_en_us.json")) + this.outputFile.set(layout.buildDirectory.file("en_us.json")) } +tasks.processResources { from(collectTranslations) } diff --git a/example/src/main/kotlin/test.kt b/example/src/main/kotlin/test.kt index 291cdfe..7702dad 100644 --- a/example/src/main/kotlin/test.kt +++ b/example/src/main/kotlin/test.kt @@ -1,8 +1,20 @@ package moe.nea.mcautotranslations.example -data class Text(val key: String, val args: List) +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken -fun trResolved(key: String, vararg args: Any) = Text(key, args.toList()) +val resources = + Text::class.java.classLoader.getResourceAsStream("en_us.json")!!.reader().use { + Gson().fromJson(it, object : TypeToken>() {}) + } + +class Text(val key: String, val args: Array) { + override fun toString(): String { + return resources[key]!!.format(*args) + } +} + +fun trResolved(key: String, vararg args: Any) = Text(key, args) fun tr(key: String, default: String): Text = error("Did not run compiler plugin") fun main() { println(tr("test1", "Hiiiiiii")) -- cgit