Skip to content

Commit

Permalink
1.0.6: Key/object sorting, specify ICU message format version
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Nov 19, 2024
1 parent 08e51e1 commit 04fd6c8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
9 changes: 9 additions & 0 deletions changes/1.0.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# i18n tools 1.0.6

Updated the class generator to add a couple of features.

## Translations Class Generator

- **API:** Add a `messageFormatVersion` option to specify the ICU message format version specified in the `Bundle` object. Defaults to `1`, but you can use `2` for the experimental new format.
- **API/CLI:** Sort keys and objects alphabetically, making it easier to browse generated code.
- **CLI:** Expose the above option via the `-mfv` and `--message-format-version` switches.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ kotlin.incremental=true
org.gradle.jvmargs=-XX:MaxMetaspaceSize=1024m
org.gradle.parallel=true

projectVersion=1.0.5
projectVersion=1.0.6
18 changes: 17 additions & 1 deletion i18n-generator/src/main/kotlin/dev/kordex/i18n/generator/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import picocli.CommandLine.Model.OptionSpec
import java.io.File
import java.nio.charset.Charset
import java.nio.file.Files
import java.util.Properties
import java.util.*
import kotlin.system.exitProcess

public fun main(vararg args: String) {
Expand Down Expand Up @@ -62,6 +62,13 @@ public fun main(vararg args: String) {
description("Generated class name. Defaults to \"Translations\".")
}

spec.addOption<Int>("-mfv", "--message-format-version") {
paramLabel("VERSION")
defaultValue("1")

description("ICU Message Format version. Defaults to version 1, but you may specify version 2 if needed.")
}

spec.addOption<String>("-e", "--encoding") {
paramLabel("ENCODING")
defaultValue("UTF-8")
Expand Down Expand Up @@ -120,10 +127,18 @@ private fun run(result: CommandLine.ParseResult): Int {
val encoding: String = result.matchedOptionValue("e", "UTF-8")
val internal: Boolean = result.matchedOptionValue("in", false)
val noCamelCase: Boolean = result.matchedOptionValue("ncc", false)
val messageFormatVersion: Int = result.matchedOptionValue("mfv", 1)

val className: String = result.matchedOptionValue("c", "Translations")
val outputDir: File = result.matchedOptionValue("o", File("output"))

if (messageFormatVersion !in MESSAGE_FORMAT_VERSIONS) {
exitError(
"Invalid message format version $messageFormatVersion - " +
"must be one of ${MESSAGE_FORMAT_VERSIONS.joinToString()}"
)
}

if ("." !in bundle) {
bundle = "$bundle.strings"
}
Expand Down Expand Up @@ -158,6 +173,7 @@ private fun run(result: CommandLine.ParseResult): Int {
publicVisibility = !internal,
splitToCamelCase = !noCamelCase,
classPackage = classPackage,
messageFormatVersion = messageFormatVersion,
)

translationsClass.writeTo(outputDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

@file:Suppress("StringLiteralDuplication")

package dev.kordex.i18n.generator

import com.hanggrian.kotlinpoet.TypeSpecBuilder
Expand All @@ -15,9 +17,10 @@ import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import java.io.File
import java.util.Properties
import java.util.*

public val DELIMITERS: Array<String> = arrayOf("_", "-", ".")
public val MESSAGE_FORMAT_VERSIONS: Array<Int> = arrayOf(1, 2)

/**
* Representation of a generated translations object.
Expand Down Expand Up @@ -51,9 +54,9 @@ public class TranslationsClass(
@Deprecated("This option is provided for compatibility with old code, and will be removed in a future version.")
public val splitToCamelCase: Boolean = true,

public val classPackage: String
public val classPackage: String,
public val messageFormatVersion: Int = 1,
) {

init {
@Suppress("DEPRECATION")
if (!splitToCamelCase) {
Expand All @@ -66,6 +69,13 @@ public class TranslationsClass(

System.err.println("")
}

if (messageFormatVersion !in MESSAGE_FORMAT_VERSIONS) {
error(
"Invalid message format version $messageFormatVersion - " +
"must be one of ${MESSAGE_FORMAT_VERSIONS.joinToString()}"
)
}
}

/** KModifier represented by [publicVisibility]. **/
Expand All @@ -84,6 +94,10 @@ public class TranslationsClass(
* The [TranslationsClass] fills this automatically, and it is complete as soon as you've created one.
*/
public val spec: FileSpec = buildFileSpec(classPackage, className) {
if (messageFormatVersion != 1) {
this.addImport("dev.kordex.core.i18n.types", "MessageFormatVersion")
}

types.addObject(className) {
addModifiers(visibility)
bundle()
Expand Down Expand Up @@ -132,9 +146,9 @@ public class TranslationsClass(
translationsClassName: String,
parent: String? = null,
) {
val paritioned = keys.partition()
val partitioned = keys.sorted().partition()

paritioned.forEach { (k, v) ->
partitioned.forEach { (k, v) ->
val keyName = if (parent != null) {
"$parent.$k"
} else {
Expand All @@ -161,7 +175,12 @@ public class TranslationsClass(
properties.add(
buildPropertySpec("bundle", ClassName("dev.kordex.core.i18n.types", "Bundle")) {
addModifiers(visibility)
setInitializer("Bundle(%S)", bundle)

if (messageFormatVersion != 1) {
setInitializer("Bundle(%S, %L)", bundle, messageFormatVersion.toMessageFormatEnum())
} else {
setInitializer("Bundle(%S)", bundle)
}
}
)
}
Expand Down Expand Up @@ -206,8 +225,17 @@ public class TranslationsClass(
} else {
it.replace("-", " ")
.split(" ")
.map { it.capitalized() }
.joinToString("")
.joinToString("") { it.capitalized() }
}
}

public fun Int.toMessageFormatEnum(): String = when (this) {
1 -> "MessageFormatVersion.ONE"
2 -> "MessageFormatVersion.TWO"

else -> error(
"Invalid message format version $this - " +
"must be one of ${MESSAGE_FORMAT_VERSIONS.joinToString()}"
)
}
}

0 comments on commit 04fd6c8

Please sign in to comment.