From 0d01561476f30fc89325a69e8c9fa4856811572e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 12 Dec 2023 21:39:58 +0600 Subject: [PATCH] optimizations and improvements in resources --- resources/build.gradle | 4 ++++ ...ourceGetter.kt => StringResourceContextGetter.kt} | 12 +++--------- resources/src/commonMain/kotlin/StringResource.kt | 6 +++--- .../src/jvmMain/kotlin/StringResourceLocaleGetter.kt | 8 ++++++++ 4 files changed, 18 insertions(+), 12 deletions(-) rename resources/src/androidMain/kotlin/{StringResourceGetter.kt => StringResourceContextGetter.kt} (57%) create mode 100644 resources/src/jvmMain/kotlin/StringResourceLocaleGetter.kt diff --git a/resources/build.gradle b/resources/build.gradle index 178fbdff3f5..ba3a7ca9d9e 100644 --- a/resources/build.gradle +++ b/resources/build.gradle @@ -13,5 +13,9 @@ kotlin { api project(":micro_utils.language_codes") } } + + androidMain { + dependsOn(jvmMain) + } } } diff --git a/resources/src/androidMain/kotlin/StringResourceGetter.kt b/resources/src/androidMain/kotlin/StringResourceContextGetter.kt similarity index 57% rename from resources/src/androidMain/kotlin/StringResourceGetter.kt rename to resources/src/androidMain/kotlin/StringResourceContextGetter.kt index 8c18b7ded4f..a7839d28898 100644 --- a/resources/src/androidMain/kotlin/StringResourceGetter.kt +++ b/resources/src/androidMain/kotlin/StringResourceContextGetter.kt @@ -6,18 +6,12 @@ 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) { +fun StringResource.translation(configuration: Configuration): String = translation( + 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) diff --git a/resources/src/commonMain/kotlin/StringResource.kt b/resources/src/commonMain/kotlin/StringResource.kt index 12c433be7b6..c7a332b20de 100644 --- a/resources/src/commonMain/kotlin/StringResource.kt +++ b/resources/src/commonMain/kotlin/StringResource.kt @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.language_codes.IetfLanguageCode class StringResource( val default: String, - private val map: Map> + val map: Map> ) { class Builder( var default: String @@ -14,9 +14,9 @@ class StringResource( infix fun IetfLanguageCode.variant(value: Lazy) { 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) = IetfLanguageCode(this) variant value - infix fun String.variant(value: String) = this variant lazy { value } + infix fun String.variant(value: String) = this variant lazyOf(value) fun build() = StringResource(default, map.toMap()) } diff --git a/resources/src/jvmMain/kotlin/StringResourceLocaleGetter.kt b/resources/src/jvmMain/kotlin/StringResourceLocaleGetter.kt new file mode 100644 index 00000000000..af9895439d5 --- /dev/null +++ b/resources/src/jvmMain/kotlin/StringResourceLocaleGetter.kt @@ -0,0 +1,8 @@ +package dev.inmo.micro_utils.strings + +import dev.inmo.micro_utils.language_codes.toIetfLanguageCode +import java.util.Locale + +fun StringResource.translation(locale: Locale): String { + return translation(locale.toIetfLanguageCode()) +}