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

commonMain.de.comahe.i18n4k.strings.LocalizedStringMap.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.

There is a newer version: 0.9.0
Show newest version
package de.comahe.i18n4k.strings

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

/**
 * A simple implementation of [LocalizedString] that maps a locale values to string values.
 *
 * Attributes may also be specified with the `attributes` parameter.
 */
class LocalizedStringMap(
    private val messages: Map,
    private val attributes: LocalizedAttributable?
) : LocalizedString, LocalizedAttributable {

    constructor(
        vararg messages: Pair,
        attributes: LocalizedAttributable? = null
    ) :
        this(
            messages = messages.fold(mutableMapOf()) { map, pair ->
                map[pair.first] = pair.second
                return@fold map
            },
            attributes = attributes
        )

    override fun getAttribute(attributeName: CharSequence, locale: Locale?): String? {
        return attributes?.getAttribute(attributeName, locale)
    }

    override fun toString(): String = toString(null)


    override fun toString(locale: Locale?): String =
        applyLocales(locale) { messages[it] } ?: "?"
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy