a lot of improvements in language codes

This commit is contained in:
2023-12-21 15:41:11 +06:00
parent 7ab21871cd
commit 48d3fe41f2
8 changed files with 2992 additions and 2477 deletions

View File

@@ -2,6 +2,19 @@ package dev.inmo.micro_utils.strings
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
/**
* Use this class as a type of your strings object fields. For example:
*
* ```kotlin
* object Strings {
* val someResource: StringResource
* }
* ```
*
* Use [buildStringResource] for useful creation of string resource
*
* @see buildStringResource
*/
class StringResource(
val default: String,
val map: Map<IetfLanguageCode, Lazy<String>>
@@ -14,14 +27,24 @@ class StringResource(
infix fun IetfLanguageCode.variant(value: Lazy<String>) {
map[this] = value
}
infix fun IetfLanguageCode.variant(value: () -> String) = this variant lazy(value)
infix fun IetfLanguageCode.variant(value: String) = this variant lazyOf(value)
infix fun String.variant(value: Lazy<String>) = IetfLanguageCode(this) variant value
infix fun String.variant(value: () -> String) = IetfLanguageCode(this) variant lazy(value)
infix fun String.variant(value: String) = this variant lazyOf(value)
fun build() = StringResource(default, map.toMap())
}
fun translation(languageCode: IetfLanguageCode): String = (map[languageCode] ?: map[IetfLanguageCode(languageCode.withoutDialect)]) ?.value ?: default
fun translation(languageCode: IetfLanguageCode): String {
map[languageCode] ?.let { return it.value }
return languageCode.parentLang ?.let {
map[it] ?.value
} ?: default
}
}
inline fun buildStringResource(