mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-29 13:38:45 +00:00
optimizations and improvements in resources
This commit is contained in:
parent
f6ded92251
commit
0d01561476
@ -13,5 +13,9 @@ kotlin {
|
|||||||
api project(":micro_utils.language_codes")
|
api project(":micro_utils.language_codes")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
androidMain {
|
||||||
|
dependsOn(jvmMain)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,12 @@ import android.content.res.Resources
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import dev.inmo.micro_utils.language_codes.toIetfLanguageCode
|
import dev.inmo.micro_utils.language_codes.toIetfLanguageCode
|
||||||
|
|
||||||
fun StringResource.translation(configuration: Configuration): String {
|
fun StringResource.translation(configuration: Configuration): String = translation(
|
||||||
val locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
configuration.locales[0]
|
configuration.locales[0]
|
||||||
} else {
|
} else {
|
||||||
configuration.locale
|
configuration.locale
|
||||||
}
|
}
|
||||||
|
)
|
||||||
return translation(locale.toIetfLanguageCode())
|
|
||||||
}
|
|
||||||
fun StringResource.translation(resources: Resources): String = translation(resources.configuration)
|
fun StringResource.translation(resources: Resources): String = translation(resources.configuration)
|
||||||
fun StringResource.translation(context: Context): String = translation(context.resources)
|
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)
|
|
@ -4,7 +4,7 @@ import dev.inmo.micro_utils.language_codes.IetfLanguageCode
|
|||||||
|
|
||||||
class StringResource(
|
class StringResource(
|
||||||
val default: String,
|
val default: String,
|
||||||
private val map: Map<IetfLanguageCode, Lazy<String>>
|
val map: Map<IetfLanguageCode, Lazy<String>>
|
||||||
) {
|
) {
|
||||||
class Builder(
|
class Builder(
|
||||||
var default: String
|
var default: String
|
||||||
@ -14,9 +14,9 @@ class StringResource(
|
|||||||
infix fun IetfLanguageCode.variant(value: Lazy<String>) {
|
infix fun IetfLanguageCode.variant(value: Lazy<String>) {
|
||||||
map[this] = value
|
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: Lazy<String>) = 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())
|
fun build() = StringResource(default, map.toMap())
|
||||||
}
|
}
|
||||||
|
@ -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())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user