All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.de.comahe.i18n4k.strings.LocalizedAttributeMap.kt Maven / Gradle / Ivy

Go to download

i18n4k is a multiplatform (JVM, JS, native) library and code generator for Kotlin to handle internationalisation (i18n) in your program.

The newest version!
package de.comahe.i18n4k.strings

import de.comahe.i18n4k.Locale
import de.comahe.i18n4k.applyLocales

/** A map of attribute values per locale that can be used attribute-based message formatters. */
class LocalizedAttributeMap(

    /** Key: attribute name; Value: map of locale to values */
    private val attributes: Map>
) : LocalizedAttributable {

    /** Uses a list of triples: attribute name, locale, value */
    constructor(vararg attributes: Triple) : this(
        attributes.fold(
            mutableMapOf>()
        ) { map, triple ->
            val localeValues = map.getOrPut(triple.first) { mutableMapOf() }
            localeValues[triple.second] = triple.third
            return@fold map
        }
    )

    override fun getAttribute(attributeName: CharSequence, locale: Locale?): String? {
        val localeValues = attributes[attributeName] ?: return null
        return applyLocales(locale) { localeValues[it] }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy