Skip to content

Commit

Permalink
1.0.5: Replace underscores with camel-case names
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Nov 4, 2024
1 parent cf3b735 commit 08e51e1
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
8 changes: 8 additions & 0 deletions changes/1.0.5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# i18n tools 1.0.5

Updated the class generator to remove common delimiters from names and camel-case them instead of replacing the delimiters with underscores.

## Translations Class Generator

- **API:** Add a (default on) option to remove common delimiters in names. You can disable this for compatibility with old code, but I'll remove the option in a future version.
- **CLI:** Expose the above option via the `-ncc` and `--no-camel-case` switches. I'll remove these when I remove the API option.
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.4
projectVersion=1.0.5
16 changes: 14 additions & 2 deletions i18n-generator/src/main/kotlin/dev/kordex/i18n/generator/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public fun main(vararg args: String) {
)
}

spec.addOption<Boolean>("-ncc", "--no-camel-case") {
paramLabel("CAMEL CASE")
defaultValue("false")

description(
"Replace common delimiters in names with underscores instead of camel-casing them. " +
"This option is provided for compatibility, and will be removed in the future.."
)
}

val commandLine = CommandLine(spec)

commandLine.setExecutionStrategy(::run)
Expand All @@ -109,6 +119,7 @@ private fun run(result: CommandLine.ParseResult): Int {
val classPackage: String = result.matchedOption("p").getValue()
val encoding: String = result.matchedOptionValue("e", "UTF-8")
val internal: Boolean = result.matchedOptionValue("in", false)
val noCamelCase: Boolean = result.matchedOptionValue("ncc", false)

val className: String = result.matchedOptionValue("c", "Translations")
val outputDir: File = result.matchedOptionValue("o", File("output"))
Expand Down Expand Up @@ -141,11 +152,12 @@ private fun run(result: CommandLine.ParseResult): Int {
println("Generating class \"$className\" for bundle \"$bundle\"...")

val translationsClass = TranslationsClass(
bundle = bundle,
allProps = props,
bundle = bundle,
className = className,
publicVisibility = !internal,
splitToCamelCase = !noCamelCase,
classPackage = classPackage,
publicVisibility = !internal
)

translationsClass.writeTo(outputDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.squareup.kotlinpoet.PropertySpec
import java.io.File
import java.util.Properties

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

/**
* Representation of a generated translations object.
*
Expand All @@ -36,6 +38,8 @@ import java.util.Properties
* @param publicVisibility Whether to use `public` (`true`) or `internal` (`false`) visibility modifiers in the
* generated code.
* Defaults to (`true`).
*
* @param splitToCamelCase Whether to replace common delimiters in generated names
*/
public class TranslationsClass(
public val allProps: Properties,
Expand All @@ -44,8 +48,26 @@ public class TranslationsClass(
public val className: String = "Translations",
public val publicVisibility: Boolean = true,

@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
) {

init {
@Suppress("DEPRECATION")
if (!splitToCamelCase) {
System.err.println("")

System.err.println(
"WARNING: Configured to replace delimiters with underscores instead of converting names to " +
"camel-case. This option will be removed in a future version."
)

System.err.println("")
}
}

/** KModifier represented by [publicVisibility]. **/
public val visibility: KModifier = if (publicVisibility) {
KModifier.PUBLIC
Expand Down Expand Up @@ -127,13 +149,7 @@ public class TranslationsClass(

if (v.isNotEmpty()) {
// Object
val objName = k
.replace("-", " ")
.split(" ")
.map { it.capitalized() }
.joinToString("")

types.addObject(objName) {
types.addObject(k.toClassName()) {
addModifiers(visibility)
addKeys(v, props, translationsClassName, keyName)
}
Expand Down Expand Up @@ -170,8 +186,28 @@ public class TranslationsClass(
public fun String.capitalized(): String =
replaceFirstChar { it.uppercase() }

public fun String.toVarName(): String =
replace("-", "_")
.replace(".", "_")
.replaceFirstChar { it.lowercase() }
@Suppress("DEPRECATION")
public fun String.toVarName(): String = let {
if (splitToCamelCase) {
toClassName()
.replaceFirstChar { it.lowercase() }
} else {
it.replace("-", "_")
.replace(".", "_")
.replaceFirstChar { it.lowercase() }
}
}

@Suppress("DEPRECATION", "SpreadOperator")
public fun String.toClassName(): String =
let {
if (splitToCamelCase) {
it.split(*DELIMITERS).joinToString("") { it.capitalized() }
} else {
it.replace("-", " ")
.split(" ")
.map { it.capitalized() }
.joinToString("")
}
}
}
4 changes: 2 additions & 2 deletions test/strings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ commands.button.name=button
commands.slap.action=slaps {0} with their {1}
commands.slap.args.target.description=Person you want to slap
commands.slap.args.target.name=target
commands.slap.args.weapon.description=What you want to slap with
commands.slap.args.weapon.name=weapon
commands.slap.args.weapon-description=What you want to slap with
commands.slap.args.weapon_name=weapon
commands.slap.description=Ask the bot to slap another user
commands.slap.name=slap

0 comments on commit 08e51e1

Please sign in to comment.