mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-26 10:37:14 +00:00
init resources module
This commit is contained in:
parent
d01b735cc6
commit
f6ded92251
@ -2,6 +2,9 @@
|
||||
|
||||
## 0.20.21
|
||||
|
||||
* `Resources`:
|
||||
* Inited
|
||||
|
||||
## 0.20.20
|
||||
|
||||
* `Repos`:
|
||||
|
17
resources/build.gradle
Normal file
17
resources/build.gradle
Normal file
@ -0,0 +1,17 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
id "com.android.library"
|
||||
}
|
||||
|
||||
apply from: "$mppJvmJsAndroidLinuxMingwLinuxArm64ProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api project(":micro_utils.language_codes")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
resources/src/androidMain/kotlin/StringResourceGetter.kt
Normal file
23
resources/src/androidMain/kotlin/StringResourceGetter.kt
Normal file
@ -0,0 +1,23 @@
|
||||
package dev.inmo.micro_utils.strings
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import android.os.Build
|
||||
import dev.inmo.micro_utils.language_codes.toIetfLanguageCode
|
||||
|
||||
fun StringResource.translation(configuration: Configuration): String {
|
||||
val locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
configuration.locales[0]
|
||||
} else {
|
||||
configuration.locale
|
||||
}
|
||||
|
||||
return translation(locale.toIetfLanguageCode())
|
||||
}
|
||||
fun StringResource.translation(resources: Resources): String = translation(resources.configuration)
|
||||
fun StringResource.translation(context: Context): String = translation(context.resources)
|
||||
|
||||
fun Configuration.translation(resource: StringResource): String = resource.translation(this)
|
||||
fun Resources.translation(resource: StringResource): String = configuration.translation(resource)
|
||||
fun Context.translation(resource: StringResource): String = resources.translation(resource)
|
32
resources/src/commonMain/kotlin/StringResource.kt
Normal file
32
resources/src/commonMain/kotlin/StringResource.kt
Normal file
@ -0,0 +1,32 @@
|
||||
package dev.inmo.micro_utils.strings
|
||||
|
||||
import dev.inmo.micro_utils.language_codes.IetfLanguageCode
|
||||
|
||||
class StringResource(
|
||||
val default: String,
|
||||
private val map: Map<IetfLanguageCode, Lazy<String>>
|
||||
) {
|
||||
class Builder(
|
||||
var default: String
|
||||
) {
|
||||
private val map = mutableMapOf<IetfLanguageCode, Lazy<String>>()
|
||||
|
||||
infix fun IetfLanguageCode.variant(value: Lazy<String>) {
|
||||
map[this] = value
|
||||
}
|
||||
infix fun IetfLanguageCode.variant(value: String) = this variant lazy { value }
|
||||
infix fun String.variant(value: Lazy<String>) = IetfLanguageCode(this) variant value
|
||||
infix fun String.variant(value: String) = this variant lazy { value }
|
||||
|
||||
fun build() = StringResource(default, map.toMap())
|
||||
}
|
||||
|
||||
fun translation(languageCode: IetfLanguageCode): String = (map[languageCode] ?: map[IetfLanguageCode(languageCode.withoutDialect)]) ?.value ?: default
|
||||
}
|
||||
|
||||
inline fun buildStringResource(
|
||||
default: String,
|
||||
builder: StringResource.Builder.() -> Unit
|
||||
): StringResource {
|
||||
return StringResource.Builder(default).apply(builder).build()
|
||||
}
|
@ -43,6 +43,8 @@ String[] includes = [
|
||||
":startup:plugin",
|
||||
":startup:launcher",
|
||||
|
||||
":resources",
|
||||
|
||||
":fsm:common",
|
||||
":fsm:repos:common",
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user