init resources module

This commit is contained in:
InsanusMokrassar 2023-12-12 20:58:08 +06:00
parent d01b735cc6
commit f6ded92251
5 changed files with 77 additions and 0 deletions

View File

@ -2,6 +2,9 @@
## 0.20.21 ## 0.20.21
* `Resources`:
* Inited
## 0.20.20 ## 0.20.20
* `Repos`: * `Repos`:

17
resources/build.gradle Normal file
View 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")
}
}
}
}

View 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)

View 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()
}

View File

@ -43,6 +43,8 @@ String[] includes = [
":startup:plugin", ":startup:plugin",
":startup:launcher", ":startup:launcher",
":resources",
":fsm:common", ":fsm:common",
":fsm:repos:common", ":fsm:repos:common",