Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // AuthMessages.kt
- object AuthMessages : Messages("auth") {
- object user {
- val username by messageKey()
- val password by messageKey()
- val roles by messageKey()
- val enabled by messageKey()
- }
- object action {
- val logout by messageKey()
- }
- object role {
- val users_viewing by messageKey()
- val users_editing by messageKey()
- }
- }
- // Messages.kt
- abstract class Messages(private val rootKey: String) {
- init {
- require(rootKey.isNotEmpty()) { "Root key can't be empty." }
- }
- protected fun messageKey() = object : ReadOnlyProperty<Any, String> {
- private var messageKey: String? = null
- override fun getValue(thisRef: Any, property: KProperty<*>): String {
- var key = messageKey
- if (key == null) {
- val nodeClassName = thisRef.javaClass.name
- val nestedClassNameIndex = nodeClassName.indexOf('$')
- key = rootKey + "." + when (nestedClassNameIndex) {
- -1 -> property.name // top level
- else -> { // nested
- val parentKey = nodeClassName.substring(nestedClassNameIndex + 1).replace('$', '.')
- "$parentKey.${property.name}"
- }
- }
- messageKey = key
- }
- return key
- }
- }
- }
- // auth/messages_pl.properties
- auth.user.username = Login
- auth.user.password = Hasło
- auth.user.enabled = Włączony?
- auth.user.roles = Role
- auth.action.logout = Wyloguj
- auth.role.users_viewing = Podgląd użytkowników
- auth.role.users_editing = Edycja użytkowników
- // example
- button(caption = i18n[AuthMessages.action.logout])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement