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

appleMain.net.codinux.i18n.platform.AppleLocale.kt Maven / Gradle / Ivy

Go to download

Localizing language, country, unit and currency names and formatting numbers and dates for Kotlin Multiplatform with the data from Unicode CLDR project

There is a newer version: 0.6.0
Show newest version
package net.codinux.i18n.platform

import net.codinux.collections.toImmutableList
import net.codinux.i18n.LanguageTag
import platform.Foundation.*

object AppleLocale {

    private val AvailableNSLocales by lazy {
        NSLocale.availableLocaleIdentifiers()
            .filterIsInstance()
            .map { NSLocale(it) }
    }

    val AvailableLocales: List by lazy {
        AvailableNSLocales
            .map { mapToUtilLocale(it) }
            .toImmutableList()
    }

    fun getDeviceLocale(): LanguageTag =
        mapToUtilLocale(NSLocale.currentLocale) // this returns the Device language

    fun getAppLanguage(): String? =
        NSLocale.preferredLanguages().firstOrNull() as? String
            ?: NSBundle.mainBundle().preferredLocalizations().firstOrNull() as? String

    fun nsLocaleForLocale(locale: LanguageTag): NSLocale? =
        nsLocaleForLanguageTag(locale.tag)
            ?: nsLocaleForLanguageTag("${locale.languageCode}_${locale.regionCode}")

    fun nsLocaleForLanguageTag(languageTag: String): NSLocale? =
        AvailableNSLocales.firstOrNull { it.localeIdentifier == languageTag }

    private fun mapToUtilLocale(locale: NSLocale) =
        LanguageTag(locale.localeIdentifier, locale.languageCode, locale.countryCode ?: "", locale.scriptCode, locale.variantCode)

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy