mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-22 16:23:50 +00:00
update StringResource
This commit is contained in:
parent
afb066c4ee
commit
a374e53a3a
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
## 0.20.32
|
## 0.20.32
|
||||||
|
|
||||||
|
* `Resources`:
|
||||||
|
* Make `StringResource` serializable
|
||||||
|
* Add several variants of builder usages
|
||||||
|
|
||||||
## 0.20.31
|
## 0.20.31
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package dev.inmo.micro_utils.strings
|
package dev.inmo.micro_utils.strings
|
||||||
|
|
||||||
import dev.inmo.micro_utils.language_codes.IetfLang
|
import dev.inmo.micro_utils.language_codes.IetfLang
|
||||||
|
import kotlinx.serialization.KSerializer
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||||
|
import kotlinx.serialization.encoding.Decoder
|
||||||
|
import kotlinx.serialization.encoding.Encoder
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use this class as a type of your strings object fields. For example:
|
* Use this class as a type of your strings object fields. For example:
|
||||||
@ -15,9 +20,10 @@ import dev.inmo.micro_utils.language_codes.IetfLang
|
|||||||
*
|
*
|
||||||
* @see buildStringResource
|
* @see buildStringResource
|
||||||
*/
|
*/
|
||||||
class StringResource(
|
@Serializable(StringResource.Companion::class)
|
||||||
|
data class StringResource(
|
||||||
val default: String,
|
val default: String,
|
||||||
val map: Map<IetfLang, Lazy<String>>
|
val translations: Map<IetfLang, Lazy<String>>
|
||||||
) {
|
) {
|
||||||
class Builder(
|
class Builder(
|
||||||
var default: String
|
var default: String
|
||||||
@ -31,11 +37,18 @@ class StringResource(
|
|||||||
infix fun IetfLang.variant(value: () -> String) = this variant lazy(value)
|
infix fun IetfLang.variant(value: () -> String) = this variant lazy(value)
|
||||||
infix fun IetfLang.variant(value: String) = this variant lazyOf(value)
|
infix fun IetfLang.variant(value: String) = this variant lazyOf(value)
|
||||||
|
|
||||||
|
operator fun IetfLang.invoke(value: () -> String) = this variant value
|
||||||
|
operator fun IetfLang.invoke(value: String) = this variant value
|
||||||
|
|
||||||
|
|
||||||
infix fun String.variant(value: Lazy<String>) = IetfLang(this) variant value
|
infix fun String.variant(value: Lazy<String>) = IetfLang(this) variant value
|
||||||
infix fun String.variant(value: () -> String) = IetfLang(this) variant lazy(value)
|
infix fun String.variant(value: () -> String) = IetfLang(this) variant lazy(value)
|
||||||
infix fun String.variant(value: String) = this variant lazyOf(value)
|
infix fun String.variant(value: String) = this variant lazyOf(value)
|
||||||
|
|
||||||
|
|
||||||
|
operator fun String.invoke(value: () -> String) = this variant value
|
||||||
|
operator fun String.invoke(value: String) = this variant value
|
||||||
|
|
||||||
fun build() = StringResource(default, map.toMap())
|
fun build() = StringResource(default, map.toMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,17 +56,48 @@ class StringResource(
|
|||||||
if (languageCode == null) {
|
if (languageCode == null) {
|
||||||
return default
|
return default
|
||||||
}
|
}
|
||||||
map[languageCode] ?.let { return it.value }
|
translations[languageCode] ?.let { return it.value }
|
||||||
|
|
||||||
return languageCode.parentLang ?.let {
|
return languageCode.parentLang ?.let {
|
||||||
map[it] ?.value
|
translations[it] ?.value
|
||||||
} ?: default
|
} ?: default
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object : KSerializer<StringResource> {
|
||||||
|
@Serializable
|
||||||
|
private class Surrogate(
|
||||||
|
val default: String,
|
||||||
|
val translations: Map<String, String>
|
||||||
|
)
|
||||||
|
|
||||||
|
override val descriptor: SerialDescriptor
|
||||||
|
get() = Surrogate.serializer().descriptor
|
||||||
|
|
||||||
|
override fun deserialize(decoder: Decoder): StringResource {
|
||||||
|
val surrogate = Surrogate.serializer().deserialize(decoder)
|
||||||
|
return StringResource(
|
||||||
|
surrogate.default,
|
||||||
|
surrogate.translations.map { IetfLang(it.key) to lazyOf(it.value) }.toMap()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun serialize(encoder: Encoder, value: StringResource) {
|
||||||
|
Surrogate.serializer().serialize(
|
||||||
|
encoder,
|
||||||
|
Surrogate(
|
||||||
|
value.default,
|
||||||
|
value.translations.map {
|
||||||
|
it.key.code to it.value.value
|
||||||
|
}.toMap()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun buildStringResource(
|
inline fun buildStringResource(
|
||||||
default: String,
|
default: String,
|
||||||
builder: StringResource.Builder.() -> Unit
|
builder: StringResource.Builder.() -> Unit = {}
|
||||||
): StringResource {
|
): StringResource {
|
||||||
return StringResource.Builder(default).apply(builder).build()
|
return StringResource.Builder(default).apply(builder).build()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user