Merge pull request #359 from InsanusMokrassar/0.20.21

0.20.21
This commit is contained in:
InsanusMokrassar 2023-12-12 23:49:26 +06:00 committed by GitHub
commit 8c76834ae4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 94 additions and 3 deletions

View File

@ -1,5 +1,10 @@
# Changelog # Changelog
## 0.20.21
* `Resources`:
* Inited
## 0.20.20 ## 0.20.20
* `Repos`: * `Repos`:

View File

@ -15,5 +15,5 @@ crypto_js_version=4.1.1
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.20.20 version=0.20.21
android_code_version=226 android_code_version=227

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

21
resources/build.gradle Normal file
View File

@ -0,0 +1,21 @@
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")
}
}
androidMain {
dependsOn(jvmMain)
}
}
}

View File

@ -0,0 +1,21 @@
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 = translation(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.locales[0]
} else {
configuration.locale
}
)
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,
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 lazyOf(value)
infix fun String.variant(value: Lazy<String>) = IetfLanguageCode(this) variant 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
}
inline fun buildStringResource(
default: String,
builder: StringResource.Builder.() -> Unit
): StringResource {
return StringResource.Builder(default).apply(builder).build()
}

View File

@ -0,0 +1,10 @@
package dev.inmo.micro_utils.strings
import dev.inmo.micro_utils.language_codes.toIetfLanguageCode
import java.util.Locale
fun StringResource.translation(locale: Locale = Locale.getDefault()): String {
return translation(locale.toIetfLanguageCode())
}
fun Locale.translation(resource: StringResource): String = resource.translation(this)

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",